-
-
Notifications
You must be signed in to change notification settings - Fork 671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GetClassToInstanceOfRector invalid fix to instanceof for strict type check #8639
Comments
Seems like |
@TomasVotruba @jeremyVignelles I am thinking that this rule should only cover <?php
class A {}
class B extends A {}
$c = new B();
-if (get_class($c) !== A::class) {
+if (! $c instanceof A) {
} wdyt? |
Oh, the result seems different https://3v4l.org/7cbU6 |
If the type is exactly of what it defined (sub type), it can be checked by something like: $varType = $this->nodeTypeResolver->getNativeType($varNode);
$objectType = new ObjectType($className);
if ($varType instanceof ObjectType
&& ! $this->typeComparator->areTypesEqual($varType, $objectType)
&& $this->typeComparator->isSubtype($varType, $objectType)) {
return null;
} some verification on dynamic variable may be needed. I guess for your use case, you can skip the rule: https://getrector.com/documentation/ignoring-rules-or-paths as it may be usefull to improve type check, but on some very specific use case, it may cause a bug. |
Bug Report
I'm using
getclass($object) === A::class
to test if my variable is exactly of type A (and not a subclass).Rector tries to edit this into an instanceof check, due to the
GetClassToInstanceOfRector
ruleMinimal PHP Code Causing Issue
Code gets "fixed" to
$c instanceof \A
Expected Behaviour
I should be able to express that I am willing to keep that get_class call to check that exact type.
The text was updated successfully, but these errors were encountered: