Skip to content

Commit db3f132

Browse files
committed
ReferenceUsedNamesOnlySniff: Improved fixer
1 parent b62e40e commit db3f132

10 files changed

+188
-6
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use PHP_CodeSniffer\Util\Tokens;
78
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
89
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
910
use SlevomatCodingStandard\Helpers\Annotation\GenericAnnotation;
1011
use SlevomatCodingStandard\Helpers\AnnotationConstantExpressionHelper;
1112
use SlevomatCodingStandard\Helpers\AnnotationHelper;
1213
use SlevomatCodingStandard\Helpers\AnnotationTypeHelper;
1314
use SlevomatCodingStandard\Helpers\ClassHelper;
15+
use SlevomatCodingStandard\Helpers\CommentHelper;
1416
use SlevomatCodingStandard\Helpers\ConstantHelper;
1517
use SlevomatCodingStandard\Helpers\FunctionHelper;
1618
use SlevomatCodingStandard\Helpers\NamespaceHelper;
@@ -37,6 +39,7 @@
3739
use function function_exists;
3840
use function in_array;
3941
use function sprintf;
42+
use function strlen;
4043
use function strtolower;
4144
use function substr;
4245
use const T_COMMA;
@@ -572,14 +575,41 @@ private function getUseStatementPlacePointer(File $phpcsFile, int $openTagPointe
572575
return $useStatementPlacePointer;
573576
}
574577

575-
$pointerAfterOpenTagPointer = TokenHelper::findNextEffective($phpcsFile, $openTagPointer + 1);
576-
if ($phpcsFile->getTokens()[$pointerAfterOpenTagPointer]['code'] === T_DECLARE) {
577-
/** @var int $useStatementPlacePointer */
578-
$useStatementPlacePointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $pointerAfterOpenTagPointer + 1);
579-
return $useStatementPlacePointer;
578+
$tokens = $phpcsFile->getTokens();
579+
580+
$useStatementPlacePointer = $openTagPointer;
581+
582+
$nonWhitespacePointerAfterOpenTag = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $openTagPointer + 1);
583+
if (in_array($tokens[$nonWhitespacePointerAfterOpenTag]['code'], Tokens::$commentTokens, true)) {
584+
$commentEndPointer = CommentHelper::getCommentEndPointer($phpcsFile, $nonWhitespacePointerAfterOpenTag);
585+
586+
if (substr($tokens[$commentEndPointer]['content'], -strlen($phpcsFile->eolChar)) === $phpcsFile->eolChar) {
587+
$useStatementPlacePointer = $commentEndPointer;
588+
} else {
589+
$newLineAfterComment = $commentEndPointer + 1;
590+
591+
if (array_key_exists($newLineAfterComment, $tokens) && $tokens[$newLineAfterComment]['content'] === $phpcsFile->eolChar) {
592+
$pointerAfterCommentEnd = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $newLineAfterComment + 1);
593+
594+
if (TokenHelper::findNextContent(
595+
$phpcsFile,
596+
T_WHITESPACE,
597+
$phpcsFile->eolChar,
598+
$newLineAfterComment + 1,
599+
$pointerAfterCommentEnd
600+
) !== null) {
601+
$useStatementPlacePointer = $commentEndPointer;
602+
}
603+
}
604+
}
605+
}
606+
607+
$pointerAfter = TokenHelper::findNextEffective($phpcsFile, $useStatementPlacePointer + 1);
608+
if ($tokens[$pointerAfter]['code'] === T_DECLARE) {
609+
return TokenHelper::findNext($phpcsFile, T_SEMICOLON, $pointerAfter + 1);
580610
}
581611

582-
return $openTagPointer;
612+
return $useStatementPlacePointer;
583613
}
584614

585615
private function isRequiredToBeUsed(string $name): bool

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,4 +987,40 @@ public function testReferencingGlobalTypesInGlobalNamespace(): void
987987
self::assertNoSniffErrorInFile($report);
988988
}
989989

990+
public function testWithFileComment(): void
991+
{
992+
$report = self::checkFile(__DIR__ . '/data/referenceUsedNamesOnlyWithFileComment.php');
993+
994+
self::assertSniffError($report, 11, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
995+
996+
self::assertAllFixedInFile($report);
997+
}
998+
999+
public function testWithFileCommentAndDeclare(): void
1000+
{
1001+
$report = self::checkFile(__DIR__ . '/data/referenceUsedNamesOnlyWithFileCommentAndDeclare.php');
1002+
1003+
self::assertSniffError($report, 13, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
1004+
1005+
self::assertAllFixedInFile($report);
1006+
}
1007+
1008+
public function testWithInlineFileComment(): void
1009+
{
1010+
$report = self::checkFile(__DIR__ . '/data/referenceUsedNamesOnlyWithInlineFileComment.php');
1011+
1012+
self::assertSniffError($report, 9, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
1013+
1014+
self::assertAllFixedInFile($report);
1015+
}
1016+
1017+
public function testWithClassComment(): void
1018+
{
1019+
$report = self::checkFile(__DIR__ . '/data/referenceUsedNamesOnlyWithClassComment.php');
1020+
1021+
self::assertSniffError($report, 11, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
1022+
1023+
self::assertAllFixedInFile($report);
1024+
}
1025+
9901026
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use F\Q\N;
4+
/**
5+
* Class comment
6+
*/
7+
class Foo
8+
{
9+
10+
public function test()
11+
{
12+
N::doStuff();
13+
}
14+
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* Class comment
5+
*/
6+
class Foo
7+
{
8+
9+
public function test()
10+
{
11+
\F\Q\N::doStuff();
12+
}
13+
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/*
3+
* File header
4+
*/
5+
use F\Q\N;
6+
7+
class Foo
8+
{
9+
10+
public function test()
11+
{
12+
N::doStuff();
13+
}
14+
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/*
3+
* File header
4+
*/
5+
6+
class Foo
7+
{
8+
9+
public function test()
10+
{
11+
\F\Q\N::doStuff();
12+
}
13+
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/*
3+
* File header
4+
*/
5+
6+
declare(strict_types = 1);
7+
use F\Q\N;
8+
9+
class Foo
10+
{
11+
12+
public function test()
13+
{
14+
N::doStuff();
15+
}
16+
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/*
3+
* File header
4+
*/
5+
6+
declare(strict_types = 1);
7+
8+
class Foo
9+
{
10+
11+
public function test()
12+
{
13+
\F\Q\N::doStuff();
14+
}
15+
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
// File header
3+
4+
use F\Q\N;
5+
class Foo
6+
{
7+
8+
public function test()
9+
{
10+
N::doStuff();
11+
}
12+
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
// File header
3+
4+
class Foo
5+
{
6+
7+
public function test()
8+
{
9+
\F\Q\N::doStuff();
10+
}
11+
12+
}

0 commit comments

Comments
 (0)