Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions rules/Renaming/NodeManipulator/ClassRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Renaming\NodeManipulator;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Name;
Expand All @@ -24,21 +25,24 @@
use Rector\NodeTypeResolver\ValueObject\OldToNewType;
use Rector\Renaming\Collector\RenamedNameCollector;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\Util\FileHasher;

final class ClassRenamer
{
/**
* @var array<string, OldToNewType[]>
* @var array<string, string>
*/
private array $oldToNewTypesByCacheKey = [];
private array $oldToNewClasses = [];

/**
* @var OldToNewType[]
*/
private array $oldToNewTypes = [];

public function __construct(
private readonly PhpDocClassRenamer $phpDocClassRenamer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DocBlockClassRenamer $docBlockClassRenamer,
private readonly ReflectionProvider $reflectionProvider,
private readonly FileHasher $fileHasher,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly RenamedNameCollector $renamedNameCollector
) {
Expand All @@ -65,13 +69,16 @@ public function renameNode(Node $node, array $oldToNewClasses, ?Scope $scope): ?
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if ($phpDocInfo instanceof PhpDocInfo) {
$oldToNewTypes = $this->createOldToNewTypes($oldToNewClasses);
$hasPhpDocChanged = $this->refactorPhpDoc($node, $oldToNewTypes, $oldToNewClasses, $phpDocInfo);
// only a real docblock can carry a renamable type; plain comments or none never change here
if ($node->getDocComment() instanceof Doc) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if ($phpDocInfo instanceof PhpDocInfo) {
$oldToNewTypes = $this->createOldToNewTypes($oldToNewClasses);
$hasPhpDocChanged = $this->refactorPhpDoc($node, $oldToNewTypes, $oldToNewClasses, $phpDocInfo);

if ($hasPhpDocChanged) {
return $node;
if ($hasPhpDocChanged) {
return $node;
}
}
}

Expand Down Expand Up @@ -261,11 +268,9 @@ private function isValidClassNameChange(FullyQualified $fullyQualified, ClassRef
*/
private function createOldToNewTypes(array $oldToNewClasses): array
{
$serialized = \serialize($oldToNewClasses);
$cacheKey = $this->fileHasher->hash($serialized);

if (isset($this->oldToNewTypesByCacheKey[$cacheKey])) {
return $this->oldToNewTypesByCacheKey[$cacheKey];
// the rename map is constant during a run, so memoize on the identical array
if ($this->oldToNewClasses === $oldToNewClasses) {
return $this->oldToNewTypes;
}

$oldToNewTypes = [];
Expand All @@ -276,7 +281,8 @@ private function createOldToNewTypes(array $oldToNewClasses): array
$oldToNewTypes[] = new OldToNewType($oldObjectType, $newObjectType);
}

$this->oldToNewTypesByCacheKey[$cacheKey] = $oldToNewTypes;
$this->oldToNewClasses = $oldToNewClasses;
$this->oldToNewTypes = $oldToNewTypes;

return $oldToNewTypes;
}
Expand Down
Loading