phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

static::class is_a interface::class, child implements Interface but errors on parent

Open belisoful opened this issue 2 years ago • 3 comments

Bug report

My apologies for not checking my own stuff before. Here is the correction-update to the two PHPStan bugs I am encountering in PHPstan

1- Call to an undefined static method static(Component)::singleton(). The problem is that this should be blocked by the is_a validation check. 2- Method Application::singleton() should return static(Application)|null but returns Application.. It is actually returning the correct type but is being flagged is incorrect.

Code snippet that reproduces the problem

https://phpstan.org/r/e38e5f86-796e-4b58-9748-5edbbb591d3b

Expected output

No error on both bugs

Did PHPStan help you today? Did it make you happy in any way?

No response

belisoful avatar Jun 19 '23 12:06 belisoful

Your 2nd problem can be solved with /** @var static */ above the property: https://phpstan.org/r/64e2155d-f240-4625-9a0f-68004c97ff18

ondrejmirtes avatar Jun 19 '23 12:06 ondrejmirtes

This issue is holding up our issue: https://github.com/pradosoft/prado/issues/976

belisoful avatar Jun 20 '23 02:06 belisoful

Hi,

Just encountered a variation the first part of this issue as well: https://phpstan.org/r/21b97a66-55ae-4884-8361-b54dddad8f8a

While I can use (static::class)::method() to trick PHPStan, the need for dereferencing should be changed by marking static with an intersection of any successful is_a() call on its second argument like elsewhere

kallesommernielsen avatar Feb 14 '24 16:02 kallesommernielsen

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

@@ @@
 PHP 7.4 – 8.2 (2 errors)
 ==========
 
-18: Call to an undefined static method static(Component)::singleton().
+18: Call to an undefined static method Component::singleton().
 37: Method Application::singleton() should return static(Application)|null but returns Application.
 
 PHP 7.2 – 7.3 (3 errors)
 ==========
 
-18: Call to an undefined static method static(Component)::singleton().
+18: Call to an undefined static method Component::singleton().
 35: Return type static(Application)|null of method Application::singleton() is not compatible with return type static(ISingleton)|null of method ISingleton::singleton().
 37: Method Application::singleton() should return static(Application)|null but returns Application.
Full report

PHP 7.4 – 8.2 (2 errors)

Line Error
18 Call to an undefined static method Component::singleton().
37 `Method Application::singleton() should return static(Application)

PHP 7.2 – 7.3 (3 errors)

Line Error
18 Call to an undefined static method Component::singleton().
35 `Return type static(Application)
37 `Method Application::singleton() should return static(Application)

phpstan-bot avatar Feb 24 '24 14:02 phpstan-bot

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

@@ @@
 PHP 7.4 – 8.2 (1 error)
 ==========
 
-18: Call to an undefined static method static(Component)::singleton().
+18: Call to an undefined static method Component::singleton().
 
 PHP 7.2 – 7.3 (2 errors)
 ==========
 
-18: Call to an undefined static method static(Component)::singleton().
+18: Call to an undefined static method Component::singleton().
 36: Return type static(Application)|null of method Application::singleton() is not compatible with return type static(ISingleton)|null of method ISingleton::singleton().
Full report

PHP 7.4 – 8.2 (1 error)

Line Error
18 Call to an undefined static method Component::singleton().

PHP 7.2 – 7.3 (2 errors)

Line Error
18 Call to an undefined static method Component::singleton().
36 `Return type static(Application)

phpstan-bot avatar Feb 24 '24 14:02 phpstan-bot

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

@@ @@
 PHP 8.0 – 8.3 (1 error)
 ==========
 
-17: Call to an undefined static method static(Parent1)::isLoadable().
+17: Call to an undefined static method Parent1::isLoadable().
 
 PHP 7.4 (3 errors)
 ==========
 
-17: Call to an undefined static method static(Parent1)::isLoadable().
+17: Call to an undefined static method Parent1::isLoadable().
 17: Named arguments are supported only on PHP 8.0 and later.
 33: Named arguments are supported only on PHP 8.0 and later.
 
@@ @@
 PHP 7.2 – 7.3 (5 errors)
 ==========
 
-17: Call to an undefined static method static(Parent1)::isLoadable().
+17: Call to an undefined static method Parent1::isLoadable().
 17: Named arguments are supported only on PHP 8.0 and later.
 33: Named arguments are supported only on PHP 8.0 and later.
 45: Return type false of method Child2::isLoadable() is not compatible with return type bool of method Dependency::isLoadable().
 54: Return type false of method Child4::isLoadable() is not compatible with return type bool of method Dependency::isLoadable().
Full report

PHP 8.0 – 8.3 (1 error)

Line Error
17 Call to an undefined static method Parent1::isLoadable().

PHP 7.4 (3 errors)

Line Error
17 Call to an undefined static method Parent1::isLoadable().
17 Named arguments are supported only on PHP 8.0 and later.
33 Named arguments are supported only on PHP 8.0 and later.

PHP 7.2 – 7.3 (5 errors)

Line Error
17 Call to an undefined static method Parent1::isLoadable().
17 Named arguments are supported only on PHP 8.0 and later.
33 Named arguments are supported only on PHP 8.0 and later.
45 Return type false of method Child2::isLoadable() is not compatible with return type bool of method Dependency::isLoadable().
54 Return type false of method Child4::isLoadable() is not compatible with return type bool of method Dependency::isLoadable().

phpstan-bot avatar Feb 24 '24 14:02 phpstan-bot