Skip to content

Commit 114c8ff

Browse files
committed
UseSpacingSniff: Fixed false positive
1 parent b9e0fc0 commit 114c8ff

7 files changed

+39
-7
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/UseSpacingSniff.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function count;
1616
use function in_array;
1717
use function sprintf;
18+
use const T_DOC_COMMENT_OPEN_TAG;
1819
use const T_OPEN_TAG;
1920
use const T_SEMICOLON;
2021
use const T_WHITESPACE;
@@ -143,14 +144,13 @@ private function checkLinesAfterLastUse(File $phpcsFile, UseStatement $lastUse):
143144

144145
if (
145146
in_array($tokens[$pointerAfterWhitespaceEnd]['code'], Tokens::$commentTokens, true)
147+
&& $tokens[$pointerAfterWhitespaceEnd]['code'] !== T_DOC_COMMENT_OPEN_TAG
146148
&& (
147149
$tokens[$useEndPointer]['line'] === $tokens[$pointerAfterWhitespaceEnd]['line']
148150
|| $tokens[$useEndPointer]['line'] + 1 === $tokens[$pointerAfterWhitespaceEnd]['line']
149151
)
150152
) {
151-
$useEndPointer = array_key_exists('comment_closer', $tokens[$pointerAfterWhitespaceEnd])
152-
? $tokens[$pointerAfterWhitespaceEnd]['comment_closer']
153-
: CommentHelper::getMultilineCommentEndPointer($phpcsFile, $pointerAfterWhitespaceEnd);
153+
$useEndPointer = CommentHelper::getMultilineCommentEndPointer($phpcsFile, $pointerAfterWhitespaceEnd);
154154
/** @var int $pointerAfterWhitespaceEnd */
155155
$pointerAfterWhitespaceEnd = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $useEndPointer + 1);
156156
}
@@ -183,7 +183,7 @@ private function checkLinesAfterLastUse(File $phpcsFile, UseStatement $lastUse):
183183
}
184184

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

tests/Sniffs/Namespaces/UseSpacingSniffTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public function testDefaultSettingsWithCommentsErrors(): void
4545
self::assertAllFixedInFile($report);
4646
}
4747

48+
public function testDefaultSettingsWithComments2Errors(): void
49+
{
50+
$report = self::checkFile(__DIR__ . '/data/useSpacingWithDefaultSettingsWithComments2Errors.php');
51+
52+
self::assertSame(1, $report->getErrorCount());
53+
54+
self::assertSniffError($report, 3, UseSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_USE);
55+
56+
self::assertAllFixedInFile($report);
57+
}
58+
4859
public function testDefaultSettingsWithInlineCommentsNoErrors(): void
4960
{
5061
$report = self::checkFile(__DIR__ . '/data/useSpacingWithDefaultSettingsWithInlineCommentsNoErrors.php');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types = 1);
2+
3+
use DateTimeImmutable;
4+
5+
/**
6+
* Whatever
7+
*/
8+
class Whatever
9+
{
10+
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
use DateTimeImmutable;
4+
/**
5+
* Whatever
6+
*/
7+
class Whatever
8+
{
9+
10+
}

tests/Sniffs/Namespaces/data/useSpacingWithDefaultSettingsWithCommentsErrors.fixed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Anything
1414
*/
1515
use const PHP_VERSION_ID;
16-
/**
16+
/*
1717
* Nothing
1818
*/
1919

tests/Sniffs/Namespaces/data/useSpacingWithDefaultSettingsWithCommentsErrors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Anything
1717
*/
1818
use const PHP_VERSION_ID;
19-
/**
19+
/*
2020
* Nothing
2121
*/
2222

tests/Sniffs/Namespaces/data/useSpacingWithDefaultSettingsWithCommentsNoErrors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* Anything
1414
*/
1515
use const PHP_VERSION_ID;
16+
1617
/**
1718
* Nothing
1819
*/
19-
2020
class Whatever
2121
{
2222

0 commit comments

Comments
 (0)