Skip to content

Commit

Permalink
DeclareStrictTypesSniff: Some errors were not reported
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Apr 9, 2021
1 parent c46fe7e commit 0858034
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public function process(File $phpcsFile, $openTagPointer): void
}
}
} else {
$newLinesCountBefore = substr_count($whitespaceBefore, $phpcsFile->eolChar);
$linesCountBefore = $newLinesCountBefore > 0 ? $newLinesCountBefore - 1 : 0;
if ($linesCountBefore !== $requiredLinesCountBeforeDeclare) {
$declareOnFirstLine = $tokens[$declarePointer]['line'] === $tokens[$openTagPointer]['line'];
$linesCountBefore = $declareOnFirstLine ? 0 : substr_count($whitespaceBefore, $phpcsFile->eolChar) - 1;
if ($declareOnFirstLine || $linesCountBefore !== $requiredLinesCountBeforeDeclare) {
$fix = $phpcsFile->addFixableError(
sprintf(
'Expected %d line%s before declare statement, found %d.',
Expand Down
8 changes: 8 additions & 0 deletions tests/Sniffs/TypeHints/DeclareStrictTypesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ public function testFixableOneNewLineBefore(): void
self::assertAllFixedInFile($report);
}

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

public function testFixableMissingOneNewLine(): void
{
$report = self::checkFile(__DIR__ . '/data/fixableDeclareStrictTypesMissingOneNewLine.php', [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
declare(strict_types = 1);

class Foo
{

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

class Foo
{

}

0 comments on commit 0858034

Please sign in to comment.