Skip to content

No warning that a method being called on a class does not exist (even though the type's class is correct) #11353

Open
phpstan/phpstan-src
#5976

Description

@gnutix

Bug report

Hello there,

Quite an edge case, but I thought it was still worth reporting. We're dynamically calling a method create on a class using $classFQCN::create(), and $classFQCN is properly typed as a union of multiple classes that should all have the create method. But if one of these class do not have the method, no warning is thrown, and an error happens at runtime.

Code snippet that reproduces the problem

https://phpstan.org/r/22f07bc0-a79d-44a4-92b6-e969d0e97e4f

Expected output

PHPStan should report an error like :

Cannot call "Test\C::create()" as the method does not exist.

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

PHPStan is such a critical component when doing any kind of technical change (updating vendors, applying Rectors, refactorings...).

Activity

  1. phpstan-bot commented on Sep 15, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
     
     45: Dumped type: 'Test\\A'|'Test\\B'|'Test\\C'
     
    -PHP 7.2 – 8.0 (2 errors)
    +PHP 8.0 (2 errors)
    +==========
    +
    + 5: Syntax error, unexpected T_STRING on line 5
    + 6: Syntax error, unexpected '{' on line 6
    +
    +PHP 7.2 – 7.4 (2 errors)
     ==========
     
      5: Syntax error, unexpected T_STRING on line 5
      7: Syntax error, unexpected T_CASE on line 7
    Full report

    PHP 8.1 – 8.3 (1 error)

    Line Error
    45 `Dumped type: 'Test\A'

    PHP 8.0 (2 errors)

    Line Error
    5 Syntax error, unexpected T_STRING on line 5
    6 Syntax error, unexpected '{' on line 6

    PHP 7.2 – 7.4 (2 errors)

    Line Error
    5 Syntax error, unexpected T_STRING on line 5
    7 Syntax error, unexpected T_CASE on line 7
  2. phpstan-bot commented on Sep 11, 2026

    @phpstan-bot
    Contributor

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

    @@ @@
    -PHP 8.1 – 8.3 (1 error)
    +PHP 8.1 – 8.3 (2 errors)
     ==========
     
     45: Dumped type: 'Test\\A'|'Test\\B'|'Test\\C'
    +48: Variable $class is never read.
     
    -PHP 7.2 – 8.0 (2 errors)
    +PHP 8.0 (2 errors)
    +==========
    +
    + 5: Syntax error, unexpected T_STRING on line 5
    + 6: Syntax error, unexpected '{' on line 6
    +
    +PHP 7.2 – 7.4 (2 errors)
     ==========
     
      5: Syntax error, unexpected T_STRING on line 5
      7: Syntax error, unexpected T_CASE on line 7
    Full report

    PHP 8.1 – 8.3 (2 errors)

    Line Error
    45 `Dumped type: 'Test\A'
    48 Variable $class is never read.

    PHP 8.0 (2 errors)

    Line Error
    5 Syntax error, unexpected T_STRING on line 5
    6 Syntax error, unexpected '{' on line 6

    PHP 7.2 – 7.4 (2 errors)

    Line Error
    5 Syntax error, unexpected T_STRING on line 5
    7 Syntax error, unexpected T_CASE on line 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions