Skip to content

Commit bb293b1

Browse files
committed
Squiz/DisallowComparisonAssignment: bug fix - ignore match structures
Fixes 3616 Note: I've changed the `findNext()` call from an exhaustive call to the end of the statement to a faster check for the first non-empty token, which I believe was the actual _intention_ of the function call. As no tests are failing, I'm fairly certain my suspicion is correct ;-)
1 parent 1359e17 commit bb293b1

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,18 @@ public function process(File $phpcsFile, $stackPtr)
5252
}
5353
}
5454

55-
// Ignore values in array definitions.
56-
$array = $phpcsFile->findNext(
57-
T_ARRAY,
55+
// Ignore values in array definitions or match structures.
56+
$nextNonEmpty = $phpcsFile->findNext(
57+
Tokens::$emptyTokens,
5858
($stackPtr + 1),
5959
null,
60-
false,
61-
null,
6260
true
6361
);
6462

65-
if ($array !== false) {
63+
if ($nextNonEmpty !== false
64+
&& ($tokens[$nextNonEmpty]['code'] === \T_ARRAY
65+
|| $tokens[$nextNonEmpty]['code'] === \T_MATCH)
66+
) {
6667
return;
6768
}
6869

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,13 @@ $callback = function ($value) {
7171
return false;
7272
}
7373
};
74+
75+
function issue3616() {
76+
$food = 'cake';
77+
78+
$returnValue = match (true) {
79+
$food === 'apple' => 'This food is an apple',
80+
$food === 'bar' => 'This food is a bar',
81+
$food === 'cake' => 'This food is a cake',
82+
};
83+
}

0 commit comments

Comments
 (0)