Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squiz.PHP.DisallowMultipleAssignmentsSniff: Fixed false positives #3293

Merged
merged 3 commits into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -43,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();

// Ignore default value assignments in function definitions.
$function = $phpcsFile->findPrevious([T_FUNCTION, T_CLOSURE], ($stackPtr - 1), null, false, null, true);
$function = $phpcsFile->findPrevious([T_FUNCTION, T_CLOSURE, T_FN], ($stackPtr - 1), null, false, null, true);
if ($function !== false) {
$opener = $tokens[$function]['parenthesis_opener'];
$closer = $tokens[$function]['parenthesis_closer'];
Expand Down Expand Up @@ -83,6 +83,12 @@ public function process(File $phpcsFile, $stackPtr)
*/

for ($varToken = ($stackPtr - 1); $varToken >= 0; $varToken--) {
if (in_array($tokens[$varToken]['code'], [T_SEMICOLON, T_OPEN_CURLY_BRACKET], true) === true) {
// We've reached the next statement, so we
// didn't find a variable.
return;
}

// Skip brackets.
if (isset($tokens[$varToken]['parenthesis_opener']) === true && $tokens[$varToken]['parenthesis_opener'] < $varToken) {
$varToken = $tokens[$varToken]['parenthesis_opener'];
Expand All @@ -94,12 +100,6 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}

if ($tokens[$varToken]['code'] === T_SEMICOLON) {
// We've reached the next statement, so we
// didn't find a variable.
return;
}

if ($tokens[$varToken]['code'] === T_VARIABLE) {
// We found our variable.
break;
Expand Down Expand Up @@ -146,6 +146,7 @@ public function process(File $phpcsFile, $stackPtr)

if ($tokens[$varToken]['code'] === T_VARIABLE
|| $tokens[$varToken]['code'] === T_OPEN_TAG
|| $tokens[$varToken]['code'] === T_GOTO_LABEL
|| $tokens[$varToken]['code'] === T_INLINE_THEN
|| $tokens[$varToken]['code'] === T_INLINE_ELSE
|| $tokens[$varToken]['code'] === T_SEMICOLON
Expand Down
Expand Up @@ -87,3 +87,24 @@ $array = [
false => 0
},
];

$arrow_function = fn ($a = null) => $a;

function ($html) {
$regEx = '/regexp/';

return preg_replace_callback($regEx, function ($matches) {
[$all] = $matches;
return $all;
}, $html);
};


function () {
$a = false;

some_label:

$b = getB();
};