Skip to content

Commit 8b6fc76

Browse files
committed
Document the code style check of the PHP blocks in feature files
Adds a section alongside the one for the static analysis, covering which docstrings are checked and why a docstring merely opening with `<?php` is not among them, where the defaults live, and how a package replaces them. The README is regenerated from the partials on push to main.
1 parent 94222b2 commit 8b6fc76

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ To make use of the WP-CLI testing framework, you need to complete the following
7878
```
7979
8080
All other [PHPCS configuration options](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset) are, of course, available.
81+
The PHP snippets embedded in your feature files are checked along with the rest of the package. See [Checking the code style of the PHP blocks in feature files](#checking-the-code-style-of-the-php-blocks-in-feature-files) below.
82+
8183
6. Optionally add a `phpstan-feature-files.neon.dist` file to the package root to also run PHPStan over the PHP snippets embedded in your feature files. See [Analysing the PHP blocks in feature files](#analysing-the-php-blocks-in-feature-files) below.
8284
8385
7. Update your composer dependencies and regenerate your autoloader and binary folders:
@@ -165,6 +167,56 @@ Two kinds of blocks are left out of the analysis, and are listed at the end of t
165167
Blocks that declare the same class or function as another block are analysed separately from each
166168
other, so that PHPStan does not resolve a name to the wrong block's declaration.
167169

170+
### Checking the code style of the PHP blocks in feature files
171+
172+
`composer phpcs` also checks the PHP snippets that feature files embed in docstrings, and
173+
`composer phpcbf` fixes them in place. No configuration is needed, and like the analysis above the
174+
blocks are padded so that findings are reported against the feature file itself:
175+
176+
```text
177+
FILE: features/command.feature
178+
----------------------------------------------------------------------
179+
FOUND 1 ERROR AFFECTING 1 LINE
180+
----------------------------------------------------------------------
181+
438 | ERROR | [x] Expected 1 space after IF keyword; 0 found
182+
----------------------------------------------------------------------
183+
```
184+
185+
Only a docstring belonging to a step that creates a `.php` file is checked:
186+
187+
```gherkin
188+
Given a wp-content/mu-plugins/test-harness.php file:
189+
"""
190+
<?php
191+
WP_CLI::add_command( 'test-harness', 'Test_Harness' );
192+
"""
193+
```
194+
195+
Unlike the analysis above, a docstring that merely opens with `<?php` does not count. Those are
196+
routinely an expectation about the contents of a file rather than a file, and reformatting one would
197+
make it stop matching what it is checked against.
198+
199+
The defaults leave out the sniffs that look at a block as if it were a file of its own, along with
200+
those that ask of a fixture what is only worth asking of production code. They live in
201+
`phpcs/feature-files.sh` and are shared by the check and the fixer, so that the two cannot disagree
202+
over which sniff applies. A package replaces them wholesale by adding a `phpcs-feature-files.xml`
203+
(or `phpcs-feature-files.xml.dist`) ruleset to its root:
204+
205+
```xml
206+
<?xml version="1.0"?>
207+
<ruleset name="WP-CLI-PROJECT-NAME-feature-files">
208+
<arg name="warning-severity" value="0"/>
209+
210+
<rule ref="WP_CLI_CS">
211+
<exclude name="Generic.Files.InlineHTML"/>
212+
<exclude name="Squiz.Commenting.FileComment"/>
213+
</rule>
214+
</ruleset>
215+
```
216+
217+
The blocks are left alone when a run is narrowed down to a path, as in `composer phpcs -- src/`,
218+
since such an argument is about the files of the package itself.
219+
168220
### Controlling what to test
169221

170222
To send one or more arguments to one of the test tools, prepend the argument(s) with a double dash. As an example, here's how to run the functional tests for a specific feature file only:

0 commit comments

Comments
 (0)