Skip code changes on unresolvable/unknown classes#4619
Skip code changes on unresolvable/unknown classes#4619samsonasik merged 7 commits intorectorphp:mainfrom
Conversation
|
|
||
| class SkipUnknownParent extends UnknownParentClass | ||
| { | ||
| public function go($item) |
There was a problem hiding this comment.
if we don't know the parent class (because e.g. its not autoloadable), we should be pessimistic and do no changes
|
throw Exception to be validated in the rules can be the way to go: try {
parentGuard->...
} catch (ClassParentNotAtoloadedException) {
} |
|
thanks for the suggestion. I have a different idea.. let me show it to you :) |
|
I expect some delay until Roave/BetterReflection#1359 will be implemented in PHPStan. therefore I implemented the suggested workaround for now |
|
@samsonasik should be good to go |
| } | ||
|
|
||
| public function hasParentClassMethod(ClassMethod $classMethod): bool | ||
| public function hasParentClassMethod(ClassMethod $classMethod): ?bool |
There was a problem hiding this comment.
this method now returns true/false when we can make a final decision or null when classes involved are not known and we can't judge
| return $parentClassMethod instanceof MethodReflection; | ||
| } catch (UnresolvableClassException) { | ||
| // we don't know all involved parents. | ||
| return null; |
There was a problem hiding this comment.
just return true here, so it "marked" as has parent so bool can be returned.
There was a problem hiding this comment.
I don't think its a good idea to handle "we don't know" the same as "sure, the method exists".
for our callers, as they exist right now, this is acceptable, but it might suprisingly break in the future as soon as someone compares the result of this method against true .
callers might handle the situation differently, depending on the method exists or not.
There was a problem hiding this comment.
I prefer to create separate service for param guard handling, eg: ClassMethodParamTypeOverrideGuard.
Something like ClassMethodReturnTypeOverrideGuard::shouldSkipClassMethod() do
There was a problem hiding this comment.
@staabm just checked the getParentClassMethod() usages in rules, there are various usage directly, which cause exception, I think try catch should be in consumer instead of in hasParentClassMethod() as that cause double call.
I will create new PR for that.
There was a problem hiding this comment.
hm.., the service is named ParentClassMethodTypeOverrideGuard, which has vs get should only validate instead of getting in real use case, so I prefer return bool true for hasParentClassMethod(), and return null for getParentClassMethod() when no parent at all.
There was a problem hiding this comment.
handling UnresolvableClassException at the caller site could indeed improve the current situation 👍
There was a problem hiding this comment.
I create PR
return bool only should be the way to go as it only validate, not a "real" verification, we then return ?MethodReflection for getParentClassMethod()
samsonasik
left a comment
There was a problem hiding this comment.
@TomasVotruba let's merge it so we can improve in separate PR, thank you @staabm
adding test cases which show the bug described in rectorphp/rector#8093