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
Class level template on static method ignored? #10513
Comments
Class-level When you're calling an instance method, you're doing so on an instance of type But when you're calling a static method, you're calling But I think the signature in your code snippet is just misunderstanding, and this is what you wanted to to do instead - introduce a method-level |
While I understand the argument for not supporting it, then it also shouldn't read it. I.e.: the original snippet should result in an error that type And for what it's worth, in C# class level generic types can be used by static methods as well, but the type needs to be explicitly named, so |
We've discussed it and decided against it for now. See: phpstan/phpstan-src#2075 |
One more link phpstan/phpstan-src#2232 |
Just checking, how do these tickets relate? Is that in response to possibly supporting class level template types? Or to (at least) error that an undefined / unknown (class) type name is used? I think the second one, phpstan/phpstan-src#2232, would change it such that an error is reported that |
The linked pull requests are about this exact topic. What we realize in the discussions there is we can't stop taking class-level |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Bug report
It seems like class level template declarations are ignored on static methods. See the linked snippet for an example. I would expect the dumped type to be
GenericItem<TestItem>
(based on the type of the$item
argument), while it just uses the parameter type? Template constraint type? (i.e.ItemInterface
as generic type)Testing the same snippet with Psalm does show there is an error with the usage of
TItem
on the static method:And while I don't really get why this wouldn't work, fixing it, does fix the issue in PHPStan as well: https://phpstan.org/r/196645b0-ac23-4299-8d33-1536fad66aa8 .
But when changing the generic type to use an unknown name it does properly error. See here where I dropped the
@template T
on the static, while still usingT
for the param and return type. This properly reports "has invalid (return) type T".Code snippet that reproduces the problem
https://phpstan.org/r/6b06b2b0-b234-4b9e-afac-ff0e694419b2
Expected output
Preferably, "it just works", i.e. the original snippet results in
GenericItem<TestItem>
.If there is some limitation / good reason why this wouldn't (always) work an error should be reported that
TItem
is an unknown type / is a class level template which can't be used on a static method / ... (IMO the Psalm error isn't really clear either, because the template type is actually defined, but ignore because it is on the class and not used on the static method).Did PHPStan help you today? Did it make you happy in any way?
No response
The text was updated successfully, but these errors were encountered: