Skip to content

Commit

Permalink
[PostRector] Allow remove unused use sub namespace on UnusedImportRem…
Browse files Browse the repository at this point in the history
…ovingPostRector (#5299)

* [PostRector] Allow remove unused use sub namespace on UnusedImportRemovingPostRector

* more fixture

* more fixtures

* more fixture

* implemented 🎉

* fix phpstan

* fix phpstan
  • Loading branch information
samsonasik committed Dec 2, 2023
1 parent e48234e commit 7badba6
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function findNonUseImportNames(Namespace_|FileWithoutNamespace $namespac
{
$names = [];

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($namespace, static function (Node $node) use (
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($namespace->stmts, static function (Node $node) use (
&$names
): int|null|Name {
if ($node instanceof Use_) {
Expand Down Expand Up @@ -176,7 +176,7 @@ private function isUseImportUsed(UseUse $useUse, array $names): bool
return true;
}

if (str_starts_with($name, $namespacedPrefix)) {
if ($this->isSubNamespace($name, $namespacedPrefix)) {
return true;
}

Expand All @@ -200,4 +200,14 @@ private function isUseImportUsed(UseUse $useUse, array $names): bool

return false;
}

private function isSubNamespace(string $name, string $namespacedPrefix): bool
{
if (str_starts_with($name, $namespacedPrefix)) {
$subNamespace = substr($name, strlen($namespacedPrefix));
return ! str_contains($subNamespace, '\\');
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Bar;

use App\Bar\Baz;

final class SkipSubNamepaceFromNamespaceInUse
{
public function run(Baz $baz)
{
}
}
12 changes: 12 additions & 0 deletions tests/Issues/NamespacedUse/Fixture/skip_subnamespace_used.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Bar;

use App;

final class SkipSubNamespaceUsed
{
public function run(App\Baz $baz)
{
}
}
21 changes: 21 additions & 0 deletions tests/Issues/NamespacedUse/Fixture/sub_namespace.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Bar;

use App;

final class SubNamespace
{
}

?>
-----
<?php

namespace App\Bar;

final class SubNamespace
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Bar;

use App;

final class SubNamepaceFromNamespace
{
public function run(Baz $baz)
{
}
}

?>
-----
<?php

namespace App\Bar;

final class SubNamepaceFromNamespace
{
public function run(Baz $baz)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Bar;

use App;

final class SubNamepaceFromNamespace2
{
public function run(Bar\Baz $baz)
{
}
}

?>
-----
<?php

namespace App\Bar;

final class SubNamepaceFromNamespace2
{
public function run(Bar\Baz $baz)
{
}
}

?>

0 comments on commit 7badba6

Please sign in to comment.