Skip to content

Commit

Permalink
DeclarationCompatibility: fix incorrect signature check for `Walker::…
Browse files Browse the repository at this point in the history
…walk()`

... and `Walker::paged_walk()`.

The method signature of both the `Walker::walk()` method as well as the `Walker::paged_walk()` method were changed in WordPress 5.3 to make the variadic nature of the allowed arguments explicit.

This changes update the sniff to verify the signature of these methods in child classes against the new method signature as it is in WordPress Core.

Refs:
* https://make.wordpress.org/core/2019/10/09/wp-5-3-introducing-the-spread-operator/
* https://core.trac.wordpress.org/changeset/46442

Fixes 448
  • Loading branch information
jrfnl committed Jul 21, 2020
1 parent df43dcd commit eadda0a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,18 @@ class DeclarationCompatibilitySniff extends AbstractScopeSniff {
'walk' => [
'elements',
'max_depth',
'args' => [
'variable_length' => true,
],
],
'paged_walk' => [
'elements',
'max_depth',
'page_num',
'per_page',
'args' => [
'variable_length' => true,
],
],
'get_number_of_root_elements' => [
'elements',
Expand Down Expand Up @@ -276,6 +282,9 @@ protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currSco
) || (
true === array_key_exists( 'pass_by_reference', $param ) &&
$param['pass_by_reference'] !== $signatureParams[ $i ]['pass_by_reference']
) || (
true === array_key_exists( 'variable_length', $param ) &&
$param['variable_length'] !== $signatureParams[ $i ]['variable_length']
)
) {
$this->addError( $originalParentClassName, $methodName, $signatureParams, $parentSignature, $phpcsFile, $stackPtr );
Expand Down Expand Up @@ -331,6 +340,10 @@ private function generateParamList( $methodSignature ) {
$paramName .= $param;
}

if ( true === array_key_exists( 'variable_length', $options ) && true === $options['variable_length'] ) {
$paramName = '...' . $paramName;
}

if ( true === array_key_exists( 'pass_by_reference', $options ) && true === $options['pass_by_reference'] ) {
$paramName = '&' . $paramName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,13 @@ class MyWalker extends Walker {

function unset_children( $el, $children_elements ) {
} // Bad. $children_elements should be passed by reference
}

function walk( $elements, $max_depth, ...$args ) {
} // Ok.

function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
} // Bad. Missing $args.

function paged_walk( $elements, $max_depth, $page_num, $per_page, $args ) {
} // Bad. $args is not variadic.
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function getErrorList() {
112 => 1,
119 => 1,
128 => 1,
134 => 1,
137 => 1,
];
}

Expand Down

0 comments on commit eadda0a

Please sign in to comment.