Skip to content

Commit

Permalink
[AutoImport][Renaming] Handle same last name no namespace just rename…
Browse files Browse the repository at this point in the history
…d auto import (#5248)

* [AutoImport][Renaming] Handle same last name no namespace just renamed auto import

* udpate fixture

* Fixed 🎉

* fix

* add namespaced fixture

* use in attrbiute

* fix

* fixed

* fix

* clean up

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 14, 2023
1 parent f0b8af8 commit 889372f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

/**
Expand All @@ -25,7 +24,8 @@
final class FullyQualifiedNameClassNameImportSkipVoter implements ClassNameImportSkipVoterInterface
{
public function __construct(
private readonly ShortNameResolver $shortNameResolver
private readonly ShortNameResolver $shortNameResolver,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector
) {
}

Expand All @@ -36,13 +36,11 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
$shortNamesToFullyQualifiedNames = $this->shortNameResolver->resolveFromFile($file);
$fullyQualifiedObjectTypeShortName = $fullyQualifiedObjectType->getShortName();
$className = $fullyQualifiedObjectType->getClassName();
$justRenamed = $node instanceof FullyQualified && ! $node->hasAttribute(AttributeKey::ORIGINAL_NAME);
$removedUses = $this->renamedClassesDataCollector->getOldClasses();

foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
if ($fullyQualifiedObjectTypeShortName !== $shortName) {
$shortName = str_starts_with($shortName, '\\')
? ltrim((string) Strings::after($shortName, '\\', -1))
: $shortName;
$shortName = $this->cleanShortName($shortName);
}

if ($fullyQualifiedObjectTypeShortName !== $shortName) {
Expand All @@ -54,9 +52,16 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
return false;
}

return ! $justRenamed;
return ! in_array($fullyQualifiedName, $removedUses, true);
}

return false;
}

private function cleanShortName(string $shortName): string
{
return str_starts_with($shortName, '\\')
? ltrim((string) Strings::after($shortName, '\\', -1))
: $shortName;
}
}
4 changes: 3 additions & 1 deletion rules/Renaming/Rector/Name/RenameClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Configuration\RenamedClassesDataCollector;
Expand Down Expand Up @@ -80,11 +81,12 @@ public function getNodeTypes(): array
Expression::class,
ClassLike::class,
Namespace_::class,
If_::class,
];
}

/**
* @param FunctionLike|Name|ClassLike|Expression|Namespace_|Property $node
* @param FunctionLike|Name|ClassLike|Expression|Namespace_|Property|If_ $node
*/
public function refactor(Node $node): ?Node
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

function NoNamespaceUsedClassRenamed(
// fqcn
\Some\Exception $e
)
{
// from root
$obj = new Exception();
}

?>
-----
<?php

function NoNamespaceUsedClassRenamed(
// fqcn
\Some\Target\Exception $e
)
{
// from root
$obj = new Exception();
}

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

namespace App;

function WithNamespaceUsedClassRenamed(
// fqcn
\Some\Exception $e
)
{
// from root namespace App
$obj = new Exception();
}

?>
-----
<?php

namespace App;

function WithNamespaceUsedClassRenamed(
// fqcn
\Some\Target\Exception $e
)
{
// from root namespace App
$obj = new Exception();
}

?>
4 changes: 4 additions & 0 deletions tests/Issues/AutoImport/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames();
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'Some\Exception' => 'Some\Target\Exception',
]);
};

0 comments on commit 889372f

Please sign in to comment.