Skip to content

Commit

Permalink
Fix TypeError via JumpStatementsSpacingSniff when return is on the fi…
Browse files Browse the repository at this point in the history
…rst line of the file
  • Loading branch information
herndlm authored and kukulich committed Jul 12, 2023
1 parent 723c330 commit 4446ad2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ protected function checkLinesBefore(File $phpcsFile, int $controlStructurePointe
$phpcsFile->fixer->replaceToken($pointerBefore, '<?php');
}

FixerHelper::removeBetweenIncluding($phpcsFile, $pointerBefore + 1, $endOfLineBeforePointer);
if ($endOfLineBeforePointer !== null) {
FixerHelper::removeBetweenIncluding($phpcsFile, $pointerBefore + 1, $endOfLineBeforePointer);
}

$linesToAdd = $hasCommentWithLineEndBefore ? $requiredLinesCountBefore - 1 : $requiredLinesCountBefore;
for ($i = 0; $i <= $linesToAdd; $i++) {
Expand Down
16 changes: 16 additions & 0 deletions tests/Sniffs/ControlStructures/JumpStatementsSpacingSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,22 @@ public function testModifiedSettingsErrors(): void
self::assertAllFixedInFile($report);
}

public function testAtTheBeginnningOfFile(): void
{
$report = self::checkFile(__DIR__ . '/data/jumpStatementsSpacingAtTheBeginningOfFile.php');

self::assertSame(1, $report->getErrorCount());

self::assertSniffError(
$report,
1,
JumpStatementsSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE,
'Expected 1 line before "return", found -1.'
);

self::assertAllFixedInFile($report);
}

public function testAtTheEndOfFile(): void
{
$report = self::checkFile(__DIR__ . '/data/jumpStatementsSpacingAtTheEndOfFile.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return true;

0 comments on commit 4446ad2

Please sign in to comment.