Skip to content

Commit 4446ad2

Browse files
herndlmkukulich
authored andcommitted
Fix TypeError via JumpStatementsSpacingSniff when return is on the first line of the file
1 parent 723c330 commit 4446ad2

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/AbstractControlStructureSpacing.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ protected function checkLinesBefore(File $phpcsFile, int $controlStructurePointe
217217
$phpcsFile->fixer->replaceToken($pointerBefore, '<?php');
218218
}
219219

220-
FixerHelper::removeBetweenIncluding($phpcsFile, $pointerBefore + 1, $endOfLineBeforePointer);
220+
if ($endOfLineBeforePointer !== null) {
221+
FixerHelper::removeBetweenIncluding($phpcsFile, $pointerBefore + 1, $endOfLineBeforePointer);
222+
}
221223

222224
$linesToAdd = $hasCommentWithLineEndBefore ? $requiredLinesCountBefore - 1 : $requiredLinesCountBefore;
223225
for ($i = 0; $i <= $linesToAdd; $i++) {

tests/Sniffs/ControlStructures/JumpStatementsSpacingSniffTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,22 @@ public function testModifiedSettingsErrors(): void
292292
self::assertAllFixedInFile($report);
293293
}
294294

295+
public function testAtTheBeginnningOfFile(): void
296+
{
297+
$report = self::checkFile(__DIR__ . '/data/jumpStatementsSpacingAtTheBeginningOfFile.php');
298+
299+
self::assertSame(1, $report->getErrorCount());
300+
301+
self::assertSniffError(
302+
$report,
303+
1,
304+
JumpStatementsSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE,
305+
'Expected 1 line before "return", found -1.'
306+
);
307+
308+
self::assertAllFixedInFile($report);
309+
}
310+
295311
public function testAtTheEndOfFile(): void
296312
{
297313
$report = self::checkFile(__DIR__ . '/data/jumpStatementsSpacingAtTheEndOfFile.php');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return true;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return true;

0 commit comments

Comments
 (0)