Skip to content

Commit cbc2e89

Browse files
committed
Updated Rector to commit 0866c49d632b44ad60f1176c2180a0664943d6f1
rectorphp/rector-src@0866c49 [Renaming] [Symfony 7.0] Skip ClassConstFetch that rename its ->class to Interface that constant not exists in new interface (#6966)
1 parent 15f1720 commit cbc2e89

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

rules/Renaming/NodeManipulator/ClassRenamer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockClassRenamer;
2222
use Rector\NodeTypeResolver\ValueObject\OldToNewType;
2323
use Rector\Renaming\Collector\RenamedNameCollector;
24+
use Rector\Renaming\Rector\Name\RenameClassRector;
2425
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
2526
use Rector\Util\FileHasher;
2627
final class ClassRenamer
@@ -73,6 +74,9 @@ public function __construct(PhpDocClassRenamer $phpDocClassRenamer, PhpDocInfoFa
7374
*/
7475
public function renameNode(Node $node, array $oldToNewClasses, ?Scope $scope) : ?Node
7576
{
77+
if ($node instanceof FullyQualified && $node->getAttribute(RenameClassRector::SKIPPED_AS_CLASS_CONST_FETCH_CLASS) === \true) {
78+
return null;
79+
}
7680
$oldToNewTypes = $this->createOldToNewTypes($oldToNewClasses);
7781
if ($node instanceof FullyQualified) {
7882
return $this->refactorName($node, $oldToNewClasses);

rules/Renaming/Rector/Name/RenameClassRector.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
declare (strict_types=1);
44
namespace Rector\Renaming\Rector\Name;
55

6+
use PHPStan\Reflection\ReflectionProvider;
7+
use PhpParser\Node\Identifier;
68
use PhpParser\Node;
9+
use PhpParser\Node\Expr\ClassConstFetch;
710
use PhpParser\Node\FunctionLike;
811
use PhpParser\Node\Name\FullyQualified;
912
use PhpParser\Node\Stmt\ClassLike;
1013
use PhpParser\Node\Stmt\Expression;
1114
use PhpParser\Node\Stmt\If_;
1215
use PhpParser\Node\Stmt\Property;
16+
use PhpParser\NodeVisitor;
1317
use Rector\Configuration\RenamedClassesDataCollector;
1418
use Rector\Contract\Rector\ConfigurableRectorInterface;
1519
use Rector\NodeTypeResolver\Node\AttributeKey;
@@ -31,10 +35,19 @@ final class RenameClassRector extends AbstractRector implements ConfigurableRect
3135
* @readonly
3236
*/
3337
private ClassRenamer $classRenamer;
34-
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ClassRenamer $classRenamer)
38+
/**
39+
* @readonly
40+
*/
41+
private ReflectionProvider $reflectionProvider;
42+
/**
43+
* @var string
44+
*/
45+
public const SKIPPED_AS_CLASS_CONST_FETCH_CLASS = 'skipped_as_class_const_fetch_class';
46+
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ClassRenamer $classRenamer, ReflectionProvider $reflectionProvider)
3547
{
3648
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
3749
$this->classRenamer = $classRenamer;
50+
$this->reflectionProvider = $reflectionProvider;
3851
}
3952
public function getRuleDefinition() : RuleDefinition
4053
{
@@ -69,14 +82,37 @@ function someFunction(SomeNewClass $someOldClass): SomeNewClass
6982
*/
7083
public function getNodeTypes() : array
7184
{
72-
return [FullyQualified::class, Property::class, FunctionLike::class, Expression::class, ClassLike::class, If_::class];
85+
return [ClassConstFetch::class, FullyQualified::class, Property::class, FunctionLike::class, Expression::class, ClassLike::class, If_::class];
7386
}
7487
/**
75-
* @param FunctionLike|FullyQualified|ClassLike|Expression|Property|If_ $node
88+
* @param ClassConstFetch|FunctionLike|FullyQualified|ClassLike|Expression|Property|If_ $node
89+
* @return int|null|\PhpParser\Node
7690
*/
77-
public function refactor(Node $node) : ?Node
91+
public function refactor(Node $node)
7892
{
7993
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
94+
if ($node instanceof ClassConstFetch) {
95+
if ($node->class instanceof FullyQualified && $node->name instanceof Identifier && $this->reflectionProvider->hasClass($node->class->toString())) {
96+
foreach ($oldToNewClasses as $oldClass => $newClass) {
97+
if ($this->isName($node->class, $oldClass) && $this->reflectionProvider->hasClass($newClass)) {
98+
$classReflection = $this->reflectionProvider->getClass($newClass);
99+
if (!$classReflection->isInterface()) {
100+
continue;
101+
}
102+
$oldClassReflection = $this->reflectionProvider->getClass($oldClass);
103+
if ($oldClassReflection->hasConstant($node->name->toString()) && !$classReflection->hasConstant($node->name->toString())) {
104+
// used by ClassRenamer::renameNode()
105+
// that called on ClassRenamingPostRector PostRector
106+
$node->class->setAttribute(self::SKIPPED_AS_CLASS_CONST_FETCH_CLASS, \true);
107+
// no constant found on new interface? skip node below ClassConstFetch on this rule
108+
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
109+
}
110+
}
111+
}
112+
}
113+
// continue to next FullyQualified usage
114+
return null;
115+
}
80116
if ($oldToNewClasses !== []) {
81117
$scope = $node->getAttribute(AttributeKey::SCOPE);
82118
return $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = 'da52751a6d28053f1dfa87abe1645f2cbdc262be';
22+
public const PACKAGE_VERSION = '0866c49d632b44ad60f1176c2180a0664943d6f1';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-06-09 17:02:53';
27+
public const RELEASE_DATE = '2025-06-09 18:23:51';
2828
/**
2929
* @var int
3030
*/

0 commit comments

Comments
 (0)