You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4f4ce95
Browse filesBrowse the repository at this point in the historyBrowse files
Add PHPStan analysis for PHP blocks in feature files
Feature files embed PHP snippets in docstrings that no static analysis tool
currently looks at. This extracts those snippets into standalone PHP files
padded so that their line numbers match the feature file, runs PHPStan over
them, and reports the findings against the feature file they came from.
PHPStan needs a few things that a code style check does not:
* The opening tag has to come first. Padding in front of it counts as inline
HTML, which makes a `declare()` or `namespace` statement in a block a fatal
error, so the tag a block brings along itself is dropped instead.
* Blocks that are not standalone PHP have to be left out, since a single file
that fails to parse makes PHPStan abort the whole run. Feature files hold
deliberately broken snippets as well as Behat placeholders such as
`{POST_ID}` that are substituted before the snippet is written to disk.
* Blocks declaring the same class or function are analysed separately from
each other, so that a name is not resolved to another block's declaration.
The analysis is opt-in through a `phpstan-feature-files.neon[.dist]` file in
the package root, layered on top of the defaults in `phpstan/feature-files.neon`.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M4oxq4G2FxhwJfHd3w6VLw
Copy file name to clipboardExpand all lines: .readme-partials/USING.md
+62-1Lines changed: 62 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,11 +13,13 @@ To make use of the WP-CLI testing framework, you need to complete the following
13
13
"lint": "run-linter-tests",
14
14
"phpcs": "run-phpcs-tests",
15
15
"phpcbf": "run-phpcbf-cleanup",
16
+
"phpstan": "run-phpstan-tests",
16
17
"phpunit": "run-php-unit-tests",
17
18
"prepare-tests": "install-package-tests",
18
19
"test": [
19
20
"@lint",
20
21
"@phpcs",
22
+
"@phpstan",
21
23
"@phpunit",
22
24
"@behat"
23
25
]
@@ -76,7 +78,9 @@ To make use of the WP-CLI testing framework, you need to complete the following
76
78
```
77
79
78
80
All other [PHPCS configuration options](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset) are, of course, available.
79
-
6. Update your composer dependencies and regenerate your autoloader and binary folders:
81
+
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.
82
+
83
+
7. Update your composer dependencies and regenerate your autoloader and binary folders:
80
84
```bash
81
85
composer update
82
86
```
@@ -92,9 +96,66 @@ You can use the following commands to control the tests:
92
96
* `composer lint` - Run only the linting test suite.
93
97
* `composer phpcs` - Run only the code sniffer test suite.
94
98
* `composer phpcbf` - Run only the code sniffer cleanup.
99
+
* `composer phpstan` - Run only the static analysis.
95
100
* `composer phpunit` - Run only the unit test suite.
96
101
* `composer behat` - Run only the functional test suite.
97
102
103
+
### Analysing the PHP blocks in feature files
104
+
105
+
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally
106
+
look at:
107
+
108
+
```gherkin
109
+
Given a wp-content/mu-plugins/test-harness.php file:
The defaults in `phpstan/feature-files.neon` are applied first, so the file only needs to hold what
128
+
it wants to change. An empty set of parameters is enough to run with the defaults:
129
+
130
+
```yaml
131
+
parameters:
132
+
level: 5
133
+
```
134
+
135
+
Do note that snippets in feature files are fixtures, not production code, and that they run inside a
136
+
WordPress installation the analysis knows nothing about. Expect to have to ignore errors that are
137
+
not actually wrong, such as functions a scenario deliberately leaves undefined:
138
+
139
+
```yaml
140
+
parameters:
141
+
level: 5
142
+
ignoreErrors:
143
+
-
144
+
identifier: function.notFound
145
+
path: features/shutdown-handler.feature
146
+
```
147
+
148
+
Two kinds of blocks are left out of the analysis, and are listed at the end of the run:
149
+
150
+
* Blocks that are not standalone PHP, such as snippets holding a placeholder that Behat substitutes
151
+
(`get_the_title( {POST_ID} )`) or code that is deliberately broken to test error handling. PHPStan
152
+
stops analysing altogether when a single file fails to parse, so these have to be skipped.
153
+
* Docstrings that do not belong to a step creating a `.php` file, since those are not necessarily
154
+
PHP at all.
155
+
156
+
Blocks that declare the same class or function as another block are analysed separately from each
157
+
other, so that PHPStan does not resolve a name to the wrong block's declaration.
158
+
98
159
### Controlling what to test
99
160
100
161
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:
Copy file name to clipboardExpand all lines: README.md
+62-1Lines changed: 62 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,11 +24,13 @@ To make use of the WP-CLI testing framework, you need to complete the following
24
24
"lint": "run-linter-tests",
25
25
"phpcs": "run-phpcs-tests",
26
26
"phpcbf": "run-phpcbf-cleanup",
27
+
"phpstan": "run-phpstan-tests",
27
28
"phpunit": "run-php-unit-tests",
28
29
"prepare-tests": "install-package-tests",
29
30
"test": [
30
31
"@lint",
31
32
"@phpcs",
33
+
"@phpstan",
32
34
"@phpunit",
33
35
"@behat"
34
36
]
@@ -87,7 +89,9 @@ To make use of the WP-CLI testing framework, you need to complete the following
87
89
```
88
90
89
91
All other [PHPCS configuration options](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset) are, of course, available.
90
-
6. Update your composer dependencies and regenerate your autoloader and binary folders:
92
+
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.
93
+
94
+
7. Update your composer dependencies and regenerate your autoloader and binary folders:
91
95
```bash
92
96
composer update
93
97
```
@@ -103,9 +107,66 @@ You can use the following commands to control the tests:
103
107
* `composer lint` - Run only the linting test suite.
104
108
* `composer phpcs` - Run only the code sniffer test suite.
105
109
* `composer phpcbf` - Run only the code sniffer cleanup.
110
+
* `composer phpstan` - Run only the static analysis.
106
111
* `composer phpunit` - Run only the unit test suite.
107
112
* `composer behat` - Run only the functional test suite.
108
113
114
+
### Analysing the PHP blocks in feature files
115
+
116
+
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally
117
+
look at:
118
+
119
+
```gherkin
120
+
Given a wp-content/mu-plugins/test-harness.php file:
The defaults in `phpstan/feature-files.neon` are applied first, so the file only needs to hold what
139
+
it wants to change. An empty set of parameters is enough to run with the defaults:
140
+
141
+
```yaml
142
+
parameters:
143
+
level: 5
144
+
```
145
+
146
+
Do note that snippets in feature files are fixtures, not production code, and that they run inside a
147
+
WordPress installation the analysis knows nothing about. Expect to have to ignore errors that are
148
+
not actually wrong, such as functions a scenario deliberately leaves undefined:
149
+
150
+
```yaml
151
+
parameters:
152
+
level: 5
153
+
ignoreErrors:
154
+
-
155
+
identifier: function.notFound
156
+
path: features/shutdown-handler.feature
157
+
```
158
+
159
+
Two kinds of blocks are left out of the analysis, and are listed at the end of the run:
160
+
161
+
* Blocks that are not standalone PHP, such as snippets holding a placeholder that Behat substitutes
162
+
(`get_the_title( {POST_ID} )`) or code that is deliberately broken to test error handling. PHPStan
163
+
stops analysing altogether when a single file fails to parse, so these have to be skipped.
164
+
* Docstrings that do not belong to a step creating a `.php` file, since those are not necessarily
165
+
PHP at all.
166
+
167
+
Blocks that declare the same class or function as another block are analysed separately from each
168
+
other, so that PHPStan does not resolve a name to the wrong block's declaration.
169
+
109
170
### Controlling what to test
110
171
111
172
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