phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

Type alias imports in traits do not work

Open Seldaek opened this issue 4 years ago • 6 comments
trafficstars

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.

Seldaek avatar May 28 '21 14:05 Seldaek

@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<string, string>.

phpstan-bot avatar Sep 16 '21 15:09 phpstan-bot

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

Ilyes512 avatar Mar 02 '22 15:03 Ilyes512

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

arderyp avatar Apr 20 '22 15:04 arderyp

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

staabm avatar Jul 27 '22 12:07 staabm

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 ?

Khez avatar Sep 09 '22 08:09 Khez

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

Seldaek avatar Sep 09 '22 09:09 Seldaek

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

noahlvb avatar Oct 25 '22 09:10 noahlvb

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

ptlis avatar Nov 09 '22 10:11 ptlis

Has anyone found a solution?

ruscon avatar Jan 17 '23 21:01 ruscon

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

ondrejmirtes avatar Mar 07 '23 16:03 ondrejmirtes

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.

github-actions[bot] avatar Apr 08 '23 00:04 github-actions[bot]