Skip to content

Commit

Permalink
Merge branch 'feature/3460-generic-multiplestatement-closure-param-bu…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Dec 6, 2021
2 parents 25d87b7 + 1f39246 commit 2bea753
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
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

0 comments on commit 2bea753

Please sign in to comment.