Skip to content

Commit

Permalink
[Renaming] Fix duplicate namespacing on RenameNamespaceRector (#1759)
Browse files Browse the repository at this point in the history
Co-authored-by: Harings Rob <haringsrob@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Feb 2, 2022
1 parent 2468277 commit 8320d29
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

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

namespace App\Repositories\Example;

class BlogRepository {
}

function index()
{
\App\Repositories\Example\BlogRepository::class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
'Old\Long\AnyNamespace' => 'Short\AnyNamespace',
'PHPUnit_Framework_' => 'PHPUnit\Framework\\',
'Foo\Bar' => 'Foo\Tmp',
'App\Repositories' => 'App\Repositories\Example',
]);
};
42 changes: 9 additions & 33 deletions rules/Renaming/Rector/Namespace_/RenameNamespaceRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Renaming\Rector\Namespace_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -134,18 +133,19 @@ public function configure(array $configuration): void

private function processFullyQualified(Name $name, RenamedNamespace $renamedNamespace): ?FullyQualified
{
$newName = $this->isPartialNamespace($name) ? $this->resolvePartialNewName(
$name,
$renamedNamespace
) : $renamedNamespace->getNameInNewNamespace();
if (str_starts_with($name->toString(), $renamedNamespace->getNewNamespace() . '\\')) {
return null;
}

$nameInNewNamespace = $renamedNamespace->getNameInNewNamespace();

$values = array_values($this->oldToNewNamespaces);
if (! isset($this->isChangedInNamespaces[$newName])) {
return new FullyQualified($newName);
if (! isset($this->isChangedInNamespaces[$nameInNewNamespace])) {
return new FullyQualified($nameInNewNamespace);
}

if (! in_array($newName, $values, true)) {
return new FullyQualified($newName);
if (! in_array($nameInNewNamespace, $values, true)) {
return new FullyQualified($nameInNewNamespace);
}

return null;
Expand Down Expand Up @@ -173,28 +173,4 @@ private function isClassFullyQualifiedName(Node $node): bool

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);
}
}
9 changes: 9 additions & 0 deletions rules/Renaming/ValueObject/RenamedNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public function __construct(

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;
}
}

0 comments on commit 8320d29

Please sign in to comment.