diff --git a/src/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php b/src/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php index e52b00d722..01c2818ec0 100644 --- a/src/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php @@ -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 @@ -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; } } diff --git a/src/Standards/Squiz/Tests/PHP/DisallowMultipleAssignmentsUnitTest.inc b/src/Standards/Squiz/Tests/PHP/DisallowMultipleAssignmentsUnitTest.inc index f38806bf86..f284b44878 100644 --- a/src/Standards/Squiz/Tests/PHP/DisallowMultipleAssignmentsUnitTest.inc +++ b/src/Standards/Squiz/Tests/PHP/DisallowMultipleAssignmentsUnitTest.inc @@ -56,3 +56,9 @@ $obj->$classVar = $prefix.'-'.$type; $closureWithDefaultParamter = function(array $testArray=array()) {}; ?> + +