Updated Rector to commit cb4ccd650eba6b43ac07da13e53b816a4119fad1

cb4ccd650e [UnusedImport] Different letter case for classes + optimization (#6362)
This commit is contained in:
Tomas Votruba 2024-10-11 01:59:43 +00:00
parent c8ccd4898f
commit 43ea81221f
2 changed files with 28 additions and 9 deletions

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '6dee862408c55479969b4bc0ba28a1d50d5d2e44';
public const PACKAGE_VERSION = 'cb4ccd650eba6b43ac07da13e53b816a4119fad1';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-10-10 15:04:05';
public const RELEASE_DATE = '2024-10-11 08:57:33';
/**
* @var int
*/

View File

@ -42,18 +42,29 @@ final class UnusedImportRemovingPostRector extends \Rector\PostRector\Rector\Abs
}
$hasChanged = \false;
$namespaceName = $node instanceof Namespace_ && $node->name instanceof Name ? $node->name : null;
$names = $this->resolveUsedPhpAndDocNames($node);
$namesInOriginalCase = $this->resolveUsedPhpAndDocNames($node);
$namesInLowerCase = null;
// Initialized as null, lazy loaded when case in-sensitive names are needed
foreach ($node->stmts as $key => $namespaceStmt) {
if (!$namespaceStmt instanceof Use_) {
continue;
}
if ($namespaceStmt->uses === []) {
if ($namespaceStmt->uses === [] || $namesInOriginalCase === []) {
unset($node->stmts[$key]);
$hasChanged = \true;
continue;
}
$useUse = $namespaceStmt->uses[0];
if ($this->isUseImportUsed($useUse, $names, $namespaceName)) {
$isCaseSensitive = $namespaceStmt->type === Use_::TYPE_CONSTANT;
if ($isCaseSensitive) {
$names = $namesInOriginalCase;
} else {
if ($namesInLowerCase === null) {
$namesInLowerCase = \array_map(\Closure::fromCallable('strtolower'), $namesInOriginalCase);
}
$names = $namesInLowerCase;
}
if ($this->isUseImportUsed($useUse, $isCaseSensitive, $names, $namespaceName)) {
continue;
}
unset($node->stmts[$key]);
@ -140,11 +151,14 @@ final class UnusedImportRemovingPostRector extends \Rector\PostRector\Rector\Abs
return \array_unique($names);
}
/**
* @param string[] $names
* @param string[] $names
*/
private function isUseImportUsed(UseUse $useUse, array $names, ?Name $namespaceName) : bool
private function isUseImportUsed(UseUse $useUse, bool $isCaseSensitive, array $names, ?Name $namespaceName) : bool
{
$comparedName = $useUse->alias instanceof Identifier ? $useUse->alias->toString() : $useUse->name->toString();
if (!$isCaseSensitive) {
$comparedName = \strtolower($comparedName);
}
if (\in_array($comparedName, $names, \true)) {
return \true;
}
@ -152,8 +166,13 @@ final class UnusedImportRemovingPostRector extends \Rector\PostRector\Rector\Abs
if ($namespacedPrefix === '\\') {
$namespacedPrefix = $comparedName . '\\';
}
$lastName = $useUse->name->getLast();
$namespaceName = $namespaceName instanceof Name ? $namespaceName->toString() : null;
if (!$isCaseSensitive) {
$lastName = \strtolower($useUse->name->getLast());
$namespaceName = $namespaceName instanceof Name ? \strtolower($namespaceName->toString()) : null;
} else {
$lastName = $useUse->name->getLast();
$namespaceName = $namespaceName instanceof Name ? $namespaceName->toString() : null;
}
// match partial import
foreach ($names as $name) {
if ($this->isSubNamespace($name, $comparedName, $namespacedPrefix)) {