Skip to content

Commit

Permalink
Squiz/DisallowMultipleAssignments: fix sniff walking back too far whe…
Browse files Browse the repository at this point in the history
…n going in/out of PHP

The sniff did not recognize that when it would encounter a PHP close tag, it had reached a previous statement, leading to false positives for list assignments.

Fixed now. Includes tests.

Fixes 537
  • Loading branch information
jrfnl committed Jul 13, 2024
1 parent 27678e7 commit bf3d6f5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ 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.
if (in_array($tokens[$varToken]['code'], [T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_CLOSE_TAG], true) === true) {
// We've reached the previous statement, so we didn't find a variable.
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,29 @@ function () {
$b = getB();
};

?>
<?= $var = false; ?>

// Issue PHPCSStandards/PHP_CodeSniffer#537.
<?php $a * $b ?>
<?php
list ($c, $d) = explode(',', '1,2');
?>
<?= $a * $b ?>
<?php
list ($c, $d) = explode(',', '1,2');
?>
<?= $a * $b ?>
<?= list ($c, $d) = explode(',', '1,2');
?>
<?php $a * $b ?>
<?php
[$c, $d] = explode(',', '1,2');
?>
<?= $a * $b ?>
<?php
[$c, $d] = explode(',', '1,2');
?>
<?= $a * $b ?>
<?= [$c, $d] = explode(',', '1,2');
?>

0 comments on commit bf3d6f5

Please sign in to comment.