Skip to content

Commit 5067950

Browse files
committed
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
1 parent 1cdf865 commit 5067950

5 files changed

Lines changed: 37 additions & 11 deletions

File tree

‎src/wp-includes/widgets.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,9 @@ function dynamic_sidebar( $index = 1 ) {
790790
* @see register_sidebar()
791791
*
792792
* @param array $params {
793-
* @type array $args {
793+
* The arguments the display callback is called with, in order.
794+
*
795+
* @type array $0 {
794796
* An array of widget display arguments.
795797
*
796798
* @type string $name Name of the sidebar the widget is assigned to.
@@ -804,8 +806,8 @@ function dynamic_sidebar( $index = 1 ) {
804806
* @type string $widget_id ID of the widget.
805807
* @type string $widget_name Name of the widget.
806808
* }
807-
* @type array $widget_args {
808-
* An array of multi-widget arguments.
809+
* @type array ...$1 {
810+
* The parameters the widget was registered with, such as the multi-widget arguments.
809811
*
810812
* @type int $number Number increment used for multiples of the same widget.
811813
* }

‎tests/phpstan/HashNotationVisitor.php‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090
* is reported rather than silently typed as `mixed` — unless the hash itself
9191
* says otherwise, by listing a `...$N` entry beside its named keys.
9292
*
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+
*
9399
* @link https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#1-1-parameters-that-are-arrays Hash notation in the documentation standards.
94100
* @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.
95101
*
@@ -163,14 +169,17 @@ final class HashNotationVisitor extends NodeVisitorAbstract {
163169
/**
164170
* Translates the hashes in a node's docblock into `@phpstan-*` shapes.
165171
*
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+
*
166179
* @param Node $node The node being entered.
167180
* @return null
168181
*/
169182
public function enterNode( Node $node ): ?Node {
170-
if ( ! $node instanceof Node\FunctionLike ) {
171-
return null;
172-
}
173-
174183
$doc = $node->getDocComment();
175184
if ( null === $doc ) {
176185
return null;
@@ -181,7 +190,14 @@ public function enterNode( Node $node ): ?Node {
181190
return null;
182191
}
183192

184-
$additions = $this->build_additions( $text, $this->by_reference_parameters( $node ) );
193+
/*
194+
* Only a declaration has parameters to take by reference. The `@param` tags of
195+
* a hook docblock name the arguments passed to the hook, which are values, so
196+
* nothing there is excluded on that ground.
197+
*/
198+
$by_reference = $node instanceof Node\FunctionLike ? $this->by_reference_parameters( $node ) : array();
199+
200+
$additions = $this->build_additions( $text, $by_reference );
185201
if ( array() === $additions ) {
186202
return null;
187203
}

‎tests/phpstan/HookDocBlock.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,16 @@ private static function parseHookDocs( string $code ): array {
628628
return $docs;
629629
}
630630

631-
// Propagate each docblock down to the nested hook-call node.
631+
/*
632+
* Translate hash notation before the docblocks are collected, and propagate each
633+
* docblock down to the nested hook-call node. The file is parsed here rather than
634+
* through PHPStan's parser, so the visitors configured in base.neon have not run
635+
* over it: without the first one, a hook documented with a hash is typed from the
636+
* shape where its docblock is written at the call and from the bare `array` where
637+
* the same docblock is inherited through a reference comment.
638+
*/
632639
$traverser = new NodeTraverser();
640+
$traverser->addVisitor( new HashNotationVisitor() );
633641
$traverser->addVisitor( new HookDocsVisitor() );
634642
$stmts = $traverser->traverse( $stmts );
635643

‎tests/phpstan/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ A hash on a class rather than on `array` or `object` produces an intersection, `
9292

9393
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.
9494

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.
9696

9797
### Hook documentation
9898

‎tests/phpstan/baselines/return.type.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ parameters:
7979
count: 1
8080
path: ../../../src/wp-includes/customize/class-wp-customize-nav-menu-setting.php
8181
-
82-
message: '#^Function _wp_filter_font_directory\(\) should return string but returns array\.$#'
82+
message: '#^Function _wp_filter_font_directory\(\) should return string but returns array\<mixed\>\.$#'
8383
identifier: return.type
8484
count: 1
8585
path: ../../../src/wp-includes/fonts.php

0 commit comments

Comments
 (0)