diff --git a/package.xml b/package.xml index aa4f705337..91c6eb990d 100644 --- a/package.xml +++ b/package.xml @@ -28,8 +28,11 @@ http://pear.php.net/dtd/package-2.0.xsd"> - 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 diff --git a/src/Files/File.php b/src/Files/File.php index 31c263230e..96b971d2b0 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -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) { @@ -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; diff --git a/src/Standards/Squiz/Sniffs/PHP/InnerFunctionsSniff.php b/src/Standards/Squiz/Sniffs/PHP/InnerFunctionsSniff.php index c83cb35a9d..4e3ee2152c 100644 --- a/src/Standards/Squiz/Sniffs/PHP/InnerFunctionsSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/InnerFunctionsSniff.php @@ -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; diff --git a/src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.inc b/src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.inc index 732402962c..dd851461b9 100644 --- a/src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.inc +++ b/src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.inc @@ -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() { + } + } + } +}; diff --git a/src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.php b/src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.php index 595ea10b9d..3c9ad07bd3 100644 --- a/src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.php +++ b/src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.php @@ -25,7 +25,10 @@ class InnerFunctionsUnitTest extends AbstractSniffUnitTest */ public function getErrorList() { - return [5 => 1]; + return [ + 5 => 1, + 46 => 1, + ]; }//end getErrorList()