Skip to content

Commit

Permalink
Merge pull request #505 from Automattic/fix/448-detect-correct-walker…
Browse files Browse the repository at this point in the history
…-walk-signature

DeclarationCompatibility: fix incorrect signature check for `Walker::walk()`
  • Loading branch information
GaryJones authored Jul 21, 2020
2 parents df43dcd + eadda0a commit e523148
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 e523148

Please sign in to comment.