mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 03:35:01 +01:00
87 lines
3.0 KiB
PHP
87 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\BetterPhpDocParser\PhpDocManipulator;
|
|
|
|
use Rector\BetterPhpDocParser\Contract\Doctrine\DoctrineRelationTagValueNodeInterface;
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\JMS\SerializerTypeTagValueNode;
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Symfony\Validator\Constraints\AssertChoiceTagValueNode;
|
|
|
|
final class PhpDocClassRenamer
|
|
{
|
|
/**
|
|
* Covers annotations like @ORM, @Serializer, @Assert etc
|
|
* See https://github.com/rectorphp/rector/issues/1872
|
|
*
|
|
* @param string[] $oldToNewClasses
|
|
*/
|
|
public function changeTypeInAnnotationTypes(PhpDocInfo $phpDocInfo, array $oldToNewClasses): void
|
|
{
|
|
$this->processAssertChoiceTagValueNode($oldToNewClasses, $phpDocInfo);
|
|
$this->processDoctrineRelationTagValueNode($oldToNewClasses, $phpDocInfo);
|
|
$this->processSerializerTypeTagValueNode($oldToNewClasses, $phpDocInfo);
|
|
}
|
|
|
|
/**
|
|
* @param string[] $oldToNewClasses
|
|
*/
|
|
private function processAssertChoiceTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo): void
|
|
{
|
|
$assertChoiceTagValueNode = $phpDocInfo->getByType(AssertChoiceTagValueNode::class);
|
|
if (! $assertChoiceTagValueNode instanceof AssertChoiceTagValueNode) {
|
|
return;
|
|
}
|
|
|
|
foreach ($oldToNewClasses as $oldClass => $newClass) {
|
|
if (! $assertChoiceTagValueNode->isCallbackClass($oldClass)) {
|
|
continue;
|
|
}
|
|
|
|
$assertChoiceTagValueNode->changeCallbackClass($newClass);
|
|
$phpDocInfo->markAsChanged();
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string[] $oldToNewClasses
|
|
*/
|
|
private function processDoctrineRelationTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo): void
|
|
{
|
|
$doctrineRelationTagValueNode = $phpDocInfo->getByType(DoctrineRelationTagValueNodeInterface::class);
|
|
if (! $doctrineRelationTagValueNode instanceof DoctrineRelationTagValueNodeInterface) {
|
|
return;
|
|
}
|
|
|
|
foreach ($oldToNewClasses as $oldClass => $newClass) {
|
|
if ($doctrineRelationTagValueNode->getFullyQualifiedTargetEntity() !== $oldClass) {
|
|
continue;
|
|
}
|
|
|
|
$doctrineRelationTagValueNode->changeTargetEntity($newClass);
|
|
$phpDocInfo->markAsChanged();
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string[] $oldToNewClasses
|
|
*/
|
|
private function processSerializerTypeTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo): void
|
|
{
|
|
$serializerTypeTagValueNode = $phpDocInfo->getByType(SerializerTypeTagValueNode::class);
|
|
if (! $serializerTypeTagValueNode instanceof SerializerTypeTagValueNode) {
|
|
return;
|
|
}
|
|
|
|
foreach ($oldToNewClasses as $oldClass => $newClass) {
|
|
$hasReplaced = $serializerTypeTagValueNode->replaceName($oldClass, $newClass);
|
|
if ($hasReplaced) {
|
|
$phpDocInfo->markAsChanged();
|
|
}
|
|
}
|
|
}
|
|
}
|