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
Allow undefined property fetch of ObjectWithoutClassType in Coalesce #1223
Conversation
Failing tests are unrelated to this pull request. I believe it’s related to 58d7df3 |
This feels very hacky. What about asking that Type::hasProperty() is not |
Thank you for your comment.
I think checking by hasProperty would make allowing dynamic properties. What this PR is trying is to solve is to allow undefined property fetches with $obj = returnsObj();
$val = $obj->prop ?? "foo"; // obj maybe hasProperty
$this->dynamicProp ?? "foo"; // $this maybe hasProperty if object class is not final
$this->dynamicPropTypo ?? "foo"; // want to disallow dynamic properties to catch these typoes The difference is, you have no prop information anyway for Maybe object shape feature phpstan/phpstan#6892 is a better way to go for phpstan/phpstan#6026, but this still cant solve phpstan/phpstan#3659 nor use cases with |
Alright, I changed my mind about this, we really need to clean this up. I'm fine with any changes as long as they remove false positives and do not introduce new errors. We need to make But I see two possible opportunities here:
Currently I'd also say that this piece of code in if ($classReflection->isFinal()) {
return TrinaryLogic::createNo();
} Because the possibility of property existence isn't about subclasses, it's about dynamic properties which can be assigned to final classes too. The only situation where |
Okay, thanks for your decision. I'll fix the incompatibility with |
We need a little more fix to solve issues like phpstan/phpstan#3171 (there are some couple more related issues). Also, I have an idea with
using |
Thank you very much! Can you please add a |
fixes phpstan/phpstan#3659
fixes phpstan/phpstan#6026
fixes phpstan/phpstan#6809
fixes phpstan/phpstan#4559
This PR relaxes the condition ofUndefinedExpressionAllowed
forCoalesce
slightly, to allow undefined property fetch ofObjectWithoutClassType
inCoalesce
.This can be useful in cases likewhich currently reports an error https://phpstan.org/r/fbf356da-5a24-43a3-a5a2-1638afa9dd99As seen in https://github.com/rajyan/phpstan-src/blob/c6b6aa702554e5ff14591bae130f842ebd51357e/tests/PHPStan/Rules/Properties/data/dynamic-properties.php#L7-L18 the current behavior for dynamic properties inCoalesce
is kept as is.Changed to fix by making coalesce and isset consistent.