Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UnusedImport] Fix removing multiple uses #6376

Merged
merged 5 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function enterNode(Node $node): ?Node
: null;

$namesInOriginalCase = $this->resolveUsedPhpAndDocNames($node);
$namesInLowerCase = null; // Initialized as null, lazy loaded when case in-sensitive names are needed
$namesInLowerCase = array_map(strtolower(...), $namesInOriginalCase);

foreach ($node->stmts as $key => $namespaceStmt) {
if (! $namespaceStmt instanceof Use_) {
Expand All @@ -55,25 +55,28 @@ public function enterNode(Node $node): ?Node
continue;
}

$useUse = $namespaceStmt->uses[0];
$isCaseSensitive = $namespaceStmt->type === Use_::TYPE_CONSTANT;

if ($isCaseSensitive) {
$names = $namesInOriginalCase;
} else {
if ($namesInLowerCase === null) {
$namesInLowerCase = array_map(strtolower(...), $namesInOriginalCase);
}

$names = $namesInLowerCase;
}

if ($this->isUseImportUsed($useUse, $isCaseSensitive, $names, $namespaceName)) {
continue;
foreach ($namespaceStmt->uses as $useUseKey => $useUse) {

if ($this->isUseImportUsed($useUse, $isCaseSensitive, $names, $namespaceName)) {
continue;
}

unset($namespaceStmt->uses[$useUseKey]);

$hasChanged = true;
}

unset($node->stmts[$key]);
$hasChanged = true;
if ($namespaceStmt->uses === []) {
unset($node->stmts[$key]);
}
}

if ($hasChanged === false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Bar;

use const A, B, C;
use const D, E, F;
use const G, H, I;
use const J, K, L;
use const AA, BB, CC;

use const M, N;
use const O, P;
use const Q, R;
use const X, Y;

var_dump(A);
var_dump(E);
var_dump(I);
var_dump(AA);
var_dump(BB);
var_dump(CC);

var_dump(M);
var_dump(P);
var_dump(X);
var_dump(Y);

?>
-----
<?php

namespace App\Bar;

use const A;
use const E;
use const I;
use const AA, BB, CC;

use const M;
use const P;
use const X, Y;

var_dump(A);
var_dump(E);
var_dump(I);
var_dump(AA);
var_dump(BB);
var_dump(CC);

var_dump(M);
var_dump(P);
var_dump(X);
var_dump(Y);

?>
55 changes: 55 additions & 0 deletions tests/Issues/NamespacedUse/Fixture/multiple_use_namespace.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Bar;

use A, B, C;
use D, E, F;
use G, H, I;
use J, K, L;
use AA, BB, CC;

use M, N;
use O, P;
use Q, R;
use X, Y;

var_dump(A::class);
var_dump(E::class);
var_dump(I::class);
var_dump(AA::class);
var_dump(BB::class);
var_dump(CC::class);

var_dump(M::class);
var_dump(P::class);
var_dump(X::class);
var_dump(Y::class);

?>
-----
<?php

namespace App\Bar;

use A;
use E;
use I;
use AA, BB, CC;

use M;
use P;
use X, Y;

var_dump(A::class);
var_dump(E::class);
var_dump(I::class);
var_dump(AA::class);
var_dump(BB::class);
var_dump(CC::class);

var_dump(M::class);
var_dump(P::class);
var_dump(X::class);
var_dump(Y::class);

?>