Skip to content

Commit 77c5fa2

Browse files
committed
Squiz.PHP.InnerFunctions now handles multiple nested anon classes correctly (ref #2701)
1 parent 48302f1 commit 77c5fa2

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
2828
<notes>
2929
- The PHP 7.4 numeric separator backfill now works correctly for more float formats
3030
- The PHP 7.4 numeric separator backfill is no longer run on PHP version 7.4.0 or greater
31+
- File::getCondition() now accepts a 3rd argument that allows for the closest matching token to be returned
32+
-- By default, it continues to return the first matched token found from the top of the file
3133
- Added Generic.PHP.DisallowRequestSuperglobal to ban the use of the $_REQUEST superglobal
3234
-- Thanks to Morerice for the contribution
35+
- Squiz.PHP.InnerFunctions now handles multiple nested anon classes correctly
3336
- Fixed bug #2688 : Case statements not tokenized correctly when switch is contained within ternary
3437
- Fixed bug #2698 : PHPCS throws errors determining auto report width when shell_exec is disabled
3538
-- Thanks to Matthew Peveler for the patch

src/Files/File.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2490,10 +2490,14 @@ public function hasCondition($stackPtr, $types)
24902490
*
24912491
* @param int $stackPtr The position of the token we are checking.
24922492
* @param int|string $type The type of token to search for.
2493+
* @param bool $first If TRUE, will return the matched condition
2494+
* furtherest away from the passed token.
2495+
* If FALSE, will return the matched condition
2496+
* closest to the passed token.
24932497
*
24942498
* @return int|false
24952499
*/
2496-
public function getCondition($stackPtr, $type)
2500+
public function getCondition($stackPtr, $type, $first=true)
24972501
{
24982502
// Check for the existence of the token.
24992503
if (isset($this->tokens[$stackPtr]) === false) {
@@ -2506,6 +2510,10 @@ public function getCondition($stackPtr, $type)
25062510
}
25072511

25082512
$conditions = $this->tokens[$stackPtr]['conditions'];
2513+
if ($first === false) {
2514+
$conditions = array_reverse($conditions, true);
2515+
}
2516+
25092517
foreach ($conditions as $token => $condition) {
25102518
if ($condition === $type) {
25112519
return $token;

src/Standards/Squiz/Sniffs/PHP/InnerFunctionsSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function process(File $phpcsFile, $stackPtr)
4747
return;
4848
}
4949

50-
$class = $phpcsFile->getCondition($stackPtr, T_ANON_CLASS);
50+
$class = $phpcsFile->getCondition($stackPtr, T_ANON_CLASS, false);
5151
if ($class !== false && $class > $function) {
5252
// Ignore methods in anon classes.
5353
return;

src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.inc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ function test()
2727
}
2828
};
2929
}
30+
31+
new class {
32+
public function valueObject(): object
33+
{
34+
return new class {
35+
public function string(): string {
36+
return 'string';
37+
}
38+
};
39+
}
40+
};
41+
42+
new class {
43+
public function outer()
44+
{
45+
if (!function_exists('inner')) {
46+
function inner() {
47+
}
48+
}
49+
}
50+
};

src/Standards/Squiz/Tests/PHP/InnerFunctionsUnitTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class InnerFunctionsUnitTest extends AbstractSniffUnitTest
2525
*/
2626
public function getErrorList()
2727
{
28-
return [5 => 1];
28+
return [
29+
5 => 1,
30+
46 => 1,
31+
];
2932

3033
}//end getErrorList()
3134

0 commit comments

Comments
 (0)