Skip to content

Commit 2ba5393

Browse files
committed
Fixed bug #3219 : Generic.Formatting.MultipleStatementAlignment false positive for empty anonymous classes and closures
1 parent ea8158c commit 2ba5393

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
112112
- Fixed bug #3192 : findStartOfStatement doesn't work correctly inside switch
113113
-- Thanks to Vincent Langlet for the patch
114114
- Fixed bug #3197 : Squiz.NamingConventions.ValidVariableName does not use correct error code for all member vars
115+
- Fixed bug #3219 : Generic.Formatting.MultipleStatementAlignment false positive for empty anonymous classes and closures
115116
</notes>
116117
<contents>
117118
<dir name="/">

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,25 @@ public function checkAlignment($phpcsFile, $stackPtr, $end=null)
147147
break;
148148
}
149149

150-
if (isset($scopes[$tokens[$assign]['code']]) === true
151-
&& isset($tokens[$assign]['scope_opener']) === true
150+
if (isset($tokens[$assign]['scope_opener']) === true
152151
&& $tokens[$assign]['level'] === $tokens[$stackPtr]['level']
153152
) {
154-
break;
153+
if (isset($scopes[$tokens[$assign]['code']]) === true) {
154+
// This type of scope indicates that the assignment block is over.
155+
break;
156+
}
157+
158+
// Skip over the scope block because it is seen as part of the assignment block,
159+
// but also process any assignment blocks that are inside as well.
160+
$nextAssign = $phpcsFile->findNext($find, ($assign + 1), ($tokens[$assign]['scope_closer'] - 1));
161+
if ($nextAssign !== false) {
162+
$assign = $this->checkAlignment($phpcsFile, $nextAssign);
163+
} else {
164+
$assign = $tokens[$assign]['scope_closer'];
165+
}
166+
167+
$lastCode = $assign;
168+
continue;
155169
}
156170

157171
if ($assign === $arrayEnd) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,17 @@ $varonetwothreefour = 'four';
454454
$one <<= 8;
455455
$onetwothree = 3;
456456

457+
// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 1000
458+
459+
$a = 123;
460+
$model = new class() {
461+
// empty
462+
};
463+
$resource = new class() {
464+
// empty
465+
};
466+
457467
// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEnd true
458468

459469
$one <<= 8;
460470
$onetwothree = 3;
461-
462-
// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 1000

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,17 @@ $varonetwothreefour = 'four';
454454
$one <<= 8;
455455
$onetwothree = 3;
456456

457+
// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 1000
458+
459+
$a = 123;
460+
$model = new class() {
461+
// empty
462+
};
463+
$resource = new class() {
464+
// empty
465+
};
466+
457467
// phpcs:set Generic.Formatting.MultipleStatementAlignment alignAtEnd true
458468

459469
$one <<= 8;
460470
$onetwothree = 3;
461-
462-
// phpcs:set Generic.Formatting.MultipleStatementAlignment maxPadding 1000

0 commit comments

Comments
 (0)