-
Notifications
You must be signed in to change notification settings - Fork 546
Implement @phpstan-consistent-templates
#1289
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
Conversation
a47cfdc to
a43efe4
Compare
|
On second thought maybe it should be it's own rule, with in the bleeding edge. Because Psalm version of the tag is already in use. I'll change it. |
|
I think it's not necessary, it's going to be really rare. I've also enabled conditional return types related rules for everyone :) Some ideas (I haven't seen the code yet):
|
Yes, that's the goal from phpstan/phpstan#5512 I was planning to do that in another PR. But sure it can be part of this one.
If the logic will be in |
|
Yes, I often add a boolean parameter in bleedingEdge that changes behaviour of existing rules 😊 But if you find the implementation inelegant, certainly feel free to implement it in a separate rule. For one, it only applies to methods in classes and not functions, so IncompatiblePhpDocTypeRule might not be great after all. |
a43efe4 to
756da6c
Compare
|
I added a commit to correctly infer I'll implement the second part about the |
|
Some ideas about validating
|
|
It's funny a little because one of the places where |
|
The current problem I'm having is that |
|
@canvural Yeah, I realized that we might need a new Same as GenericObjectType extends ObjectType, GenericStaticObjectType should extend StaticType. It's also useful because we should be able to validate that
The hardest part is going to be to figure out the correct relationships between ObjectType, StaticType, GenericObjectType, GenericStaticType when asked about |
|
Before PHPStan 1.7.0 is out I'll take a look at the current state of this PR and decide what we can cut from my demands to make it part of that release :) |
|
@ondrejmirtes I actually implemented all the rule changes and |
|
Looking forward to it 😊 |
9ebf9e0 to
0755bb6
Compare
|
@ondrejmirtes I pushed the changes. I did the rule changes from here. But did not implement Other than that, I guess this is ready for a review. |
|
@canvural It describes the relationship between types. The best way to test it is through type normalization (https://phpstan.org/developing-extensions/type-system#type-normalization) which means to add test cases in In case of intersection, the more specific type wins and in case of incompatible types, the result is NeverType. I'd expect GenericStaticObjectType to be tested against ObjectWithoutClassType, ObjectType (compatible class), ObjectType (incompatible class), StaticType, GenericObjectType, and GenericStaticObjectType. |
|
@ondrejmirtes Thank you for the detailed explanation. But I already knew all that. I can implement that, and add the tests. But my question was more specific to in which scenarios the All that said, I'll probably continue to work on this next week. Because tomorrow morning I'm leaving for the phpday! I'll make sure to say hi to you! 😊 |
|
Oh wow, I'm looking forward to that a lot! I kind of hoped to see more familiar faces aside from other speakers so it's great my wishes are gonna be fulfilled 😊 |
|
And about your question:
You're in a class method like and have code like this: if (rand(0, 1)) {
$a = $this->doFoo(); // returns static<T, U>
} else {
$a = $this->doBar(); // returns static<Foo, Bar>
}
// $a is union of $a from if+else branchesGenericStaticObjectType remains GenericStaticObjectType here but we need to union the types from if and else. Of course you can come up with other (basically an infinite number) of examples. The relationship between GenericStaticObjectType and StaticType is the same as GenericObjectType and ObjectType, so you can draw some inspiration from there. Also it just occured to me that |
|
PHPStan 1.7.0 is on its way to be released, I think we can put this one even in 1.7.x or maybe in 1.8.0 depending on when you find time to finish this :) Thanks. |
|
It's fine by me. It's not urgent to get this merged. But I guess I'll spend some time on this this week. Then we'll see. |
0755bb6 to
0bfdbd8
Compare
0bfdbd8 to
83d885a
Compare
746dc97 to
46c2aa7
Compare
46c2aa7 to
9a7e97d
Compare
|
I can provide another example if needed: https://phpstan.org/r/5b601b92-3518-4f51-9de6-0387b28ebfd5 as discussed in phpstan/phpstan#9975 |
2728beb to
da6021f
Compare
da6021f to
4995f1e
Compare
|
Just bumped into this issue with my generic collection class as well. |
|
any hope for making it into 2.x? |
|
Superseded by: 0c1f22a I realized we don't need to enforce I couldn't have done it without your PR, it saved me a lot of time figuring out how the |
This PR adds a new tag
@phpstan-consistent-templatesand new checks for it. Fixes one part of phpstan/phpstan#5512There are some parts I am not sure of.
hasConsistentTemplatesEverything else, likeinternal,final, has naming withisXX. ButisConsistentTemplatesdid not make sense to me.@extendsor@implementstag. Justclass Foo extends Barwill not produce these errors evenBarhas@phpstan-consistent-templatesLastly, there is this edge case.
Basically if a class both extends and implements something, and if those parent classes have different number of template types, the child class can never satisfy both.