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 5067950
Browse filesBrowse the repository at this point in the historyBrowse files
Build/Test Tools: Read hash notation on hook docblocks.
The visitor that translates hash notation into a PHPStan shape only entered a function-like node, and a hook's docblock is attached to the statement that fires the hook rather than to a function. Every hash core writes there was left as free text, so the value a filter handed back stayed a bare `array`. This undid, at the call, the shape the enclosing function's own hash had just established.
The visitor now considers every node carrying a docblock, and `HookDocBlock::parseHookDocs()` runs it over the files it parses itself, so a hook is typed alike wherever it is fired. One baselined message is re-rendered as a result.
Trusting these hashes surfaced one inaccurate docblock. The array the `dynamic_sidebar_params` filter passes is positional, but its hash named the two entries `$args` and `$widget_args`, keys the array never has. They are now documented as `$0` and `...$1`.
Developed in WordPress#13644.
Follow-up to r62939, r63420.
Props westonruter, swissspidy.
See #65817.
git-svn-id: https://develop.svn.wordpress.org/trunk@63789 602fd350-edb4-49c9-b593-d223f7449a82
Copy file name to clipboardExpand all lines: tests/phpstan/HashNotationVisitor.php
+21-5Lines changed: 21 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -90,6 +90,12 @@
90
90
* is reported rather than silently typed as `mixed` — unless the hash itself
91
91
* says otherwise, by listing a `...$N` entry beside its named keys.
92
92
*
93
+
* The docblock of a hook is translated on the same terms as the docblock of a
94
+
* function. It documents the arguments passed to the hook, and for a filter the
95
+
* first of them is also the value the call returns, so a hash left untranslated
96
+
* there widens that value back to a bare `array` — undoing, at the call, the
97
+
* shape the enclosing function's own hash established.
98
+
*
93
99
* @link https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#1-1-parameters-that-are-arrays Hash notation in the documentation standards.
94
100
* @link https://github.com/php-stubs/wordpress-stubs/blob/master/src/Visitor.php The equivalent translation php-stubs/wordpress-stubs performs when generating stubs, MIT license.
95
101
*
@@ -163,14 +169,17 @@ final class HashNotationVisitor extends NodeVisitorAbstract {
163
169
/**
164
170
* Translates the hashes in a node's docblock into `@phpstan-*` shapes.
165
171
*
172
+
* Every node carrying a docblock is considered, not only a declaration. A hook's
173
+
* docblock is written above the statement that calls it rather than above a
174
+
* function, and HookDocBlock resolves the hook's types out of that text, so a
175
+
* hash left untranslated there types the filtered value as a bare `array` at
176
+
* every call site — including the one the hash was written for, where the value
177
+
* goes back into the very function whose parameter the hash describes.
178
+
*
166
179
* @param Node $node The node being entered.
167
180
* @return null
168
181
*/
169
182
publicfunctionenterNode( Node$node ): ?Node {
170
-
if ( ! $nodeinstanceofNode\FunctionLike ) {
171
-
returnnull;
172
-
}
173
-
174
183
$doc = $node->getDocComment();
175
184
if ( null === $doc ) {
176
185
returnnull;
@@ -181,7 +190,14 @@ public function enterNode( Node $node ): ?Node {
Copy file name to clipboardExpand all lines: tests/phpstan/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,7 +92,7 @@ A hash on a class rather than on `array` or `object` produces an intersection, `
92
92
93
93
One kind of hash is outside what the visitor covers today: **a `@var` hash on a property**. A property declaration is inherited by every subclass and has to accept its own default, so a shape there would say more than the hash does — that no subclass may widen the property, and that the declared default already has the shape.
94
94
95
-
Hashes are also written on hook docblocks, where core documents `apply_filters()` and `do_action()`. Those are not attached to a function, so they are outside what this visitor sees, and the value a filter passes stays typed by [the hook extensions below](#hook-documentation).
95
+
Hashes are also written on hook docblocks, where core documents `apply_filters()` and `do_action()`. Those are not attached to a function, but they are translated on the same terms, because [the hook extensions below](#hook-documentation) type a filtered value from the `@param` the hook documents. Without it a filter hands back a bare `array` — undoing, at the call, the shape the enclosing function's own hash established, as `wp_set_cookie()` does when it filters the `$options` its signature documents key by key. The same translation is applied where a docblock is inherited through a `/** This filter is documented in <file> */` comment, so a hook is typed alike wherever it is fired.
0 commit comments