Skip to content

Traits are evaluated based on static knowledge in assembled classes #4570

Closed
phpstan/phpstan-src
#5309

Description

@schlessera

Bug report

The logic in a trait is evaluated together with static knowledge that is only present in its assembled form as used within a class.

This leads to problems where traits contain logic that is conditional based on which class the trait is included in. For the classes where the conditional logic would not be needed, PHPStan complains about these conditionals, although they could not be left out of the trait in its stand-alone form.

As an example, when a trait looks at a class constant to see if it contains a certain value or key, PHPStan will evaluate this with the actual class constant present for each class, and complain that conditional would always yield the same result.

Code snippet that reproduces the problem

https://phpstan.org/r/770c0b81-f547-4214-b677-dac3d3c1cc8f

Expected output

Conditional logic in traits cannot make use of knowledge that is only present once the trait is compiled into a class, and no errors like the above should be raised.

Activity

  1. added this to the Easy fixes milestone on Feb 19, 2021
  2. phpstan-bot commented on Sep 23, 2021

    @phpstan-bot
    Contributor

    @schlessera After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

    @@ @@
    - 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'valueToFetch' => '123') will always evaluate to true.
    -10: Unreachable statement - code above always terminates.
    - 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'someOtherValue' => '123') will always evaluate to false.
    +-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
    +Run PHPStan with --debug option and post the stack trace to:
    +https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
    Full report
    Line Error
    -1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
  3. phpstan-bot commented on Oct 4, 2021

    @phpstan-bot
    Contributor

    @schlessera After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

    @@ @@
    - 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'valueToFetch' => '123') will always evaluate to true.
    -10: Unreachable statement - code above always terminates.
    - 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'someOtherValue' => '123') will always evaluate to false.
    +18: Constant FirstConsumer::MY_CONST_ARRAY is unused.
    +28: Constant SecondConsumer::MY_CONST_ARRAY is unused.
    + 5: Method FirstConsumer::fetchValue() should return string but return statement is missing.
    + 5: Method SecondConsumer::fetchValue() should return string but return statement is missing.
    Full report
    Line Error
    18 Constant FirstConsumer::MY_CONST_ARRAY is unused.
    28 Constant SecondConsumer::MY_CONST_ARRAY is unused.
    5 Method FirstConsumer::fetchValue() should return string but return statement is missing.
    5 Method SecondConsumer::fetchValue() should return string but return statement is missing.
  4. phpstan-bot commented on Oct 13, 2021

    @phpstan-bot
    Contributor

    @schlessera After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

    @@ @@
    - 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'valueToFetch' => '123') will always evaluate to true.
    + 6: Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', valueToFetch: '123'} will always evaluate to true.
     10: Unreachable statement - code above always terminates.
    - 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'someOtherValue' => '123') will always evaluate to false.
    + 6: Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', someOtherValue: '123'} will always evaluate to false.
    Full report
    Line Error
    6 Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', valueToFetch: '123'} will always evaluate to true.
    10 Unreachable statement - code above always terminates.
    6 Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', someOtherValue: '123'} will always evaluate to false.
  5. LastDragon-ru commented on Sep 20, 2022

    @LastDragon-ru

    Really annoying issue :(

  6. paranoiq commented on Sep 7, 2023

    @paranoiq
    Contributor

    probably related or the same issue: https://phpstan.org/r/89bc3b55-f22e-4a0b-bf92-84db066c899a (static::class used in trait)

  7. added a commit that references this issue on Mar 27, 2026
  8. 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.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions