phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

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

Open canvural opened this issue 4 years ago • 13 comments
trafficstars

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 avatar Jan 24 '21 19:01 canvural

@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 avatar Jan 24 '21 19:01 ondrejmirtes

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.

canvural avatar Jan 24 '21 22:01 canvural

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

dktapps avatar Jan 24 '21 22:01 dktapps

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

ondrejmirtes avatar Mar 09 '21 22:03 ondrejmirtes

@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 avatar Sep 22 '22 15:09 phpstan-bot

@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 avatar Sep 22 '22 15:09 phpstan-bot

@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 avatar Sep 22 '22 15:09 phpstan-bot

@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 avatar Jun 28 '23 20:06 phpstan-bot

@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 avatar Jun 28 '23 20:06 phpstan-bot

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

phpstan-bot avatar Jun 28 '23 20:06 phpstan-bot

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

phpstan-bot avatar Aug 26 '24 21:08 phpstan-bot

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

phpstan-bot avatar Aug 26 '24 21:08 phpstan-bot

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

phpstan-bot avatar Aug 26 '24 21:08 phpstan-bot

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

phpstan-bot avatar Nov 01 '25 13:11 phpstan-bot

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

phpstan-bot avatar Nov 01 '25 13:11 phpstan-bot

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

phpstan-bot avatar Nov 01 '25 13:11 phpstan-bot