Skip to content

Squiz.PHP.DisallowMultipleAssignments false positive in while loop conditions #2121

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public function process(File $phpcsFile, $stackPtr)
}
}

// Ignore assignments in WHILE loop conditions.
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
$nested = $tokens[$stackPtr]['nested_parenthesis'];
foreach ($nested as $opener => $closer) {
if (isset($tokens[$opener]['parenthesis_owner']) === true
&& $tokens[$tokens[$opener]['parenthesis_owner']]['code'] === T_WHILE
) {
return;
}
}
}

/*
The general rule is:
Find an equal sign and go backwards along the line. If you hit an
Expand Down Expand Up @@ -122,10 +134,10 @@ public function process(File $phpcsFile, $stackPtr)

// Ignore the first part of FOR loops as we are allowed to
// assign variables there even though the variable is not the
// first thing on the line. Also ignore WHILE loops.
// first thing on the line.
if ($tokens[$varToken]['code'] === T_OPEN_PARENTHESIS && isset($tokens[$varToken]['parenthesis_owner']) === true) {
$owner = $tokens[$varToken]['parenthesis_owner'];
if ($tokens[$owner]['code'] === T_FOR || $tokens[$owner]['code'] === T_WHILE) {
if ($tokens[$owner]['code'] === T_FOR) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ $obj->$classVar = $prefix.'-'.$type;
$closureWithDefaultParamter = function(array $testArray=array()) {};
?>
<?php $var = false; ?>

<?php

while ( ( $csvdata = fgetcsv( $handle, 2000, $separator ) ) !== false ) {
// Do something.
}