Skip to content

Commit 3c2b121

Browse files
committed
Use findEndOfStatement + confirm comparisions inside array values are also banned
1 parent bf642b2 commit 3c2b121

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,7 @@ public function process(File $phpcsFile, $stackPtr)
8282
return;
8383
}
8484

85-
$endStatement = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
86-
if ($tokens[$stackPtr]['conditions'] !== $tokens[$endStatement]['conditions']) {
87-
// This statement doesn't end with a semicolon, which is the case for
88-
// the last expression in a for loop.
89-
return;
90-
}
91-
85+
$endStatement = $phpcsFile->findEndOfStatement($stackPtr);
9286
for ($i = ($stackPtr + 1); $i < $endStatement; $i++) {
9387
if ((isset(Tokens::$comparisonTokens[$tokens[$i]['code']]) === true
9488
&& $tokens[$i]['code'] !== T_COALESCE)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ $a = $b === true ? $c : $d;
5454

5555
$this->_args = $this->_getArgs(($_SERVER['argv'] ?? []));
5656
$args = ($_SERVER['argv'] ?? []);
57+
58+
$a = [
59+
'a' => ($foo) ? $foo : $bar,
60+
];
61+
62+
$a = [
63+
'a' => ($foo) ? fn() => return 1 : fn() => return 2,
64+
];

src/Standards/Squiz/Tests/PHP/DisallowComparisonAssignmentUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function getErrorList()
3434
10 => 1,
3535
52 => 1,
3636
53 => 1,
37+
58 => 1,
38+
62 => 1,
3739
];
3840

3941
}//end getErrorList()

0 commit comments

Comments
 (0)