mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-22 10:55:10 +00:00
[Renaming] Fix duplicate namespacing on RenameNamespaceRector (#1759)
Co-authored-by: Harings Rob <haringsrob@gmail.com> Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
2468277687
commit
8320d29003
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
class BlogRepository {
|
||||
}
|
||||
|
||||
function index()
|
||||
{
|
||||
\App\Repositories\BlogRepository::class;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Example;
|
||||
|
||||
class BlogRepository {
|
||||
}
|
||||
|
||||
function index()
|
||||
{
|
||||
\App\Repositories\Example\BlogRepository::class;
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repositories\Example;
|
||||
|
||||
class BlogRepository {
|
||||
}
|
||||
|
||||
function index()
|
||||
{
|
||||
\App\Repositories\Example\BlogRepository::class;
|
||||
}
|
|
@ -14,5 +14,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
'Old\Long\AnyNamespace' => 'Short\AnyNamespace',
|
||||
'PHPUnit_Framework_' => 'PHPUnit\Framework\\',
|
||||
'Foo\Bar' => 'Foo\Tmp',
|
||||
'App\Repositories' => 'App\Repositories\Example',
|
||||
]);
|
||||
};
|
||||
|
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace Rector\Renaming\Rector\Namespace_;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Name;
|
||||
|
@ -134,18 +133,19 @@ final class RenameNamespaceRector extends AbstractRector implements Configurable
|
|||
|
||||
private function processFullyQualified(Name $name, RenamedNamespace $renamedNamespace): ?FullyQualified
|
||||
{
|
||||
$newName = $this->isPartialNamespace($name) ? $this->resolvePartialNewName(
|
||||
$name,
|
||||
$renamedNamespace
|
||||
) : $renamedNamespace->getNameInNewNamespace();
|
||||
|
||||
$values = array_values($this->oldToNewNamespaces);
|
||||
if (! isset($this->isChangedInNamespaces[$newName])) {
|
||||
return new FullyQualified($newName);
|
||||
if (str_starts_with($name->toString(), $renamedNamespace->getNewNamespace() . '\\')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! in_array($newName, $values, true)) {
|
||||
return new FullyQualified($newName);
|
||||
$nameInNewNamespace = $renamedNamespace->getNameInNewNamespace();
|
||||
|
||||
$values = array_values($this->oldToNewNamespaces);
|
||||
if (! isset($this->isChangedInNamespaces[$nameInNewNamespace])) {
|
||||
return new FullyQualified($nameInNewNamespace);
|
||||
}
|
||||
|
||||
if (! in_array($nameInNewNamespace, $values, true)) {
|
||||
return new FullyQualified($nameInNewNamespace);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -173,28 +173,4 @@ final class RenameNamespaceRector extends AbstractRector implements Configurable
|
|||
|
||||
return array_key_exists($newClassName, $this->oldToNewNamespaces);
|
||||
}
|
||||
|
||||
private function isPartialNamespace(Name $name): bool
|
||||
{
|
||||
$resolvedName = $name->getAttribute(AttributeKey::RESOLVED_NAME);
|
||||
if (! $resolvedName instanceof Name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($resolvedName instanceof FullyQualified) {
|
||||
return ! $this->isName($name, $resolvedName->toString());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function resolvePartialNewName(Name $name, RenamedNamespace $renamedNamespace): string
|
||||
{
|
||||
$nameInNewNamespace = $renamedNamespace->getNameInNewNamespace();
|
||||
|
||||
// first dummy implementation - improve
|
||||
$cutOffFromTheLeft = Strings::length($nameInNewNamespace) - Strings::length($name->toString());
|
||||
|
||||
return Strings::substring($nameInNewNamespace, $cutOffFromTheLeft);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,15 @@ final class RenamedNamespace
|
|||
|
||||
public function getNameInNewNamespace(): string
|
||||
{
|
||||
if ($this->newNamespace === $this->currentName) {
|
||||
return $this->currentName;
|
||||
}
|
||||
|
||||
return str_replace($this->oldNamespace, $this->newNamespace, $this->currentName);
|
||||
}
|
||||
|
||||
public function getNewNamespace(): string
|
||||
{
|
||||
return $this->newNamespace;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user