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

Generic/MultipleStatementAlignment: bug fix for closure params with defaults #3464

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,6 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Ignore assignments used in a condition, like an IF or FOR.
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
// If the parenthesis is on the same line as the assignment,
// then it should be ignored as it is specifically being grouped.
$parens = $tokens[$stackPtr]['nested_parenthesis'];
$lastParen = array_pop($parens);
if ($tokens[$lastParen]['line'] === $tokens[$stackPtr]['line']) {
return;
}

foreach ($tokens[$stackPtr]['nested_parenthesis'] as $start => $end) {
if (isset($tokens[$start]['parenthesis_owner']) === true) {
return;
}
}
}

$lastAssign = $this->checkAlignment($phpcsFile, $stackPtr);
return ($lastAssign + 1);

Expand All @@ -120,6 +101,23 @@ public function checkAlignment($phpcsFile, $stackPtr, $end=null)
{
$tokens = $phpcsFile->getTokens();

// Ignore assignments used in a condition, like an IF or FOR or closure param defaults.
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
// If the parenthesis is on the same line as the assignment,
// then it should be ignored as it is specifically being grouped.
$parens = $tokens[$stackPtr]['nested_parenthesis'];
$lastParen = array_pop($parens);
if ($tokens[$lastParen]['line'] === $tokens[$stackPtr]['line']) {
return $stackPtr;
}

foreach ($tokens[$stackPtr]['nested_parenthesis'] as $start => $end) {
if (isset($tokens[$start]['parenthesis_owner']) === true) {
return $stackPtr;
}
}
}

$assignments = [];
$prevAssign = null;
$lastLine = $tokens[$stackPtr]['line'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,27 @@ class Test

protected static $thisIsAReallyLongVariableName = [];
}

// Issue #3460.
function issue3460_invalid() {
$a = static function ($variables = false) use ($foo) {
return $variables;
};
$b = $a;
}

function issue3460_valid() {
$a = static function ($variables = false) use ($foo) {
return $variables;
};
$b = $a;
}

function makeSureThatAssignmentWithinClosureAreStillHandled() {
$a = static function ($variables = []) use ($temp) {
$a = 'foo';
$bar = 'bar';
$longer = 'longer';
return $variables;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,27 @@ class Test

protected static $thisIsAReallyLongVariableName = [];
}

// Issue #3460.
function issue3460_invalid() {
$a = static function ($variables = false) use ($foo) {
return $variables;
};
$b = $a;
}

function issue3460_valid() {
$a = static function ($variables = false) use ($foo) {
return $variables;
};
$b = $a;
}

function makeSureThatAssignmentWithinClosureAreStillHandled() {
$a = static function ($variables = []) use ($temp) {
$a = 'foo';
$bar = 'bar';
$longer = 'longer';
return $variables;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public function getWarningList($testFile='MultipleStatementAlignmentUnitTest.inc
442 => 1,
443 => 1,
454 => 1,
487 => 1,
499 => 1,
500 => 1,
];
break;
case 'MultipleStatementAlignmentUnitTest.js':
Expand Down