Skip to content

Commit d4aa673

Browse files
maryokukulich
authored andcommitted
SlevomatCodingStandard.TypeHints.DeclareStrictTypes: Fixing number of empty lines when previous effective token before declare is line comment
1 parent 855cd60 commit d4aa673

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/DeclareStrictTypesSniff.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\CommentHelper;
78
use SlevomatCodingStandard\Helpers\FixerHelper;
89
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
910
use SlevomatCodingStandard\Helpers\TokenHelper;
@@ -12,6 +13,7 @@
1213
use function strlen;
1314
use function substr;
1415
use function substr_count;
16+
use const T_COMMENT;
1517
use const T_DECLARE;
1618
use const T_LNUMBER;
1719
use const T_OPEN_TAG;
@@ -180,7 +182,22 @@ public function process(File $phpcsFile, $openTagPointer): void
180182
}
181183
} else {
182184
$declareOnFirstLine = $tokens[$declarePointer]['line'] === $tokens[$openTagPointer]['line'];
183-
$linesCountBefore = $declareOnFirstLine ? 0 : substr_count($whitespaceBefore, $phpcsFile->eolChar) - 1;
185+
$whitespaceLinesBeforeDeclare = $this->linesCountBeforeDeclare;
186+
$linesCountBefore = 0;
187+
188+
if (!$declareOnFirstLine) {
189+
$linesCountBefore = substr_count($whitespaceBefore, $phpcsFile->eolChar);
190+
191+
if (
192+
$tokens[$pointerBeforeDeclare]['code'] === T_COMMENT
193+
&& CommentHelper::isLineComment($phpcsFile, $pointerBeforeDeclare)
194+
) {
195+
$whitespaceLinesBeforeDeclare--;
196+
} else {
197+
$linesCountBefore--;
198+
}
199+
}
200+
184201
if ($declareOnFirstLine || $linesCountBefore !== $this->linesCountBeforeDeclare) {
185202
$fix = $phpcsFile->addFixableError(
186203
sprintf(
@@ -201,7 +218,7 @@ public function process(File $phpcsFile, $openTagPointer): void
201218

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

204-
for ($i = 0; $i <= $this->linesCountBeforeDeclare; $i++) {
221+
for ($i = 0; $i <= $whitespaceLinesBeforeDeclare; $i++) {
205222
$phpcsFile->fixer->addNewline($pointerBeforeDeclare);
206223
}
207224
$phpcsFile->fixer->endChangeset();

tests/Sniffs/TypeHints/DeclareStrictTypesSniffTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ public function testDeclareStrictWithFileCommentAbove(): void
161161
self::assertNoSniffErrorInFile($report);
162162
}
163163

164+
public function testDeclareStrictWithLineCommentAbove(): void
165+
{
166+
$report = self::checkFile(__DIR__ . '/data/declareStrictTypesWithLineCommentAbove.php', [
167+
'linesCountBeforeDeclare' => 1,
168+
]);
169+
self::assertNoSniffErrorInFile($report);
170+
}
171+
164172
public function testDeclareStrictWithTicks(): void
165173
{
166174
$report = self::checkFile(__DIR__ . '/data/declareStrictTypesWithTicks.php', [
@@ -233,6 +241,14 @@ public function testFixableCommentBefore(): void
233241
self::assertAllFixedInFile($report);
234242
}
235243

244+
public function testFixableLineCommentBefore(): void
245+
{
246+
$report = self::checkFile(__DIR__ . '/data/fixableDeclareStrictTypesLineCommentBefore.php', [
247+
'linesCountBeforeDeclare' => 1,
248+
], [DeclareStrictTypesSniff::CODE_INCORRECT_WHITESPACE_BEFORE_DECLARE]);
249+
self::assertAllFixedInFile($report);
250+
}
251+
236252
public function testFixableMissingIncorrectFormatOneSpace(): void
237253
{
238254
$report = self::checkFile(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// Comment across the entire line, it does not contain any whitespace token.
4+
5+
declare(strict_types = 1);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
//
4+
5+
declare(strict_types = 1);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
//
4+
declare(strict_types = 1);

0 commit comments

Comments
 (0)