From 716c6a41c0f49cc9b380ad1019db8fd86683239b Mon Sep 17 00:00:00 2001 From: Marek Date: Tue, 20 Aug 2024 00:46:35 +0200 Subject: [PATCH] DeclareStrictTypesSniff: Fixing number of empty lines when previous effective token before declare is line comment --- .../TypeHints/DeclareStrictTypesSniff.php | 21 +++++++++++++++++-- .../TypeHints/DeclareStrictTypesSniffTest.php | 16 ++++++++++++++ ...declareStrictTypesWithLineCommentAbove.php | 5 +++++ ...lareStrictTypesLineCommentBefore.fixed.php | 5 +++++ ...bleDeclareStrictTypesLineCommentBefore.php | 4 ++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/Sniffs/TypeHints/data/declareStrictTypesWithLineCommentAbove.php create mode 100644 tests/Sniffs/TypeHints/data/fixableDeclareStrictTypesLineCommentBefore.fixed.php create mode 100644 tests/Sniffs/TypeHints/data/fixableDeclareStrictTypesLineCommentBefore.php diff --git a/SlevomatCodingStandard/Sniffs/TypeHints/DeclareStrictTypesSniff.php b/SlevomatCodingStandard/Sniffs/TypeHints/DeclareStrictTypesSniff.php index 998798182..ac07e4b5a 100644 --- a/SlevomatCodingStandard/Sniffs/TypeHints/DeclareStrictTypesSniff.php +++ b/SlevomatCodingStandard/Sniffs/TypeHints/DeclareStrictTypesSniff.php @@ -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; @@ -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; @@ -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( @@ -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(); diff --git a/tests/Sniffs/TypeHints/DeclareStrictTypesSniffTest.php b/tests/Sniffs/TypeHints/DeclareStrictTypesSniffTest.php index de8a728da..3e5aa1498 100644 --- a/tests/Sniffs/TypeHints/DeclareStrictTypesSniffTest.php +++ b/tests/Sniffs/TypeHints/DeclareStrictTypesSniffTest.php @@ -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', [ @@ -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( diff --git a/tests/Sniffs/TypeHints/data/declareStrictTypesWithLineCommentAbove.php b/tests/Sniffs/TypeHints/data/declareStrictTypesWithLineCommentAbove.php new file mode 100644 index 000000000..ba17a7278 --- /dev/null +++ b/tests/Sniffs/TypeHints/data/declareStrictTypesWithLineCommentAbove.php @@ -0,0 +1,5 @@ +