Skip to content

Commit 06a7e06

Browse files
committed
ControlStructureSpacingSniff: Fixed false positive
1 parent e7875e3 commit 06a7e06

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use const T_GOTO;
4040
use const T_IF;
4141
use const T_OPEN_CURLY_BRACKET;
42+
use const T_OPEN_SHORT_ARRAY;
4243
use const T_OPEN_TAG;
4344
use const T_RETURN;
4445
use const T_SEMICOLON;
@@ -363,11 +364,15 @@ private function findControlStructureEnd(File $phpcsFile, int $controlStructureP
363364
return TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $pointerAfterControlStructureEnd - 1);
364365
}
365366

366-
$nextPointer = TokenHelper::findNext($phpcsFile, [T_SEMICOLON, T_ANON_CLASS, T_CLOSURE], $controlStructurePointer + 1);
367+
$nextPointer = TokenHelper::findNext($phpcsFile, [T_SEMICOLON, T_ANON_CLASS, T_CLOSURE, T_OPEN_SHORT_ARRAY], $controlStructurePointer + 1);
367368
if ($tokens[$nextPointer]['code'] === T_SEMICOLON) {
368369
return $nextPointer;
369370
}
370371

372+
if ($tokens[$nextPointer]['code'] === T_OPEN_SHORT_ARRAY) {
373+
return (int) TokenHelper::findNext($phpcsFile, T_SEMICOLON, $tokens[$nextPointer]['bracket_closer'] + 1);
374+
}
375+
371376
return (int) TokenHelper::findNext($phpcsFile, T_SEMICOLON, $tokens[$nextPointer]['scope_closer'] + 1);
372377
}
373378

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(49, $report->getErrorCount());
22+
self::assertSame(51, $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.');
@@ -69,6 +69,8 @@ public function testDefaultSettingsErrors(): void
6969
self::assertSniffError($report, 168, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "return", found 1.');
7070
self::assertSniffError($report, 173, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "return", found 1.');
7171
self::assertSniffError($report, 182, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE, 'Expected 0 lines after "return", found 1.');
72+
self::assertSniffError($report, 199, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "return", found 1.');
73+
self::assertSniffError($report, 199, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE, 'Expected 0 lines after "return", found 1.');
7274

7375
self::assertAllFixedInFile($report);
7476
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,20 @@ public function count()
175175

176176
};
177177
};
178+
179+
function () {
180+
return [
181+
function () {
182+
$a = 1;
183+
$b = 2;
184+
185+
return $a * $b;
186+
},
187+
function () {
188+
$a = 1;
189+
$b = 2;
190+
191+
return $a * $b;
192+
},
193+
];
194+
};

tests/Sniffs/ControlStructures/data/controlStructureSpacingWithDefaultSettingsErrors.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,22 @@ public function count()
193193
};
194194

195195
};
196+
197+
function () {
198+
199+
return [
200+
function () {
201+
$a = 1;
202+
$b = 2;
203+
204+
return $a * $b;
205+
},
206+
function () {
207+
$a = 1;
208+
$b = 2;
209+
210+
return $a * $b;
211+
},
212+
];
213+
214+
};

tests/Sniffs/ControlStructures/data/controlStructureSpacingWithDefaultSettingsNoErrors.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ public function count()
135135
};
136136
};
137137

138+
function () {
139+
return [
140+
function () {
141+
$a = 1;
142+
$b = 2;
143+
144+
return $a * $b;
145+
},
146+
function () {
147+
$a = 1;
148+
$b = 2;
149+
150+
return $a * $b;
151+
},
152+
];
153+
};
154+
138155
if (true) {
139156

140157
}

0 commit comments

Comments
 (0)