Skip to content

Commit 17731a1

Browse files
committed
Squiz/DisallowMultipleAssignments: fix sniff walking back too far when 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
1 parent 2cb08fd commit 17731a1

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ 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.
86+
if (in_array($tokens[$varToken]['code'], [T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_CLOSE_TAG], true) === true) {
87+
// We've reached the previous statement, so we didn't find a variable.
8988
return;
9089
}
9190

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,29 @@ function () {
108108
$b = getB();
109109
};
110110

111+
?>
112+
<?= $var = false; ?>
113+
114+
// Issue PHPCSStandards/PHP_CodeSniffer#537.
115+
<?php $a * $b ?>
116+
<?php
117+
list ($c, $d) = explode(',', '1,2');
118+
?>
119+
<?= $a * $b ?>
120+
<?php
121+
list ($c, $d) = explode(',', '1,2');
122+
?>
123+
<?= $a * $b ?>
124+
<?= list ($c, $d) = explode(',', '1,2');
125+
?>
126+
<?php $a * $b ?>
127+
<?php
128+
[$c, $d] = explode(',', '1,2');
129+
?>
130+
<?= $a * $b ?>
131+
<?php
132+
[$c, $d] = explode(',', '1,2');
133+
?>
134+
<?= $a * $b ?>
135+
<?= [$c, $d] = explode(',', '1,2');
136+
?>

0 commit comments

Comments
 (0)