Skip to content

Type alias imports in traits do not work #5091

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

Closed
Seldaek opened this issue May 28, 2021 · 11 comments
Closed

Type alias imports in traits do not work #5091

Seldaek opened this issue May 28, 2021 · 11 comments
Labels

Comments

@Seldaek
Copy link
Contributor

Seldaek commented May 28, 2021

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
Copy link
Contributor

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

@Ilyes512
Copy link

Ilyes512 commented Mar 2, 2022

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
Copy link

arderyp commented Apr 20, 2022

@staabm
Copy link
Contributor

staabm commented Jul 27, 2022

@Khez
Copy link

Khez commented Sep 9, 2022

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
Copy link
Contributor Author

Seldaek commented Sep 9, 2022

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

@noahlvb
Copy link

noahlvb commented Oct 25, 2022

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

@ptlis
Copy link

ptlis commented Nov 9, 2022

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
Copy link

ruscon commented Jan 17, 2023

Has anyone found a solution?

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@60021c2

@github-actions
Copy link

github-actions bot commented Apr 8, 2023

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 github-actions bot locked as resolved and limited conversation to collaborators Apr 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

10 participants