Skip to content

Commit df75233

Browse files
committed
Improved unused uses detection
1 parent 639afb0 commit df75233

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,22 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $openTagPointer)
6464
$tokens = $phpcsFile->getTokens();
6565
$searchAnnotationsPointer = $openTagPointer + 1;
6666
while (true) {
67-
$phpDocTokenPointer = $phpcsFile->findNext([T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING], $searchAnnotationsPointer);
68-
if ($phpDocTokenPointer === false) {
67+
$docCommentPointer = $phpcsFile->findNext([T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING], $searchAnnotationsPointer);
68+
if ($docCommentPointer === false) {
6969
break;
7070
}
7171

7272
foreach ($unusedNames as $i => $useStatement) {
73-
if ($tokens[$phpDocTokenPointer]['code'] === T_DOC_COMMENT_TAG && preg_match('~^@' . preg_quote($useStatement->getNameAsReferencedInFile(), '~') . '(?:[^a-z\\d]|$)~i', $tokens[$phpDocTokenPointer]['content'])) {
74-
unset($unusedNames[$i]);
75-
} elseif ($tokens[$phpDocTokenPointer]['code'] === T_DOC_COMMENT_STRING && preg_match('~(?:^|[^a-z\\d\\\\])' . preg_quote($useStatement->getNameAsReferencedInFile(), '~') . '(?:[^a-z\\d\\\\]|$)~i', $tokens[$phpDocTokenPointer]['content'])) {
73+
$nameAsReferencedInFile = $useStatement->getNameAsReferencedInFile();
74+
if (
75+
preg_match('~^@' . preg_quote($nameAsReferencedInFile, '~') . '(?=[^a-z\\d]|$)~i', $tokens[$docCommentPointer]['content'])
76+
|| preg_match('~(?<=^|[^a-z\\d\\\\])' . preg_quote($nameAsReferencedInFile, '~') . '(?=[^a-z\\d\\\\]|$)~i', $tokens[$docCommentPointer]['content'])
77+
) {
7678
unset($unusedNames[$i]);
7779
}
7880
}
7981

80-
$searchAnnotationsPointer = $phpDocTokenPointer + 1;
82+
$searchAnnotationsPointer = $docCommentPointer + 1;
8183
}
8284
}
8385

tests/Sniffs/Namespaces/UnusedUsesSniffTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testUsedUseInAnnotationWithDisabledSearchAnnotations()
9494
'searchAnnotations' => false,
9595
]);
9696

97-
$this->assertSame(4, $report->getErrorCount());
97+
$this->assertSame(5, $report->getErrorCount());
9898

9999
$this->assertSniffError(
100100
$report,
@@ -110,13 +110,13 @@ public function testUsedUseInAnnotationWithDisabledSearchAnnotations()
110110
);
111111
$this->assertSniffError(
112112
$report,
113-
7,
113+
8,
114114
UnusedUsesSniff::CODE_UNUSED_USE,
115115
'Type X is not used in this file.'
116116
);
117117
$this->assertSniffError(
118118
$report,
119-
8,
119+
9,
120120
UnusedUsesSniff::CODE_UNUSED_USE,
121121
'Type XX is not used in this file.'
122122
);
@@ -132,7 +132,7 @@ public function testUsedUseInAnnotationWithEnabledSearchAnnotations()
132132

133133
$this->assertSniffError(
134134
$report,
135-
8,
135+
9,
136136
UnusedUsesSniff::CODE_UNUSED_USE,
137137
'Type XX is not used in this file.'
138138
);

tests/Sniffs/Namespaces/data/unusedUsesInAnnotation.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Assert;
66
use Doctrine\ORM\Mapping as ORM;
7+
use Foo\Bar;
78
use X;
89
use XX;
910

@@ -18,9 +19,15 @@ class Boo
1819
*/
1920
public $id;
2021

22+
/**
23+
* @ORM\OneToMany(targetEntity=Bar::class, mappedBy="boo")
24+
* @var \Foo\Bar[]
25+
*/
26+
private $bars;
27+
2128
/**
2229
* @Assert
23-
* @Assert\NotBlank(groups={X::SOME_CONSTANT}
30+
* @Assert\NotBlank(groups={X::SOME_CONSTANT})
2431
*/
2532
public function foo()
2633
{

0 commit comments

Comments
 (0)