vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.54k stars 659 forks source link

Templates not recognized in `@var` annotated promoted properties #7919

Open jrmajor opened 2 years ago

jrmajor commented 2 years ago

Template types are not recognized in @var annotated promoted properties. It works correctly with @param annotations.

https://psalm.dev/r/21a6833215

psalm-github-bot[bot] commented 2 years ago

I found these snippets:

https://psalm.dev/r/21a6833215 ```php $foo */ function bar(Foo $foo): void { $foo->withParam; // int $foo->withVar; // T, should be int } ``` ``` Psalm output (using commit f960d71): ERROR: UndefinedDocblockClass - 9:9 - Docblock-defined class, interface or enum named T does not exist ```
neclimdul commented 2 years ago

Think I'm running into the same or at least a closely related bug trying to coerce a dynamic type in a method.

https://psalm.dev/r/bdb53a462a

psalm-github-bot[bot] commented 2 years ago

I found these snippets:

https://psalm.dev/r/bdb53a462a ```php */ use MyTrait; } ``` ``` Psalm output (using commit a9775c6): INFO: MixedReturnTypeCoercion - 23:16 - The declared return type 'CustomRecord' for MyTrait::myFunction is more specific than the inferred return type 'T:MyTrait as mixed' ```
AndrolGenhald commented 2 years ago

@neclimdul That's a separate bug, it looks more like #4538.

neclimdul commented 2 years ago

I'm guessing these are all related as there seem to be a lot of template trait problems in psalm but I don't think my snippet is related to that issue. In hat issue the parameter type isn't getting the template type causing the method call to fail.

In the snipped I posted, the @var explicit type doesn't get the template information similar to this issue. That said its actually and different rule failing in a different spot so its likely my example is yet another bug. Without digging into the code its hard to say for sure.

AndrolGenhald commented 2 years ago

This issue is specifically about promoted properties, it's actually quite different from the issue you're having. There are indeed a lot of trait+template issues, but if none of them seem like a close match feel free to open a new one.