Skip to content

Commit 0858034

Browse files
committed
DeclareStrictTypesSniff: Some errors were not reported
1 parent c46fe7e commit 0858034

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/DeclareStrictTypesSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ public function process(File $phpcsFile, $openTagPointer): void
180180
}
181181
}
182182
} else {
183-
$newLinesCountBefore = substr_count($whitespaceBefore, $phpcsFile->eolChar);
184-
$linesCountBefore = $newLinesCountBefore > 0 ? $newLinesCountBefore - 1 : 0;
185-
if ($linesCountBefore !== $requiredLinesCountBeforeDeclare) {
183+
$declareOnFirstLine = $tokens[$declarePointer]['line'] === $tokens[$openTagPointer]['line'];
184+
$linesCountBefore = $declareOnFirstLine ? 0 : substr_count($whitespaceBefore, $phpcsFile->eolChar) - 1;
185+
if ($declareOnFirstLine || $linesCountBefore !== $requiredLinesCountBeforeDeclare) {
186186
$fix = $phpcsFile->addFixableError(
187187
sprintf(
188188
'Expected %d line%s before declare statement, found %d.',

tests/Sniffs/TypeHints/DeclareStrictTypesSniffTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ public function testFixableOneNewLineBefore(): void
196196
self::assertAllFixedInFile($report);
197197
}
198198

199+
public function testFixableOneNewLineBeforeWithDeclareOnFirstLine(): void
200+
{
201+
$report = self::checkFile(__DIR__ . '/data/fixableDeclareStrictTypesOneNewLineBeforeWithDeclareOnFirstLine.php', [
202+
'linesCountBeforeDeclare' => 0,
203+
], [DeclareStrictTypesSniff::CODE_DECLARE_STRICT_TYPES_MISSING, DeclareStrictTypesSniff::CODE_INCORRECT_WHITESPACE_BEFORE_DECLARE]);
204+
self::assertAllFixedInFile($report);
205+
}
206+
199207
public function testFixableMissingOneNewLine(): void
200208
{
201209
$report = self::checkFile(__DIR__ . '/data/fixableDeclareStrictTypesMissingOneNewLine.php', [
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
class Foo
5+
{
6+
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php declare(strict_types = 1);
2+
3+
class Foo
4+
{
5+
6+
}

0 commit comments

Comments
 (0)