Skip to content

Constant in trait analysis incorrect #11088

Closed

Description

@liquid207

Bug report

If a const array defined in trait uses const defined in class that using this trait, PHPStan will output an error (See the link for details)

Code snippet that reproduces the problem

https://phpstan.org/r/bfead168-b26e-4c62-98ee-4eefb8d42f9e

Expected output

No errors

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

No response

Activity

  1. added this to the Easy fixes milestone on May 27, 2024
  2. phpstan-bot commented on Nov 30, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
     PHP 8.2 – 8.3 (1 error)
     ==========
     
    -5: Constant HelloWorld::ARR1 with value array{k1: 'int'} overriding constant Foo::ARR1 with different value non-empty-array<int|string, 'int'> should have the same value.
    +5: Constant HelloWorld::ARR1 with value array{k1: 'int'} overriding constant Foo::ARR1 with different value non-empty-array<'int'> should have the same value.
     
     PHP 7.2 – 8.1 (2 errors)
     ==========
     
    -5: Constant HelloWorld::ARR1 with value array{k1: 'int'} overriding constant Foo::ARR1 with different value non-empty-array<int|string, 'int'> should have the same value.
    +5: Constant HelloWorld::ARR1 with value array{k1: 'int'} overriding constant Foo::ARR1 with different value non-empty-array<'int'> should have the same value.
     5: Constant is declared inside a trait but is only supported on PHP 8.2 and later.
    Full report

    PHP 8.2 – 8.3 (1 error)

    Line Error
    5 Constant HelloWorld::ARR1 with value array{k1: 'int'} overriding constant Foo::ARR1 with different value non-empty-array<'int'> should have the same value.

    PHP 7.2 – 8.1 (2 errors)

    Line Error
    5 Constant HelloWorld::ARR1 with value array{k1: 'int'} overriding constant Foo::ARR1 with different value non-empty-array<'int'> should have the same value.
    5 Constant is declared inside a trait but is only supported on PHP 8.2 and later.
  3. phpstan-bot commented on Mar 1, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5115

  4. github-actions commented on Apr 2, 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.

  5. locked as resolved and limited conversation to collaborators on Apr 2, 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