phpstan / phpstan

PHP Static Analysis Tool - discover bugs in your code without running it!
https://phpstan.org/
MIT License
12.89k stars 879 forks source link

Type alias imports in traits do not work #5091

Closed Seldaek closed 1 year ago

Seldaek commented 3 years ago

Bug report

It appears that type aliases get resolved from the scope of the class they are used in, and not from the trait's main docblock.

Code snippet that reproduces the problem

https://phpstan.org/r/fb90214f-4b8d-4f62-87b9-1680ee2afcfa

Expected output

No error.

phpstan-bot commented 3 years ago

@Seldaek After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
 33: Property Monolog\Handler\Handler::$foo has unknown class Monolog\Processor\Record as its type.
-38: Return typehint of method Monolog\Handler\Handler::foo() has invalid type Monolog\Processor\Record.
+38: Method Monolog\Handler\Handler::foo() has invalid return type Monolog\Processor\Record.
 39: Method Monolog\Handler\Handler::foo() should return Monolog\Processor\Record but returns array<string, string>.
Full report | Line | Error | |---|---| | 33 | `Property Monolog\Handler\Handler::$foo has unknown class Monolog\Processor\Record as its type.` | | 38 | `Method Monolog\Handler\Handler::foo() has invalid return type Monolog\Processor\Record.` | | 39 | `Method Monolog\Handler\Handler::foo() should return Monolog\Processor\Record but returns array.` |
Ilyes512 commented 2 years ago

I got the same. Should we be able to define types in traits and use them elsewhere or not?

edit:

Defining a @phpstan-type and then using it in the same trait does not work as well. For example: https://phpstan.org/r/bb19adc4-c0cb-4007-bd7c-b727b7faa8ab

arderyp commented 2 years ago

another demo: https://phpstan.org/r/2028d1b8-3106-49cb-82ea-586241bf5186

staabm commented 2 years ago

another example: https://phpstan.org/r/b07cd15e-abad-4f73-a077-bc4c56df24fe

Khez commented 2 years ago

Wanted to add a new issue but found this one which seems to be the same underlying issue:

Traits don't use @phpstan-type or @phpstan-import-type

Another example showing the difference when using a trait and when using classes

https://phpstan.org/r/a3d8e137-cb3b-40aa-ac5c-31d13f5368dc

@Seldaek did you find a workaround for this issue by any chance ?

Seldaek commented 2 years ago

Nope I resolved this by using value objects and removing type aliases :)

noahlvb commented 1 year ago

I'm experiencing the same issue. (https://phpstan.org/r/3a11ea33-8d1e-4928-9266-bfe23b3341ec)

ptlis commented 1 year ago

I think I've just run into this problem, here's another example of this https://phpstan.org/r/cefffe00-ce91-42c6-9b2d-feaf723fb4e9

ruscon commented 1 year ago

Has anyone found a solution?

ondrejmirtes commented 1 year ago

Fixed: https://github.com/phpstan/phpstan-src/commit/60021c2857841155fcd54f3783d8ed86a647a684

github-actions[bot] commented 1 year ago

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.