Skip to content

[Enum/Trait] Call to function method_exists() will always evaluate to false #7599

Closed
phpstan/phpstan-src
#5309

Description

@Dartui

Bug report

Hello. I am facing issues with enum/trait combo. In some of my enums I have defined method, which can be used by trait. However I am getting Call to function method_exists() will always evaluate to false. in trait which does not have this method defined. I don't know if this is some false-positive or my code is simply trash 😉

Code snippet that reproduces the problem

https://phpstan.org/r/38e7fa19-60fa-4a4e-b3dd-e3ed305f13b8

Expected output

No errors.

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

Sure thing, without it quality of my work would be much worse. You did a really good job!

Activity

  1. vpratfr commented on Mar 1, 2023

    @vpratfr

    Hi,

    Same message here. I cannot get rid of it.

    Code is working as expected and is very similar to the snippet provided in the issue. (trait used by several enums, checking for method existence on self::class).

    This would look like a false positive: we are checking in the trait that method is defined (or not) so it is expected that not all classes using that trait will have it.

  2. samuelgfeller commented on Apr 30, 2024

    @samuelgfeller

    I have the same issue with a function in a trait used by some Enum with self as parameter type. That particular function relies on a method that an Enum can but must not implement, hence the test with method_exists.

    Would love not having to use a phpstan force ignore to get rid of the message.

  3. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
     
     11: Call to function method_exists() with TestEnum::Bar|TestEnum::Foo and 'barMethod' will always evaluate to false.
     
    -PHP 7.4 – 8.0 (3 errors)
    +PHP 7.1 – 8.0 (3 errors)
     ==========
     
    -19: Syntax error, unexpected T_STRING on line 19
    -21: Syntax error, unexpected T_USE on line 21
    -23: Syntax error, unexpected T_CASE on line 23
    -
    -PHP 7.1 – 7.3 (6 errors)
    -==========
    -
    -11: Syntax error, unexpected T_VARIABLE, expecting ')' on line 11
    -13: Syntax error, unexpected ',' on line 13
    -15: Syntax error, unexpected ')' on line 15
     19: Syntax error, unexpected T_STRING on line 19
     21: Syntax error, unexpected T_USE on line 21
     23: Syntax error, unexpected T_CASE on line 23
    Full report

    PHP 8.1 (1 error)

    Line Error
    11 `Call to function method_exists() with TestEnum::Bar

    PHP 7.1 – 8.0 (3 errors)

    Line Error
    19 Syntax error, unexpected T_STRING on line 19
    21 Syntax error, unexpected T_USE on line 21
    23 Syntax error, unexpected T_CASE on line 23
  4. phpstan-bot commented on Sep 15, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
     
     11: Call to function method_exists() with TestEnum::Bar|TestEnum::Foo and 'barMethod' will always evaluate to false.
     
    -PHP 7.4 – 8.0 (3 errors)
    +PHP 8.0 (4 errors)
     ==========
     
     19: Syntax error, unexpected T_STRING on line 19
    -21: Syntax error, unexpected T_USE on line 21
    -23: Syntax error, unexpected T_CASE on line 23
    +20: Syntax error, unexpected '{' on line 20
    +27: Syntax error, unexpected T_STRING on line 27
    +28: Syntax error, unexpected '{' on line 28
     
    -PHP 7.1 – 7.3 (6 errors)
    +PHP 7.1 – 7.4 (3 errors)
     ==========
     
    -11: Syntax error, unexpected T_VARIABLE, expecting ')' on line 11
    -13: Syntax error, unexpected ',' on line 13
    -15: Syntax error, unexpected ')' on line 15
     19: Syntax error, unexpected T_STRING on line 19
     21: Syntax error, unexpected T_USE on line 21
     23: Syntax error, unexpected T_CASE on line 23
    Full report

    PHP 8.1 (1 error)

    Line Error
    11 `Call to function method_exists() with TestEnum::Bar

    PHP 8.0 (4 errors)

    Line Error
    19 Syntax error, unexpected T_STRING on line 19
    20 Syntax error, unexpected '{' on line 20
    27 Syntax error, unexpected T_STRING on line 27
    28 Syntax error, unexpected '{' on line 28

    PHP 7.1 – 7.4 (3 errors)

    Line Error
    19 Syntax error, unexpected T_STRING on line 19
    21 Syntax error, unexpected T_USE on line 21
    23 Syntax error, unexpected T_CASE on line 23
  5. phpstan-bot commented on Sep 24, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
    -PHP 8.1 (1 error)
    +PHP 8.1 (2 errors)
     ==========
     
    +11: Call to function method_exists() with SecondEnum::Baz and 'barMethod' will always evaluate to true.
     11: Call to function method_exists() with TestEnum::Bar|TestEnum::Foo and 'barMethod' will always evaluate to false.
     
    -PHP 7.4 – 8.0 (3 errors)
    +PHP 8.0 (4 errors)
     ==========
     
     19: Syntax error, unexpected T_STRING on line 19
    -21: Syntax error, unexpected T_USE on line 21
    -23: Syntax error, unexpected T_CASE on line 23
    +20: Syntax error, unexpected '{' on line 20
    +27: Syntax error, unexpected T_STRING on line 27
    +28: Syntax error, unexpected '{' on line 28
     
    -PHP 7.1 – 7.3 (6 errors)
    +PHP 7.1 – 7.4 (3 errors)
     ==========
     
    -11: Syntax error, unexpected T_VARIABLE, expecting ')' on line 11
    -13: Syntax error, unexpected ',' on line 13
    -15: Syntax error, unexpected ')' on line 15
     19: Syntax error, unexpected T_STRING on line 19
     21: Syntax error, unexpected T_USE on line 21
     23: Syntax error, unexpected T_CASE on line 23
    Full report

    PHP 8.1 (2 errors)

    Line Error
    11 Call to function method_exists() with SecondEnum::Baz and 'barMethod' will always evaluate to true.
    11 `Call to function method_exists() with TestEnum::Bar

    PHP 8.0 (4 errors)

    Line Error
    19 Syntax error, unexpected T_STRING on line 19
    20 Syntax error, unexpected '{' on line 20
    27 Syntax error, unexpected T_STRING on line 27
    28 Syntax error, unexpected '{' on line 28

    PHP 7.1 – 7.4 (3 errors)

    Line Error
    19 Syntax error, unexpected T_STRING on line 19
    21 Syntax error, unexpected T_USE on line 21
    23 Syntax error, unexpected T_CASE on line 23
  6. kartikkapatel commented on Jan 9, 2025

    @kartikkapatel

    +1 Same context.

  7. adnweedon commented on Sep 2, 2025

    @adnweedon

    As the duplicate ticket has been closed, I'll comment here as well - the same issue exists for property_exists(), for the same reasons!

    https://phpstan.org/r/a6fb1d5e-d6a3-4c01-86e2-946824deaf88

  8. added a commit that references this issue on Mar 27, 2026
  9. github-actions commented on Apr 28, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  10. locked as resolved and limited conversation to collaborators on Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions