phpstan
phpstan copied to clipboard
`template-use` only works if trait and the class using the trait, has same name for generic type
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
@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
Oh, what a coincidence then :sweat_smile:
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.
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).
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. https://github.com/phpstan/phpstan-src/commit/87669234363d89252583664ef71309d5d8d3c7c1
@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)> |
@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> |
@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)> |
@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)> |
@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> |
@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)> |
@canvural After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
@@ @@
+PHP 8.2 – 8.4 (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.4 (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)> |
@canvural After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
@@ @@
+PHP 8.2 – 8.4 (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.4 (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> |
@ondrejmirtes After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:
@@ @@
+PHP 8.2 – 8.4 (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.4 (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)> |
@canvural After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
@@ @@
+PHP 8.2 – 8.5 (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.5 (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)> |
@canvural After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
@@ @@
+PHP 8.2 – 8.5 (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.5 (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> |
@ondrejmirtes After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
@@ @@
+PHP 8.2 – 8.5 (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.5 (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)> |