Fixed '$this instanceof X will always be false' in traits#2045
Fixed '$this instanceof X will always be false' in traits#2045ondrejmirtes merged 5 commits intophpstan:1.9.xfrom
Conversation
45f4b29 to
e5ebe9d
Compare
c6c3cfc to
d052ca0
Compare
|
I have approached the problem from a few different angles. All worked more or less. At the end I get the feeling maybe this should be fixed at the core by e.g. treating |
d052ca0 to
d75abea
Compare
|
regarding the build: I can see the same errors in other PRs so I think these are unrelated |
|
This pull request has been marked as ready for review. |
ondrejmirtes
left a comment
There was a problem hiding this comment.
The underyling issues we need to fix is: https://phpstan.org/r/d8b183e1-7099-478e-af58-fdd5e8ab7d21
So it's not sufficient to try to if-else our way out of this problem in a single rule, we need to fix the typesystem here.
So something needs to happen to handling of Instanceof_ in TypeSpecifier.
9b059b0 to
e571a81
Compare
that was a great hint, thank you. I feel dumb for searching the solution in a lot of other phpstan components, but not the TypeSpecifier :) |
8389601 to
5e8963b
Compare
|
I decided this still doesn't work and we need a different approach. ThisType in a trait context needs to behave a bit differently than in a class. It needs to allow intersection with other classes, almost like an interface. |
|
So the bottom line is, that we need a new type? |
|
Some boolean arg in ThisType would suffice. But maybe TraitThisType would work too. |
f2180b4 to
9f41729
Compare
9f41729 to
e8b28a3
Compare
e8b28a3 to
9fc0b5a
Compare
9fc0b5a to
0e92cb2
Compare
ondrejmirtes
left a comment
There was a problem hiding this comment.
I see some problems with this PR.
- TypeSpecifier shouldn't be touched. Instead, MutatingScope::enterTrait should handle ThisType.
- Any changes to isSuperTypeOf logic needs to be heavily covered by tests in TypeCombinatorTest - in dataUnion and dataIntersection.
- There are some cases the method should return
no: a) for a final class that does not use the trait. b) For an ObjectType with a trait name c) For a type that's already subtracted in ThisType. This influences how unions and intersections are gonna end up looking like.
041c226 to
e53cd2d
Compare
There was a problem hiding this comment.
for some reason this error is not reported. I get a error though, when running phpstan on this file in isolation.
otherwise I think this looks fine. I will add new TypeCombinator Tests now
There was a problem hiding this comment.
That's ExistingClassInInstanceOfRule rule, not ImpossibleInstanceOfRule rule.
695c257 to
79bf7ef
Compare
|
This pull request has been marked as ready for review. |
a2d4993 to
1c526dc
Compare
1c526dc to
005b6f3
Compare
src/Analyser/MutatingScope.php
Outdated
| $this->expressionTypes, | ||
| $this->nativeExpressionTypes, | ||
| array_merge($this->expressionTypes, [ | ||
| '$this' => $thisHolder, |
There was a problem hiding this comment.
No need for array_merge. Simply do this before:
$expressionTypes = $this->expressionTypes;
$expressionType['$this'] = $thisHolder;And the same thing for nativeExpressionTypes.
There was a problem hiding this comment.
I see. I was inspired by another place where it was done this way. fixed both in 749fbab
There was a problem hiding this comment.
That's ExistingClassInInstanceOfRule rule, not ImpossibleInstanceOfRule rule.
|
Thank you. |
|
thanks for the directions. I couldn't have finished this one without your feedback <3. |
closes phpstan/phpstan#3632
test fails without the fix