Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squiz/DisallowMultipleAssignments: fix false positive when handling parameter default values #557

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function process(File $phpcsFile, $stackPtr)
if ($function !== false) {
$opener = $tokens[$function]['parenthesis_opener'];
$closer = $tokens[$function]['parenthesis_closer'];
if ($opener < $stackPtr && $closer > $stackPtr) {
if ($closer === null || ($opener < $stackPtr && $closer > $stackPtr)) {
return;
}
Comment on lines 48 to 52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is accessing the parenthesis_closer before this condition.

Would it make sense to move the check for a missing parenthesis closer up ?

I.e.:

Suggested change
$opener = $tokens[$function]['parenthesis_opener'];
$closer = $tokens[$function]['parenthesis_closer'];
if ($opener < $stackPtr && $closer > $stackPtr) {
if ($closer === null || ($opener < $stackPtr && $closer > $stackPtr)) {
return;
}
if (isset($tokens[$function]['parenthesis_closer']) === false) {
// Live coding/parse error. Bow out.
return;
}
$opener = $tokens[$function]['parenthesis_opener'];
$closer = $tokens[$function]['parenthesis_closer'];
if ($opener < $stackPtr && $closer > $stackPtr) {
return;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (missing closing parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

function missingClosingParenthesis($a =
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,29 @@ final class DisallowMultipleAssignmentsUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the test file to process.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
4 => 1,
5 => 2,
7 => 1,
9 => 1,
12 => 1,
14 => 1,
15 => 1,
79 => 1,
85 => 1,
];
switch ($testFile) {
case 'DisallowMultipleAssignmentsUnitTest.1.inc':
return [
4 => 1,
5 => 2,
7 => 1,
9 => 1,
12 => 1,
14 => 1,
15 => 1,
79 => 1,
85 => 1,
];

default:
return [];
}

}//end getErrorList()

Expand Down