mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit fcd1b1aa6bbf59aaebf119a122d4f7aa045e15f1
fcd1b1aa6b
[Performance][Renaming] Use FullyQualified on RenameClassRector (#5272)
This commit is contained in:
parent
b8b986a680
commit
622a13c7e5
|
@ -209,10 +209,6 @@ final class AttributeKey
|
|||
* @var string
|
||||
*/
|
||||
public const IS_ARRAY_IN_ATTRIBUTE = 'is_array_in_attribute';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const IS_NAMESPACE_NAME = 'is_namespace_name';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
|
@ -9,7 +9,6 @@ use PhpParser\Node\Expr\FuncCall;
|
|||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
@ -19,10 +18,6 @@ final class NameNodeVisitor extends NodeVisitorAbstract implements ScopeResolver
|
|||
{
|
||||
public function enterNode(Node $node) : ?Node
|
||||
{
|
||||
if ($node instanceof Namespace_ && $node->name instanceof Name) {
|
||||
$node->name->setAttribute(AttributeKey::IS_NAMESPACE_NAME, \true);
|
||||
return null;
|
||||
}
|
||||
if ($node instanceof UseUse && ($node->type === Use_::TYPE_NORMAL || $node->type === Use_::TYPE_UNKNOWN)) {
|
||||
$node->name->setAttribute(AttributeKey::IS_USEUSE_NAME, \true);
|
||||
return null;
|
||||
|
|
|
@ -78,8 +78,10 @@ final class ClassRenamingPostRector extends \Rector\PostRector\Rector\AbstractPo
|
|||
}
|
||||
/** @var Scope|null $scope */
|
||||
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
$result = $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
|
||||
if (!$result instanceof Name && !$node instanceof FullyQualified) {
|
||||
$result = null;
|
||||
if ($node instanceof FullyQualified) {
|
||||
$result = $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
|
||||
} else {
|
||||
$phpAttributeName = $node->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
|
||||
if (\is_string($phpAttributeName)) {
|
||||
$result = $this->classRenamer->renameNode(new FullyQualified($phpAttributeName, $node->getAttributes()), $oldToNewClasses, $scope);
|
||||
|
|
|
@ -115,7 +115,7 @@ final class ClassRenamer
|
|||
public function renameNode(Node $node, array $oldToNewClasses, ?Scope $scope) : ?Node
|
||||
{
|
||||
$oldToNewTypes = $this->createOldToNewTypes($oldToNewClasses);
|
||||
if ($node instanceof Name) {
|
||||
if ($node instanceof FullyQualified) {
|
||||
return $this->refactorName($node, $oldToNewClasses);
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
|
||||
|
@ -153,9 +153,9 @@ final class ClassRenamer
|
|||
}
|
||||
return \false;
|
||||
}
|
||||
private function shouldSkip(string $newName, Name $name) : bool
|
||||
private function shouldSkip(string $newName, FullyQualified $fullyQualified) : bool
|
||||
{
|
||||
if ($name->getAttribute(AttributeKey::IS_STATICCALL_CLASS_NAME) === \true && $this->reflectionProvider->hasClass($newName)) {
|
||||
if ($fullyQualified->getAttribute(AttributeKey::IS_STATICCALL_CLASS_NAME) === \true && $this->reflectionProvider->hasClass($newName)) {
|
||||
$classReflection = $this->reflectionProvider->getClass($newName);
|
||||
return $classReflection->isInterface();
|
||||
}
|
||||
|
@ -164,26 +164,23 @@ final class ClassRenamer
|
|||
/**
|
||||
* @param array<string, string> $oldToNewClasses
|
||||
*/
|
||||
private function refactorName(Name $name, array $oldToNewClasses) : ?Name
|
||||
private function refactorName(FullyQualified $fullyQualified, array $oldToNewClasses) : ?FullyQualified
|
||||
{
|
||||
if ($name->getAttribute(AttributeKey::IS_NAMESPACE_NAME) === \true) {
|
||||
return null;
|
||||
}
|
||||
$stringName = $this->nodeNameResolver->getName($name);
|
||||
$stringName = $fullyQualified->toString();
|
||||
$newName = $oldToNewClasses[$stringName] ?? null;
|
||||
if ($newName === null) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isClassToInterfaceValidChange($name, $newName)) {
|
||||
if (!$this->isClassToInterfaceValidChange($fullyQualified, $newName)) {
|
||||
return null;
|
||||
}
|
||||
// no need to preslash "use \SomeNamespace" of imported namespace
|
||||
if ($name->getAttribute(AttributeKey::IS_USEUSE_NAME) === \true) {
|
||||
if ($fullyQualified->getAttribute(AttributeKey::IS_USEUSE_NAME) === \true) {
|
||||
// no need to rename imports, they will be handled by autoimport and coding standard
|
||||
// also they might cause some rename
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkip($newName, $name)) {
|
||||
if ($this->shouldSkip($newName, $fullyQualified)) {
|
||||
return null;
|
||||
}
|
||||
$this->renamedNameCollector->add($stringName);
|
||||
|
@ -261,18 +258,18 @@ final class ClassRenamer
|
|||
* - implements SomeInterface
|
||||
* - implements SomeClass
|
||||
*/
|
||||
private function isClassToInterfaceValidChange(Name $name, string $newClassName) : bool
|
||||
private function isClassToInterfaceValidChange(FullyQualified $fullyQualified, string $newClassName) : bool
|
||||
{
|
||||
if (!$this->reflectionProvider->hasClass($newClassName)) {
|
||||
return \true;
|
||||
}
|
||||
$classReflection = $this->reflectionProvider->getClass($newClassName);
|
||||
// ensure new is not with interface
|
||||
if ($name->getAttribute(AttributeKey::IS_NEW_INSTANCE_NAME) !== \true) {
|
||||
return $this->isValidClassNameChange($name, $classReflection);
|
||||
if ($fullyQualified->getAttribute(AttributeKey::IS_NEW_INSTANCE_NAME) !== \true) {
|
||||
return $this->isValidClassNameChange($fullyQualified, $classReflection);
|
||||
}
|
||||
if (!$classReflection->isInterface()) {
|
||||
return $this->isValidClassNameChange($name, $classReflection);
|
||||
return $this->isValidClassNameChange($fullyQualified, $classReflection);
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
|
@ -327,9 +324,9 @@ final class ClassRenamer
|
|||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
});
|
||||
}
|
||||
private function isValidClassNameChange(Name $name, ClassReflection $classReflection) : bool
|
||||
private function isValidClassNameChange(FullyQualified $fullyQualified, ClassReflection $classReflection) : bool
|
||||
{
|
||||
if ($name->getAttribute(AttributeKey::IS_CLASS_EXTENDS) === \true) {
|
||||
if ($fullyQualified->getAttribute(AttributeKey::IS_CLASS_EXTENDS) === \true) {
|
||||
// is class to interface?
|
||||
if ($classReflection->isInterface()) {
|
||||
return \false;
|
||||
|
@ -338,7 +335,7 @@ final class ClassRenamer
|
|||
return \false;
|
||||
}
|
||||
}
|
||||
if ($name->getAttribute(AttributeKey::IS_CLASS_IMPLEMENT) === \true) {
|
||||
if ($fullyQualified->getAttribute(AttributeKey::IS_CLASS_IMPLEMENT) === \true) {
|
||||
// is interface to class?
|
||||
return !$classReflection->isClass();
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace Rector\Renaming\Rector\Name;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Declare_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
|
@ -87,10 +87,10 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Name::class, Property::class, FunctionLike::class, Expression::class, ClassLike::class, Namespace_::class, If_::class];
|
||||
return [FullyQualified::class, Property::class, FunctionLike::class, Expression::class, ClassLike::class, Namespace_::class, If_::class];
|
||||
}
|
||||
/**
|
||||
* @param FunctionLike|Name|ClassLike|Expression|Namespace_|Property|If_ $node
|
||||
* @param FunctionLike|FullyQualified|ClassLike|Expression|Namespace_|Property|If_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'e138f84792e483e8b653dc79271be376bbc1e41d';
|
||||
public const PACKAGE_VERSION = 'fcd1b1aa6bbf59aaebf119a122d4f7aa045e15f1';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-11-22 06:34:49';
|
||||
public const RELEASE_DATE = '2023-11-23 00:38:31';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue