Skip to content

Commit cd0ad6f

Browse files
committed
JumpStatementsSpacingSniff: Improved fixer
1 parent d987eac commit cd0ad6f

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/AbstractControlStructureSpacing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected function checkLinesBefore(File $phpcsFile, int $controlStructurePointe
121121
? $tokens[$pointerBefore]['comment_opener']
122122
: CommentHelper::getMultilineCommentStartPointer($phpcsFile, $pointerBefore);
123123
/** @var int $pointerBefore */
124-
$pointerBefore = $pointerBeforeComment;
124+
$pointerBefore = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $controlStructureStartPointer - 1);
125125
}
126126
}
127127

tests/Sniffs/ControlStructures/JumpStatementsSpacingSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testDefaultSettingsErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/jumpStatementsSpacingWithDefaultSettingsErrors.php');
1919

20-
self::assertSame(28, $report->getErrorCount());
20+
self::assertSame(29, $report->getErrorCount());
2121

2222
self::assertSniffError($report, 10, JumpStatementsSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 1 lines before "break", found 0.');
2323
self::assertSniffError($report, 10, JumpStatementsSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE, 'Expected 0 lines after "break", found 1.');
@@ -47,6 +47,7 @@ public function testDefaultSettingsErrors(): void
4747
self::assertSniffError($report, 127, JumpStatementsSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 1 lines before "return", found 0.');
4848
self::assertSniffError($report, 127, JumpStatementsSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE, 'Expected 1 lines after "return", found 0.');
4949
self::assertSniffError($report, 128, JumpStatementsSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 1 lines before "yield", found 0.');
50+
self::assertSniffError($report, 139, JumpStatementsSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 1 lines before "return", found 2.');
5051

5152
self::assertAllFixedInFile($report);
5253
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,13 @@ function () {
116116

117117
yield 3;
118118
};
119+
120+
function () {
121+
$foo = 1;
122+
$bar = 2;
123+
124+
// first comment
125+
126+
// second comment
127+
return $foo + $bar;
128+
};

tests/Sniffs/ControlStructures/data/jumpStatementsSpacingWithDefaultSettingsErrors.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,14 @@ function () {
127127
return 2;
128128
yield 3;
129129
};
130+
131+
function () {
132+
$foo = 1;
133+
$bar = 2;
134+
135+
// first comment
136+
137+
138+
// second comment
139+
return $foo + $bar;
140+
};

tests/Sniffs/ControlStructures/data/jumpStatementsSpacingWithDefaultSettingsNoErrors.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,13 @@ function ($array) {
168168
}
169169
}
170170
};
171+
172+
function () {
173+
$foo = 1;
174+
$bar = 2;
175+
176+
// first comment
177+
178+
// second comment
179+
return $foo + $bar;
180+
};

0 commit comments

Comments
 (0)