Skip to content

Commit

Permalink
UseSpacingSniff: Fixed false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Sep 9, 2021
1 parent b9e0fc0 commit 114c8ff
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
8 changes: 4 additions & 4 deletions SlevomatCodingStandard/Sniffs/Namespaces/UseSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function count;
use function in_array;
use function sprintf;
use const T_DOC_COMMENT_OPEN_TAG;
use const T_OPEN_TAG;
use const T_SEMICOLON;
use const T_WHITESPACE;
Expand Down Expand Up @@ -143,14 +144,13 @@ private function checkLinesAfterLastUse(File $phpcsFile, UseStatement $lastUse):

if (
in_array($tokens[$pointerAfterWhitespaceEnd]['code'], Tokens::$commentTokens, true)
&& $tokens[$pointerAfterWhitespaceEnd]['code'] !== T_DOC_COMMENT_OPEN_TAG
&& (
$tokens[$useEndPointer]['line'] === $tokens[$pointerAfterWhitespaceEnd]['line']
|| $tokens[$useEndPointer]['line'] + 1 === $tokens[$pointerAfterWhitespaceEnd]['line']
)
) {
$useEndPointer = array_key_exists('comment_closer', $tokens[$pointerAfterWhitespaceEnd])
? $tokens[$pointerAfterWhitespaceEnd]['comment_closer']
: CommentHelper::getMultilineCommentEndPointer($phpcsFile, $pointerAfterWhitespaceEnd);
$useEndPointer = CommentHelper::getMultilineCommentEndPointer($phpcsFile, $pointerAfterWhitespaceEnd);
/** @var int $pointerAfterWhitespaceEnd */
$pointerAfterWhitespaceEnd = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $useEndPointer + 1);
}
Expand Down Expand Up @@ -183,7 +183,7 @@ private function checkLinesAfterLastUse(File $phpcsFile, UseStatement $lastUse):
}

$linesToAdd = $requiredLinesCountAfterLastUse;
if (in_array($tokens[$useEndPointer]['code'], TokenHelper::$inlineCommentTokenCodes, true)) {
if (CommentHelper::isLineComment($phpcsFile, $useEndPointer)) {
$linesToAdd--;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Sniffs/Namespaces/UseSpacingSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public function testDefaultSettingsWithCommentsErrors(): void
self::assertAllFixedInFile($report);
}

public function testDefaultSettingsWithComments2Errors(): void
{
$report = self::checkFile(__DIR__ . '/data/useSpacingWithDefaultSettingsWithComments2Errors.php');

self::assertSame(1, $report->getErrorCount());

self::assertSniffError($report, 3, UseSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_USE);

self::assertAllFixedInFile($report);
}

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

use DateTimeImmutable;

/**
* Whatever
*/
class Whatever
{

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

use DateTimeImmutable;
/**
* Whatever
*/
class Whatever
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Anything
*/
use const PHP_VERSION_ID;
/**
/*
* Nothing
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Anything
*/
use const PHP_VERSION_ID;
/**
/*
* Nothing
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* Anything
*/
use const PHP_VERSION_ID;

/**
* Nothing
*/

class Whatever
{

Expand Down

0 comments on commit 114c8ff

Please sign in to comment.