Skip to content

Commit

Permalink
Updated Rector to commit c431eae446a25d1b64573500f40139b20aba2c3a
Browse files Browse the repository at this point in the history
rectorphp/rector-src@c431eae [PostRector][AutoImport] Handle remove unused import with auto import  enabled on conflict with existing use statement (#5912)
  • Loading branch information
TomasVotruba committed May 25, 2024
1 parent b153174 commit 6b07114
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '650ae6a730554c4b7472280e86edbf1b704cce25';
public const PACKAGE_VERSION = 'c431eae446a25d1b64573500f40139b20aba2c3a';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-05-25 17:44:59';
public const RELEASE_DATE = '2024-05-25 19:37:37';
/**
* @var int
*/
Expand Down
37 changes: 25 additions & 12 deletions src/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function enterNode(Node $node) : ?Node
return null;
}
$hasChanged = \false;
$namespaceName = $node instanceof Namespace_ && $node->name instanceof Name ? $node->name : null;
$names = $this->resolveUsedPhpAndDocNames($node);
foreach ($node->stmts as $key => $namespaceStmt) {
if (!$namespaceStmt instanceof Use_) {
Expand All @@ -57,7 +58,7 @@ public function enterNode(Node $node) : ?Node
continue;
}
$useUse = $namespaceStmt->uses[0];
if ($this->isUseImportUsed($useUse, $names)) {
if ($this->isUseImportUsed($useUse, $names, $namespaceName)) {
continue;
}
unset($node->stmts[$key]);
Expand Down Expand Up @@ -149,7 +150,7 @@ private function resolveAliasName(UseUse $useUse) : ?string
/**
* @param string[] $names
*/
private function isUseImportUsed(UseUse $useUse, array $names) : bool
private function isUseImportUsed(UseUse $useUse, array $names, ?Name $namespaceName) : bool
{
$comparedName = $useUse->name->toString();
if (\in_array($comparedName, $names, \true)) {
Expand All @@ -160,32 +161,44 @@ private function isUseImportUsed(UseUse $useUse, array $names) : bool
$namespacedPrefix = $comparedName . '\\';
}
$alias = $this->resolveAliasName($useUse);
$lastName = $useUse->name->getLast();
$namespaceName = $namespaceName instanceof Name ? $namespaceName->toString() : null;
// match partial import
foreach ($names as $name) {
if (\substr_compare($comparedName, '\\' . $name, -\strlen('\\' . $name)) === 0) {
if ($this->isSubNamespace($name, $comparedName, $namespacedPrefix)) {
return \true;
}
if ($this->isSubNamespace($name, $namespacedPrefix)) {
if (\is_string($alias) && $this->isUsedAlias($alias, $name)) {
return \true;
}
if (!\is_string($alias)) {
if (\strncmp($name, $lastName . '\\', \strlen($lastName . '\\')) !== 0) {
continue;
}
if ($alias === $name) {
if ($namespaceName === null) {
return \true;
}
if (\strpos($name, '\\') === \false) {
continue;
}
$namePrefix = Strings::before($name, '\\', 1);
if ($alias === $namePrefix) {
if (\strncmp($name, $namespaceName . '\\', \strlen($namespaceName . '\\')) !== 0) {
return \true;
}
}
return \false;
}
private function isSubNamespace(string $name, string $namespacedPrefix) : bool
private function isUsedAlias(string $alias, string $name) : bool
{
if ($alias === $name) {
return \true;
}
if (\strpos($name, '\\') === \false) {
return \false;
}
$namePrefix = Strings::before($name, '\\', 1);
return $alias === $namePrefix;
}
private function isSubNamespace(string $name, string $comparedName, string $namespacedPrefix) : bool
{
if (\substr_compare($comparedName, '\\' . $name, -\strlen('\\' . $name)) === 0) {
return \true;
}
if (\strncmp($name, $namespacedPrefix, \strlen($namespacedPrefix)) === 0) {
$subNamespace = \substr($name, \strlen($namespacedPrefix));
return \strpos($subNamespace, '\\') === \false;
Expand Down

0 comments on commit 6b07114

Please sign in to comment.