Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,6 @@ public function changeUnderscoreType(Node $node, string $namespacePrefix, ?array
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
}

public function resetImportedNames(): void
{
$this->importedNames = [];
}

/**
* For better performance
*/
Expand Down
39 changes: 39 additions & 0 deletions src/Rector/Class_/RenameClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rector\Rector\Class_;

use Nette\Utils\Strings;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\FunctionLike;
Expand Down Expand Up @@ -121,6 +123,8 @@ public function refactor(Node $node): ?Node
}
}

$this->changeTypeInAnnotationTypes($node);

if ($node instanceof Name) {
return $this->refactorName($node);
}
Expand Down Expand Up @@ -297,4 +301,39 @@ private function refactorName(Node $node): ?Name

return new FullyQualified($newName);
}

/**
* Covers annotations like @ORM, @Serializer, @Assert etc
* See https://github.com/rectorphp/rector/issues/1872
*/
private function changeTypeInAnnotationTypes(Node $node): void
{
$docComment = $node->getDocComment();
if ($docComment === null) {
return;
}

$textDocComment = $docComment->getText();

$oldTypes = array_keys($this->oldToNewClasses);

$oldTypesPregQuoted = [];
foreach ($oldTypes as $oldType) {
$oldTypesPregQuoted[] = '\b' . preg_quote($oldType) . '\b';
}

$oldTypesPattern = '#(?|' . implode('|', $oldTypesPregQuoted) . ')#x';

$match = Strings::match($textDocComment, $oldTypesPattern);
if ($match === null) {
return;
}

foreach ($match as $matchedOldType) {
$newType = $this->oldToNewClasses[$matchedOldType];
$textDocComment = Strings::replace($textDocComment, '#\b' . preg_quote($matchedOldType) . '\b#', $newType);
}

$node->setDocComment(new Doc($textDocComment));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Rector\Tests\Rector\Class_\RenameClassRector\Fixture;

use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation as Serializer;
use Doctrine\ORM\Mapping as ORM;

class ClassAnnotations
{
/**
* @Assert\Choice(callback={"Rector\Tests\Rector\Class_\RenameClassRector\Source\OldClass", "getValues"}, strict=true)
*/
public $name;

/**
* @Serializer\Type("Rector\Tests\Rector\Class_\RenameClassRector\Source\OldClass")
*/
public $time;

/**
* @Serializer\Type("Rector\Tests\Rector\Class_\RenameClassRector\Source\OldClassy")
*/
public $keepThis;

/**
* @Serializer\Type("array<Rector\Tests\Rector\Class_\RenameClassRector\Source\OldClass>")
* @Serializer\Type("iterable<key, Rector\Tests\Rector\Class_\RenameClassRector\Source\OldClass>")
*/
public $flights = [];

/**
* @ORM\OneToMany(targetEntity="Rector\Tests\Rector\Class_\RenameClassRector\Source\OldClass")
*/
public $entityProperty;
}

?>
-----
<?php

namespace Rector\Tests\Rector\Class_\RenameClassRector\Fixture;

use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation as Serializer;
use Doctrine\ORM\Mapping as ORM;

class ClassAnnotations
{
/**
* @Assert\Choice(callback={"Rector\Tests\Rector\Class_\RenameClassRector\Source\NewClass", "getValues"}, strict=true)
*/
public $name;

/**
* @Serializer\Type("Rector\Tests\Rector\Class_\RenameClassRector\Source\NewClass")
*/
public $time;

/**
* @Serializer\Type("Rector\Tests\Rector\Class_\RenameClassRector\Source\OldClassy")
*/
public $keepThis;

/**
* @Serializer\Type("array<Rector\Tests\Rector\Class_\RenameClassRector\Source\NewClass>")
* @Serializer\Type("iterable<key, Rector\Tests\Rector\Class_\RenameClassRector\Source\NewClass>")
*/
public $flights = [];

/**
* @ORM\OneToMany(targetEntity="Rector\Tests\Rector\Class_\RenameClassRector\Source\NewClass")
*/
public $entityProperty;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function provideTestFiles(): Iterator
yield [__DIR__ . '/Fixture/rename_trait.php.inc'];
yield [__DIR__ . '/Fixture/rename_class_without_namespace_to_class_without_namespace.php.inc'];
yield [__DIR__ . '/Fixture/rename_class_to_class_without_namespace.php.inc'];

// Symfony/Validator + Doctrine + JMS/Serializer annotations
yield [__DIR__ . '/Fixture/class_annotations.php.inc'];
}

/**
Expand Down