Skip to content

Commit 09b97cf

Browse files
committed
ControlStructureSpacingSniff: Fixed false positive
1 parent 223f02b commit 09b97cf

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
use function strlen;
2020
use function substr;
2121
use function substr_count;
22+
use const T_ANON_CLASS;
2223
use const T_BREAK;
2324
use const T_CASE;
2425
use const T_CATCH;
2526
use const T_CLOSE_CURLY_BRACKET;
27+
use const T_CLOSURE;
2628
use const T_COLON;
2729
use const T_COMMENT;
2830
use const T_CONTINUE;
@@ -361,7 +363,12 @@ private function findControlStructureEnd(File $phpcsFile, int $controlStructureP
361363
return TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $pointerAfterControlStructureEnd - 1);
362364
}
363365

364-
return (int) TokenHelper::findNext($phpcsFile, T_SEMICOLON, $controlStructurePointer + 1);
366+
$nextPointer = TokenHelper::findNext($phpcsFile, [T_SEMICOLON, T_ANON_CLASS, T_CLOSURE], $controlStructurePointer + 1);
367+
if ($tokens[$nextPointer]['code'] === T_SEMICOLON) {
368+
return $nextPointer;
369+
}
370+
371+
return (int) TokenHelper::findNext($phpcsFile, T_SEMICOLON, $tokens[$nextPointer]['scope_closer'] + 1);
365372
}
366373

367374
private function isWhilePartOfDo(File $phpcsFile, int $controlStructurePointer): bool

tests/Sniffs/ControlStructures/ControlStructureSpacingSniffTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testDefaultSettingsErrors(): void
1919
{
2020
$report = self::checkFile(__DIR__ . '/data/controlStructureSpacingWithDefaultSettingsErrors.php');
2121

22-
self::assertSame(47, $report->getErrorCount());
22+
self::assertSame(49, $report->getErrorCount());
2323

2424
self::assertSniffError($report, 4, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 1 lines before "if", found 2.');
2525
self::assertSniffError($report, 4, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE, 'Expected 1 lines after "if", found 0.');
@@ -67,6 +67,8 @@ public function testDefaultSettingsErrors(): void
6767
self::assertSniffError($report, 148, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE, 'Expected 1 lines after "if", found 0.');
6868
self::assertSniffError($report, 155, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE, 'Expected 1 lines after "if", found 2.');
6969
self::assertSniffError($report, 168, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "return", found 1.');
70+
self::assertSniffError($report, 173, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "return", found 1.');
71+
self::assertSniffError($report, 182, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE, 'Expected 0 lines after "return", found 1.');
7072

7173
self::assertAllFixedInFile($report);
7274
}

tests/Sniffs/ControlStructures/data/controlStructureSpacingWithDefaultSettingsErrors.fixed.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,27 @@ function () {
151151
// Multiline
152152
return true;
153153
};
154+
155+
function () {
156+
return function () {
157+
$a = 1;
158+
$b = 2;
159+
160+
return $a * $b;
161+
};
162+
};
163+
164+
function () {
165+
return new class implements Countable
166+
{
167+
168+
public function count()
169+
{
170+
$a = 1;
171+
$b = 2;
172+
173+
return $a * $b;
174+
}
175+
176+
};
177+
};

tests/Sniffs/ControlStructures/data/controlStructureSpacingWithDefaultSettingsErrors.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,29 @@ function () {
167167
// Multiline
168168
return true;
169169
};
170+
171+
function () {
172+
173+
return function () {
174+
$a = 1;
175+
$b = 2;
176+
177+
return $a * $b;
178+
};
179+
};
180+
181+
function () {
182+
return new class implements Countable
183+
{
184+
185+
public function count()
186+
{
187+
$a = 1;
188+
$b = 2;
189+
190+
return $a * $b;
191+
}
192+
193+
};
194+
195+
};

tests/Sniffs/ControlStructures/data/controlStructureSpacingWithDefaultSettingsNoErrors.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ function () {
111111
return true;
112112
};
113113

114+
function () {
115+
return function () {
116+
$a = 1;
117+
$b = 2;
118+
119+
return $a * $b;
120+
};
121+
};
122+
123+
function () {
124+
return new class implements Countable
125+
{
126+
127+
public function count()
128+
{
129+
$a = 1;
130+
$b = 2;
131+
132+
return $a * $b;
133+
}
134+
135+
};
136+
};
137+
114138
if (true) {
115139

116140
}

0 commit comments

Comments
 (0)