Skip to content

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

@schlessera

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions