Skip to content

Commit 8d05efb

Browse files
committed
UnusedUsesSniff: more tests
1 parent 8a16243 commit 8d05efb

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
8181

8282
$annotations = AnnotationHelper::getAnnotations($phpcsFile, $docCommentOpenPointer);
8383

84+
if (count($annotations) === 0) {
85+
$searchAnnotationsPointer = $tokens[$docCommentOpenPointer]['comment_closer'] + 1;
86+
continue;
87+
}
88+
8489
foreach ($unusedNames as $useStatement) {
8590
$nameAsReferencedInFile = $useStatement->getNameAsReferencedInFile();
8691
$uniqueId = UseStatement::getUniqueId($useStatement->getType(), $nameAsReferencedInFile);
@@ -107,7 +112,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
107112
continue;
108113
}
109114

110-
if (!preg_match('~(?<=^|=|\{)(' . preg_quote($nameAsReferencedInFile, '~') . ')(?=::|,|\})~i', $annotation->getParameters(), $matches)) {
115+
if (!preg_match('~(?<=^|=|\{)(' . preg_quote($nameAsReferencedInFile, '~') . ')(?=::)~i', $annotation->getParameters(), $matches)) {
111116
continue;
112117
}
113118

tests/Sniffs/Namespaces/UnusedUsesSniffTest.php

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

111-
self::assertSame(9, $report->getErrorCount());
111+
self::assertSame(12, $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.');
@@ -118,6 +118,9 @@ public function testUsedUseInAnnotationWithDisabledSearchAnnotations(): void
118118
self::assertSniffError($report, 11, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Doctrine\Common\Collections\Collection is not used in this file.');
119119
self::assertSniffError($report, 12, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Doctrine\ORM\Mapping\Property is not used in this file.');
120120
self::assertSniffError($report, 13, UnusedUsesSniff::CODE_UNUSED_USE, 'Type ProxyManager\Proxy\GhostObjectInterface is not used in this file.');
121+
self::assertSniffError($report, 14, UnusedUsesSniff::CODE_UNUSED_USE, 'Type InvalidArgumentException is not used in this file.');
122+
self::assertSniffError($report, 15, UnusedUsesSniff::CODE_UNUSED_USE, 'Type LengthException is not used in this file.');
123+
self::assertSniffError($report, 16, UnusedUsesSniff::CODE_UNUSED_USE, 'Type RuntimeException is not used in this file.');
121124
}
122125

123126
public function testUsedUseInAnnotationWithEnabledSearchAnnotations(): void
@@ -151,13 +154,14 @@ public function testReportCaseInsensitiveUse(): void
151154
'searchAnnotations' => true,
152155
], [UnusedUsesSniff::CODE_MISMATCHING_CASE]);
153156

154-
self::assertSame(4, $report->getErrorCount());
157+
self::assertSame(5, $report->getErrorCount());
155158

156159
self::assertSniffError($report, 9, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "bar" and use statement "Bar" do not match');
157160
self::assertSniffError($report, 11, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "BAR" and use statement "Bar" do not match');
158161

159162
self::assertSniffError($report, 13, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "boo" and use statement "Boo" do not match');
160163
self::assertSniffError($report, 14, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "BOO" and use statement "Boo" do not match');
164+
self::assertSniffError($report, 16, UnusedUsesSniff::CODE_MISMATCHING_CASE, 'Case of reference name "boo" and use statement "Boo" do not match');
161165
}
162166

163167
public function testUsedTrait(): void

tests/Sniffs/Namespaces/data/caseInsensitiveUse.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
/** @var boo */
1414
/** @BOO */
15+
/**
16+
* @ORM\OneToMany(targetEntity=boo::class, mappedBy="boo")
17+
*/
1518

1619
/**
1720
* exception (at the beginning of description)
@@ -44,3 +47,8 @@
4447
/**
4548
* @Route("/uuid/example")
4649
*/
50+
51+
/**
52+
* @Route("/widget/list", name="widget_list")
53+
* @Route("/widget/view/{uuid}", name="widget_view")
54+
*/

tests/Sniffs/Namespaces/data/unusedUsesInAnnotation.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Doctrine\Common\Collections\Collection;
1212
use Doctrine\ORM\Mapping\Property;
1313
use ProxyManager\Proxy\GhostObjectInterface;
14+
use InvalidArgumentException;
15+
use LengthException;
16+
use RuntimeException;
1417

1518
/**
1619
* @ORM\Entity()
@@ -45,4 +48,13 @@ public function foo()
4548
*/
4649
public function bar($propertyMappings, $collection) {}
4750

51+
/**
52+
* @expectedException InvalidArgumentException
53+
* @expectedException LengthException
54+
* @expectedException RuntimeException
55+
*/
56+
public function test()
57+
{
58+
59+
}
4860
}

0 commit comments

Comments
 (0)