Skip to content

Commit 3d5de20

Browse files
committed
UnusedUsesSniff: fixed false positives
1 parent f6f48ae commit 3d5de20

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
163163

164164
if (
165165
!preg_match('~(?<=^|[^a-z\\\\])(' . preg_quote($nameAsReferencedInFile, '~') . ')(?=::)~i', $annotation->getParameters(), $matches)
166-
&& !preg_match('~(?<=@)(' . preg_quote($nameAsReferencedInFile, '~') . ')(?=\\\\|\()~i', $annotation->getParameters(), $matches)
166+
&& !preg_match('~(?<=@)(' . preg_quote($nameAsReferencedInFile, '~') . ')(?=[^\\w])~i', $annotation->getParameters(), $matches)
167167
) {
168168
continue;
169169
}

tests/Sniffs/Namespaces/UnusedUsesSniffTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testUsedUseInAnnotationWithDisabledSearchAnnotations(): void
108108
'searchAnnotations' => false,
109109
]);
110110

111-
self::assertSame(32, $report->getErrorCount());
111+
self::assertSame(36, $report->getErrorCount());
112112

113113
self::assertSniffError($report, 5, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Assert is not used in this file.');
114114
self::assertSniffError($report, 6, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Doctrine\ORM\Mapping (as ORM) is not used in this file.');
@@ -141,6 +141,10 @@ public function testUsedUseInAnnotationWithDisabledSearchAnnotations(): void
141141
self::assertSniffError($report, 34, UnusedUsesSniff::CODE_UNUSED_USE, 'Type MethodParameter4 is not used in this file.');
142142
self::assertSniffError($report, 35, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Discriminator\Lorem is not used in this file.');
143143
self::assertSniffError($report, 36, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Discriminator\Ipsum is not used in this file.');
144+
self::assertSniffError($report, 37, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Outer is not used in this file.');
145+
self::assertSniffError($report, 38, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Inner1 is not used in this file.');
146+
self::assertSniffError($report, 39, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Inner2 is not used in this file.');
147+
self::assertSniffError($report, 40, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Inner3 is not used in this file.');
144148
}
145149

146150
public function testUsedUseInAnnotationWithEnabledSearchAnnotations(): void

tests/Sniffs/Namespaces/data/unusedUsesInAnnotation.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
use MethodParameter4;
3535
use Discriminator\Lorem;
3636
use Discriminator\Ipsum;
37+
use Outer;
38+
use Inner1;
39+
use Inner2;
40+
use Inner3;
3741

3842
/**
3943
* @ORM\Entity()
@@ -98,6 +102,14 @@ public function test()
98102
* @CustomAnnotation(prop=@AnotherCustomAnnotation(D::class))
99103
*/
100104

105+
/**
106+
* @Outer({
107+
* @Inner1(type="string"),
108+
* @Inner2,
109+
* @Inner3(),
110+
* })
111+
*/
112+
101113
/** @var $variable UglyInlineAnnotation */
102114

103115
/**

0 commit comments

Comments
 (0)