mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-27 21:23:31 +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',
|
'Old\Long\AnyNamespace' => 'Short\AnyNamespace',
|
||||||
'PHPUnit_Framework_' => 'PHPUnit\Framework\\',
|
'PHPUnit_Framework_' => 'PHPUnit\Framework\\',
|
||||||
'Foo\Bar' => 'Foo\Tmp',
|
'Foo\Bar' => 'Foo\Tmp',
|
||||||
|
'App\Repositories' => 'App\Repositories\Example',
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rector\Renaming\Rector\Namespace_;
|
namespace Rector\Renaming\Rector\Namespace_;
|
||||||
|
|
||||||
use Nette\Utils\Strings;
|
|
||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Expr\New_;
|
use PhpParser\Node\Expr\New_;
|
||||||
use PhpParser\Node\Name;
|
use PhpParser\Node\Name;
|
||||||
|
@ -134,18 +133,19 @@ final class RenameNamespaceRector extends AbstractRector implements Configurable
|
||||||
|
|
||||||
private function processFullyQualified(Name $name, RenamedNamespace $renamedNamespace): ?FullyQualified
|
private function processFullyQualified(Name $name, RenamedNamespace $renamedNamespace): ?FullyQualified
|
||||||
{
|
{
|
||||||
$newName = $this->isPartialNamespace($name) ? $this->resolvePartialNewName(
|
if (str_starts_with($name->toString(), $renamedNamespace->getNewNamespace() . '\\')) {
|
||||||
$name,
|
return null;
|
||||||
$renamedNamespace
|
|
||||||
) : $renamedNamespace->getNameInNewNamespace();
|
|
||||||
|
|
||||||
$values = array_values($this->oldToNewNamespaces);
|
|
||||||
if (! isset($this->isChangedInNamespaces[$newName])) {
|
|
||||||
return new FullyQualified($newName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! in_array($newName, $values, true)) {
|
$nameInNewNamespace = $renamedNamespace->getNameInNewNamespace();
|
||||||
return new FullyQualified($newName);
|
|
||||||
|
$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;
|
return null;
|
||||||
|
@ -173,28 +173,4 @@ final class RenameNamespaceRector extends AbstractRector implements Configurable
|
||||||
|
|
||||||
return array_key_exists($newClassName, $this->oldToNewNamespaces);
|
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
|
public function getNameInNewNamespace(): string
|
||||||
{
|
{
|
||||||
|
if ($this->newNamespace === $this->currentName) {
|
||||||
|
return $this->currentName;
|
||||||
|
}
|
||||||
|
|
||||||
return str_replace($this->oldNamespace, $this->newNamespace, $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