Skip to content

Commit 53b72cb

Browse files
committed
Merge branch 'disallow-multiple-assignments' of https://github.com/kukulich/PHP_CodeSniffer
2 parents 2c41990 + 557756c commit 53b72cb

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr)
4343
$tokens = $phpcsFile->getTokens();
4444

4545
// Ignore default value assignments in function definitions.
46-
$function = $phpcsFile->findPrevious([T_FUNCTION, T_CLOSURE], ($stackPtr - 1), null, false, null, true);
46+
$function = $phpcsFile->findPrevious([T_FUNCTION, T_CLOSURE, T_FN], ($stackPtr - 1), null, false, null, true);
4747
if ($function !== false) {
4848
$opener = $tokens[$function]['parenthesis_opener'];
4949
$closer = $tokens[$function]['parenthesis_closer'];
@@ -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;
@@ -146,6 +146,7 @@ public function process(File $phpcsFile, $stackPtr)
146146

147147
if ($tokens[$varToken]['code'] === T_VARIABLE
148148
|| $tokens[$varToken]['code'] === T_OPEN_TAG
149+
|| $tokens[$varToken]['code'] === T_GOTO_LABEL
149150
|| $tokens[$varToken]['code'] === T_INLINE_THEN
150151
|| $tokens[$varToken]['code'] === T_INLINE_ELSE
151152
|| $tokens[$varToken]['code'] === T_SEMICOLON

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,24 @@ $array = [
8787
false => 0
8888
},
8989
];
90+
91+
$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+
};
101+
102+
103+
function () {
104+
$a = false;
105+
106+
some_label:
107+
108+
$b = getB();
109+
};
110+

0 commit comments

Comments
 (0)