Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeclareStrictTypesSniff: Fixing number of empty lines after line comment #1693

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);