Skip to content

Commit 2bea753

Browse files
committed
Merge branch 'feature/3460-generic-multiplestatement-closure-param-bug' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 25d87b7 + 1f39246 commit 2bea753

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

src/Standards/Generic/Sniffs/Formatting/MultipleStatementAlignmentSniff.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,6 @@ public function register()
8080
*/
8181
public function process(File $phpcsFile, $stackPtr)
8282
{
83-
$tokens = $phpcsFile->getTokens();
84-
85-
// Ignore assignments used in a condition, like an IF or FOR.
86-
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
87-
// If the parenthesis is on the same line as the assignment,
88-
// then it should be ignored as it is specifically being grouped.
89-
$parens = $tokens[$stackPtr]['nested_parenthesis'];
90-
$lastParen = array_pop($parens);
91-
if ($tokens[$lastParen]['line'] === $tokens[$stackPtr]['line']) {
92-
return;
93-
}
94-
95-
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $start => $end) {
96-
if (isset($tokens[$start]['parenthesis_owner']) === true) {
97-
return;
98-
}
99-
}
100-
}
101-
10283
$lastAssign = $this->checkAlignment($phpcsFile, $stackPtr);
10384
return ($lastAssign + 1);
10485

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

104+
// Ignore assignments used in a condition, like an IF or FOR or closure param defaults.
105+
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
106+
// If the parenthesis is on the same line as the assignment,
107+
// then it should be ignored as it is specifically being grouped.
108+
$parens = $tokens[$stackPtr]['nested_parenthesis'];
109+
$lastParen = array_pop($parens);
110+
if ($tokens[$lastParen]['line'] === $tokens[$stackPtr]['line']) {
111+
return $stackPtr;
112+
}
113+
114+
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $start => $end) {
115+
if (isset($tokens[$start]['parenthesis_owner']) === true) {
116+
return $stackPtr;
117+
}
118+
}
119+
}
120+
123121
$assignments = [];
124122
$prevAssign = null;
125123
$lastLine = $tokens[$stackPtr]['line'];

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.inc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,27 @@ class Test
478478

479479
protected static $thisIsAReallyLongVariableName = [];
480480
}
481+
482+
// Issue #3460.
483+
function issue3460_invalid() {
484+
$a = static function ($variables = false) use ($foo) {
485+
return $variables;
486+
};
487+
$b = $a;
488+
}
489+
490+
function issue3460_valid() {
491+
$a = static function ($variables = false) use ($foo) {
492+
return $variables;
493+
};
494+
$b = $a;
495+
}
496+
497+
function makeSureThatAssignmentWithinClosureAreStillHandled() {
498+
$a = static function ($variables = []) use ($temp) {
499+
$a = 'foo';
500+
$bar = 'bar';
501+
$longer = 'longer';
502+
return $variables;
503+
};
504+
}

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.inc.fixed

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,27 @@ class Test
478478

479479
protected static $thisIsAReallyLongVariableName = [];
480480
}
481+
482+
// Issue #3460.
483+
function issue3460_invalid() {
484+
$a = static function ($variables = false) use ($foo) {
485+
return $variables;
486+
};
487+
$b = $a;
488+
}
489+
490+
function issue3460_valid() {
491+
$a = static function ($variables = false) use ($foo) {
492+
return $variables;
493+
};
494+
$b = $a;
495+
}
496+
497+
function makeSureThatAssignmentWithinClosureAreStillHandled() {
498+
$a = static function ($variables = []) use ($temp) {
499+
$a = 'foo';
500+
$bar = 'bar';
501+
$longer = 'longer';
502+
return $variables;
503+
};
504+
}

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public function getWarningList($testFile='MultipleStatementAlignmentUnitTest.inc
118118
442 => 1,
119119
443 => 1,
120120
454 => 1,
121+
487 => 1,
122+
499 => 1,
123+
500 => 1,
121124
];
122125
break;
123126
case 'MultipleStatementAlignmentUnitTest.js':

0 commit comments

Comments
 (0)