Skip to content
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

template-use only works if trait and the class using the trait, has same name for generic type #4423

Open
canvural opened this issue Jan 24, 2021 · 10 comments

Comments

@canvural
Copy link
Contributor

Bug report

Not working: https://phpstan.org/r/7ae5f72d-9a22-4faf-a171-8c93994a5f1a
Working: https://phpstan.org/r/c2027941-0e2e-4ad7-a464-8a63c40d7ec5

Expected Output

Dumped type: Bar<int> in both cases

@canvural canvural changed the title template-use only works if trait and the class using the trait have the same name for generic type template-use only works if trait and the class using the trait, has same name for generic type Jan 24, 2021
@ondrejmirtes
Copy link
Member

@template-use isn't actually implemented at all, just reserved. No one asked for it until now since the generics were released in 0.12 :)

One of your examples works to pure coincidence just because there's the same type variable @template T above both Child and Foo... See: https://phpstan.org/r/fa81a4f4-bb0f-4bbb-8d83-d7c34365bbcf

@ondrejmirtes ondrejmirtes added this to the Generics milestone Jan 24, 2021
@canvural
Copy link
Contributor Author

canvural commented Jan 24, 2021

Oh, what a coincidence then 😅

I'd love this to be in PHPStan. It'd solve a very large use case in Larastan/Laravel without writing bunch of extra extension code.

Looks like this coincidental way is working for now though.

@dktapps
Copy link
Contributor

dktapps commented Jan 24, 2021

I would also like to see this feature. I just never asked because I assumed it was coming sooner or later anyway (also my use case is small enough to be a minor irritation anyways).

@ondrejmirtes
Copy link
Member

I added basic support for generic traits, but making it work for magic properties and methods defined via annotations is some extra complicated work, I'm not sure if it's even gonna be possible, but we'll see. phpstan/phpstan-src@8766923

@phpstan-bot
Copy link
Contributor

@canvural After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.2 (2 errors)
+==========
+
+35: Access to an undefined property Child<int>::$bar.
+35: Dumped type: *ERROR*
+
+PHP 7.1 – 8.1 (1 error)
+==========
+
 35: Dumped type: Bar<K (class Foo, parameter)>
Full report

PHP 8.2 (2 errors)

Line Error
35 Access to an undefined property Child<int>::$bar.
35 Dumped type: *ERROR*

PHP 7.1 – 8.1 (1 error)

Line Error
35 Dumped type: Bar<K (class Foo, parameter)>

@phpstan-bot
Copy link
Contributor

@canvural After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.2 (2 errors)
+==========
+
+35: Access to an undefined property Child<int>::$bar.
+35: Dumped type: *ERROR*
+
+PHP 7.1 – 8.1 (1 error)
+==========
+
 35: Dumped type: Bar<int>
Full report

PHP 8.2 (2 errors)

Line Error
35 Access to an undefined property Child<int>::$bar.
35 Dumped type: *ERROR*

PHP 7.1 – 8.1 (1 error)

Line Error
35 Dumped type: Bar<int>

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.2 (2 errors)
+==========
+
+35: Access to an undefined property Child<int>::$bar.
+35: Dumped type: *ERROR*
+
+PHP 7.1 – 8.1 (1 error)
+==========
+
 35: Dumped type: Bar<TT (class Foo, parameter)>
Full report

PHP 8.2 (2 errors)

Line Error
35 Access to an undefined property Child<int>::$bar.
35 Dumped type: *ERROR*

PHP 7.1 – 8.1 (1 error)

Line Error
35 Dumped type: Bar<TT (class Foo, parameter)>

@phpstan-bot
Copy link
Contributor

@canvural After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.2 – 8.3 (2 errors)
+==========
+
+35: Access to an undefined property Child<int>::$bar.
+35: Dumped type: *ERROR*
+
+PHP 7.1 – 8.1 (1 error)
+==========
+
 35: Dumped type: Bar<K (class Foo, parameter)>
Full report

PHP 8.2 – 8.3 (2 errors)

Line Error
35 Access to an undefined property Child<int>::$bar.
35 Dumped type: *ERROR*

PHP 7.1 – 8.1 (1 error)

Line Error
35 Dumped type: Bar<K (class Foo, parameter)>

@phpstan-bot
Copy link
Contributor

@canvural After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.2 – 8.3 (2 errors)
+==========
+
+35: Access to an undefined property Child<int>::$bar.
+35: Dumped type: *ERROR*
+
+PHP 7.1 – 8.1 (1 error)
+==========
+
 35: Dumped type: Bar<int>
Full report

PHP 8.2 – 8.3 (2 errors)

Line Error
35 Access to an undefined property Child<int>::$bar.
35 Dumped type: *ERROR*

PHP 7.1 – 8.1 (1 error)

Line Error
35 Dumped type: Bar<int>

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.2 – 8.3 (2 errors)
+==========
+
+35: Access to an undefined property Child<int>::$bar.
+35: Dumped type: *ERROR*
+
+PHP 7.1 – 8.1 (1 error)
+==========
+
 35: Dumped type: Bar<TT (class Foo, parameter)>
Full report

PHP 8.2 – 8.3 (2 errors)

Line Error
35 Access to an undefined property Child<int>::$bar.
35 Dumped type: *ERROR*

PHP 7.1 – 8.1 (1 error)

Line Error
35 Dumped type: Bar<TT (class Foo, parameter)>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants