Skip to content

Commit

Permalink
SlevomatCodingStandard.TypeHints.DeclareStrictTypes: Fixing number of…
Browse files Browse the repository at this point in the history
… empty lines when previous effective token before declare is line comment
  • Loading branch information
maryo authored and kukulich committed Feb 1, 2025
1 parent 855cd60 commit 9e6b426
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\CommentHelper;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
Expand All @@ -12,6 +13,7 @@
use function strlen;
use function substr;
use function substr_count;
use const T_COMMENT;
use const T_DECLARE;
use const T_LNUMBER;
use const T_OPEN_TAG;
Expand Down Expand Up @@ -180,7 +182,22 @@ public function process(File $phpcsFile, $openTagPointer): void
}
} else {
$declareOnFirstLine = $tokens[$declarePointer]['line'] === $tokens[$openTagPointer]['line'];
$linesCountBefore = $declareOnFirstLine ? 0 : substr_count($whitespaceBefore, $phpcsFile->eolChar) - 1;
$whitespaceLinesBeforeDeclare = $this->linesCountBeforeDeclare;
$linesCountBefore = 0;

if (!$declareOnFirstLine) {
$linesCountBefore = substr_count($whitespaceBefore, $phpcsFile->eolChar);

if (
$tokens[$pointerBeforeDeclare]['code'] === T_COMMENT
&& CommentHelper::isLineComment($phpcsFile, $pointerBeforeDeclare)
) {
$whitespaceLinesBeforeDeclare--;
} else {
$linesCountBefore--;
}
}

if ($declareOnFirstLine || $linesCountBefore !== $this->linesCountBeforeDeclare) {
$fix = $phpcsFile->addFixableError(
sprintf(
Expand All @@ -201,7 +218,7 @@ public function process(File $phpcsFile, $openTagPointer): void

FixerHelper::removeBetween($phpcsFile, $pointerBeforeDeclare, $declarePointer);

for ($i = 0; $i <= $this->linesCountBeforeDeclare; $i++) {
for ($i = 0; $i <= $whitespaceLinesBeforeDeclare; $i++) {
$phpcsFile->fixer->addNewline($pointerBeforeDeclare);
}
$phpcsFile->fixer->endChangeset();
Expand Down
16 changes: 16 additions & 0 deletions tests/Sniffs/TypeHints/DeclareStrictTypesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public function testDeclareStrictWithFileCommentAbove(): void
self::assertNoSniffErrorInFile($report);
}

public function testDeclareStrictWithLineCommentAbove(): void
{
$report = self::checkFile(__DIR__ . '/data/declareStrictTypesWithLineCommentAbove.php', [
'linesCountBeforeDeclare' => 1,
]);
self::assertNoSniffErrorInFile($report);
}

public function testDeclareStrictWithTicks(): void
{
$report = self::checkFile(__DIR__ . '/data/declareStrictTypesWithTicks.php', [
Expand Down Expand Up @@ -233,6 +241,14 @@ public function testFixableCommentBefore(): void
self::assertAllFixedInFile($report);
}

public function testFixableLineCommentBefore(): void
{
$report = self::checkFile(__DIR__ . '/data/fixableDeclareStrictTypesLineCommentBefore.php', [
'linesCountBeforeDeclare' => 1,
], [DeclareStrictTypesSniff::CODE_INCORRECT_WHITESPACE_BEFORE_DECLARE]);
self::assertAllFixedInFile($report);
}

public function testFixableMissingIncorrectFormatOneSpace(): void
{
$report = self::checkFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// Comment across the entire line, it does not contain any whitespace token.

declare(strict_types = 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

//

declare(strict_types = 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

//
declare(strict_types = 1);

0 comments on commit 9e6b426

Please sign in to comment.