mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 03:35:01 +01:00
NamespaceReplaceRector: add fqn class statement
This commit is contained in:
parent
423c9c56d9
commit
c4aad2e3b1
@ -4,6 +4,7 @@ namespace Rector\Rector\Dynamic;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
@ -33,8 +34,11 @@ final class NamespaceReplacerRector extends AbstractRector
|
||||
}
|
||||
|
||||
$name = $this->resolveNameFromNode($node);
|
||||
if (! $this->isNamespaceToChange($name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isNamespaceToChange($name);
|
||||
return ! $this->isClassFullyQualifiedName($node);
|
||||
}
|
||||
|
||||
public function refactor(Node $node): ?Node
|
||||
@ -122,4 +126,29 @@ final class NamespaceReplacerRector extends AbstractRector
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for "new \ClassNoNamespace;"
|
||||
* This should be skipped, not a namespace.
|
||||
*/
|
||||
private function isClassFullyQualifiedName(Node $node): bool
|
||||
{
|
||||
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
|
||||
if ($parentNode === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $parentNode instanceof New_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$newClassName = $parentNode->class->toString();
|
||||
foreach ($this->oldToNewNamespaces as $oldNamespace => $newNamespace) {
|
||||
if ($newClassName === $oldNamespace) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ class SomeClass
|
||||
{
|
||||
public function someClass()
|
||||
{
|
||||
$keepThis = new \OldNamespace;
|
||||
|
||||
return new \NewNamespace\SomeClass;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ class SomeClass
|
||||
{
|
||||
public function someClass()
|
||||
{
|
||||
$keepThis = new \OldNamespace;
|
||||
|
||||
return new \OldNamespace\SomeClass;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user