Skip to content

Commit 5932287

Browse files
authored
Merge pull request #557 from rodrigoprimo/fix-disallow-multiple-assignments-false-positive
Squiz/DisallowMultipleAssignments: fix false positive when handling parameter default values
2 parents 131886e + ed00c9a commit 5932287

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public function process(File $phpcsFile, $stackPtr)
4545
// Ignore default value assignments in function definitions.
4646
$function = $phpcsFile->findPrevious([T_FUNCTION, T_CLOSURE, T_FN], ($stackPtr - 1), null, false, null, true);
4747
if ($function !== false) {
48+
if (isset($tokens[$function]['parenthesis_closer']) === false) {
49+
// Live coding/parse error. Bow out.
50+
return;
51+
}
52+
4853
$opener = $tokens[$function]['parenthesis_opener'];
4954
$closer = $tokens[$function]['parenthesis_closer'];
5055
if ($opener < $stackPtr && $closer > $stackPtr) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing closing parenthesis).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
function missingClosingParenthesis($a =

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,29 @@ final class DisallowMultipleAssignmentsUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the test file to process.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
return [
34-
4 => 1,
35-
5 => 2,
36-
7 => 1,
37-
9 => 1,
38-
12 => 1,
39-
14 => 1,
40-
15 => 1,
41-
79 => 1,
42-
85 => 1,
43-
];
35+
switch ($testFile) {
36+
case 'DisallowMultipleAssignmentsUnitTest.1.inc':
37+
return [
38+
4 => 1,
39+
5 => 2,
40+
7 => 1,
41+
9 => 1,
42+
12 => 1,
43+
14 => 1,
44+
15 => 1,
45+
79 => 1,
46+
85 => 1,
47+
];
48+
49+
default:
50+
return [];
51+
}
4452

4553
}//end getErrorList()
4654

0 commit comments

Comments
 (0)