1616use Rector \Enum \ObjectReference ;
1717use Rector \NodeAnalyzer \ClassAnalyzer ;
1818use Rector \NodeManipulator \ClassMethodManipulator ;
19+ use Rector \PhpParser \AstResolver ;
1920use Rector \Rector \AbstractRector ;
2021use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2122use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
@@ -28,7 +29,8 @@ final class RemoveParentCallWithoutParentRector extends AbstractRector
2829 public function __construct (
2930 private readonly ClassMethodManipulator $ classMethodManipulator ,
3031 private readonly ClassAnalyzer $ classAnalyzer ,
31- private readonly ReflectionProvider $ reflectionProvider
32+ private readonly ReflectionProvider $ reflectionProvider ,
33+ private readonly AstResolver $ astResolver
3234 ) {
3335 }
3436
@@ -160,6 +162,36 @@ private function doesCalledMethodExistInParent(StaticCall $staticCall, Class_ $c
160162 return false ;
161163 }
162164
163- return $ this ->classMethodManipulator ->hasParentMethodOrInterfaceMethod ($ class , $ calledMethodName );
165+ if ($ this ->classMethodManipulator ->hasParentMethodOrInterfaceMethod ($ class , $ calledMethodName )) {
166+ return true ;
167+ }
168+
169+ // the called method may be defined in an ancestor that cannot be resolved
170+ // (e.g. a grandparent class is not autoloadable); in that case we cannot
171+ // safely tell the method does not exist, so the call must not be removed
172+ return $ this ->hasUnresolvableAncestor ($ class ->extends );
173+ }
174+
175+ private function hasUnresolvableAncestor (Name $ parentName ): bool
176+ {
177+ $ parentClassName = $ this ->getName ($ parentName );
178+ if (! is_string ($ parentClassName )) {
179+ return false ;
180+ }
181+
182+ if (! $ this ->reflectionProvider ->hasClass ($ parentClassName )) {
183+ return true ;
184+ }
185+
186+ $ parentClass = $ this ->astResolver ->resolveClassFromName ($ parentClassName );
187+ if (! $ parentClass instanceof Class_) {
188+ return false ;
189+ }
190+
191+ if (! $ parentClass ->extends instanceof Name) {
192+ return false ;
193+ }
194+
195+ return $ this ->hasUnresolvableAncestor ($ parentClass ->extends );
164196 }
165197}
0 commit comments