Skip to content

Commit

Permalink
Squiz.PHP.InnerFunctions now handles multiple nested anon classes cor…
Browse files Browse the repository at this point in the history
…rectly (ref #2701)
  • Loading branch information
gsherwood committed Jan 5, 2020
1 parent 48302f1 commit 77c5fa2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<notes>
- The PHP 7.4 numeric separator backfill now works correctly for more float formats
- The PHP 7.4 numeric separator backfill is no longer run on PHP version 7.4.0 or greater
- File::getCondition() now accepts a 3rd argument that allows for the closest matching token to be returned
-- By default, it continues to return the first matched token found from the top of the file
- Added Generic.PHP.DisallowRequestSuperglobal to ban the use of the $_REQUEST superglobal
-- Thanks to Morerice for the contribution
- Squiz.PHP.InnerFunctions now handles multiple nested anon classes correctly
- Fixed bug #2688 : Case statements not tokenized correctly when switch is contained within ternary
- Fixed bug #2698 : PHPCS throws errors determining auto report width when shell_exec is disabled
-- Thanks to Matthew Peveler for the patch
Expand Down
10 changes: 9 additions & 1 deletion src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2490,10 +2490,14 @@ public function hasCondition($stackPtr, $types)
*
* @param int $stackPtr The position of the token we are checking.
* @param int|string $type The type of token to search for.
* @param bool $first If TRUE, will return the matched condition
* furtherest away from the passed token.
* If FALSE, will return the matched condition
* closest to the passed token.
*
* @return int|false
*/
public function getCondition($stackPtr, $type)
public function getCondition($stackPtr, $type, $first=true)
{
// Check for the existence of the token.
if (isset($this->tokens[$stackPtr]) === false) {
Expand All @@ -2506,6 +2510,10 @@ public function getCondition($stackPtr, $type)
}

$conditions = $this->tokens[$stackPtr]['conditions'];
if ($first === false) {
$conditions = array_reverse($conditions, true);
}

foreach ($conditions as $token => $condition) {
if ($condition === $type) {
return $token;
Expand Down
2 changes: 1 addition & 1 deletion src/Standards/Squiz/Sniffs/PHP/InnerFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$class = $phpcsFile->getCondition($stackPtr, T_ANON_CLASS);
$class = $phpcsFile->getCondition($stackPtr, T_ANON_CLASS, false);
if ($class !== false && $class > $function) {
// Ignore methods in anon classes.
return;
Expand Down
21 changes: 21 additions & 0 deletions src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ function test()
}
};
}

new class {
public function valueObject(): object
{
return new class {
public function string(): string {
return 'string';
}
};
}
};

new class {
public function outer()
{
if (!function_exists('inner')) {
function inner() {
}
}
}
};
5 changes: 4 additions & 1 deletion src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class InnerFunctionsUnitTest extends AbstractSniffUnitTest
*/
public function getErrorList()
{
return [5 => 1];
return [
5 => 1,
46 => 1,
];

}//end getErrorList()

Expand Down

0 comments on commit 77c5fa2

Please sign in to comment.