Skip to content

Commit a2771a8

Browse files
SpacePossumkeradus
authored andcommitted
Import cannot be used after :: so can be removed.
1 parent f031f23 commit a2771a8

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/Fixer/Import/NoUnusedImportsFixer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
9090
foreach ((new NamespacesAnalyzer())->getDeclarations($tokens) as $namespace) {
9191
$currentNamespaceUseDeclarations = array_filter(
9292
$useDeclarations,
93-
function (NamespaceUseAnalysis $useDeclaration) use ($namespace) {
93+
static function (NamespaceUseAnalysis $useDeclaration) use ($namespace) {
9494
return
9595
$useDeclaration->getStartIndex() >= $namespace->getScopeStartIndex()
9696
&& $useDeclaration->getEndIndex() <= $namespace->getScopeEndIndex()
@@ -143,18 +143,20 @@ private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, arra
143143

144144
if (
145145
0 === strcasecmp($shortName, $token->getContent())
146-
&& !$prevMeaningfulToken->isGivenKind([T_NS_SEPARATOR, T_CONST, T_OBJECT_OPERATOR])
146+
&& !$prevMeaningfulToken->isGivenKind([T_NS_SEPARATOR, T_CONST, T_OBJECT_OPERATOR, T_DOUBLE_COLON])
147147
) {
148148
return true;
149149
}
150150

151151
continue;
152152
}
153153

154-
if ($token->isComment() && Preg::match(
155-
'/(?<![[:alnum:]])(?<!\\\\)'.$shortName.'(?![[:alnum:]])/i',
156-
$token->getContent()
157-
)) {
154+
if ($token->isComment()
155+
&& Preg::match(
156+
'/(?<![[:alnum:]])(?<!\\\\)'.$shortName.'(?![[:alnum:]])/i',
157+
$token->getContent()
158+
)
159+
) {
158160
return true;
159161
}
160162
}

tests/Fixer/Import/NoUnusedImportsFixerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,15 @@ public function __construct(SomeEntityRepository $repo)
506506
EOF
507507
,
508508
],
509-
'property_name' => [
509+
'property name, method name, static method call, static property' => [
510510
<<<'EOF'
511511
<?php
512512
513513
514514
$foo->bar = null;
515+
$foo->bar();
516+
$foo::bar();
517+
$foo::bar;
515518
EOF
516519
,
517520
<<<'EOF'
@@ -520,6 +523,9 @@ public function __construct(SomeEntityRepository $repo)
520523
use Foo\Bar;
521524
522525
$foo->bar = null;
526+
$foo->bar();
527+
$foo::bar();
528+
$foo::bar;
523529
EOF
524530
,
525531
],

0 commit comments

Comments
 (0)