Skip to content

Commit c19a0f5

Browse files
committed
Squiz.PHP.DisallowMultipleAssignmentsSniff: Fixed false positive when assigment is on first line in closure
1 parent 9ca9a54 commit c19a0f5

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Diff for: src/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public function process(File $phpcsFile, $stackPtr)
8383
*/
8484

8585
for ($varToken = ($stackPtr - 1); $varToken >= 0; $varToken--) {
86+
if (in_array($tokens[$varToken]['code'], [T_SEMICOLON, T_OPEN_CURLY_BRACKET], true) === true) {
87+
// We've reached the next statement, so we
88+
// didn't find a variable.
89+
return;
90+
}
91+
8692
// Skip brackets.
8793
if (isset($tokens[$varToken]['parenthesis_opener']) === true && $tokens[$varToken]['parenthesis_opener'] < $varToken) {
8894
$varToken = $tokens[$varToken]['parenthesis_opener'];
@@ -94,12 +100,6 @@ public function process(File $phpcsFile, $stackPtr)
94100
continue;
95101
}
96102

97-
if ($tokens[$varToken]['code'] === T_SEMICOLON) {
98-
// We've reached the next statement, so we
99-
// didn't find a variable.
100-
return;
101-
}
102-
103103
if ($tokens[$varToken]['code'] === T_VARIABLE) {
104104
// We found our variable.
105105
break;

Diff for: src/Standards/Squiz/Tests/PHP/DisallowMultipleAssignmentsUnitTest.inc

+9
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,12 @@ $array = [
8989
];
9090

9191
$arrow_function = fn ($a = null) => $a;
92+
93+
function ($html) {
94+
$regEx = '/regexp/';
95+
96+
return preg_replace_callback($regEx, function ($matches) {
97+
[$all] = $matches;
98+
return $all;
99+
}, $html);
100+
};

0 commit comments

Comments
 (0)