Fix phpstan/phpstan#11351: Defining Array Constants in PHP Traits Causes Errors When Using Class Constants as Keys or Values#5115
Merged
staabm merged 4 commits intophpstan:2.1.xfrom Mar 1, 2026
Conversation
VincentLanglet
approved these changes
Mar 1, 2026
Contributor
|
will also fix phpstan/phpstan#11088 |
Contributor
|
@phpstan-bot add a regression test for phpstan/phpstan#11088 |
- Fixed ConflictingTraitConstantsRule to resolve trait constant value expressions in the context of the using class, not the trait itself - When a trait constant uses self::CONST, it should resolve against the using class since that's how PHP evaluates trait constants at runtime - New regression test in tests/PHPStan/Rules/Traits/data/bug-11351.php
f76b0be to
f6f4768
Compare
This was referenced Mar 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a trait defines an array constant that uses
self::to reference class constants (as keys or values), PHPStan falsely reported that the class overrides the trait constant with a different value. This happened because the trait constant expression was resolved in the trait's own context, whereself::couldn't see the using class's constants.Changes
src/Rules/Traits/ConflictingTraitConstantsRule.phpto resolve the trait constant value expression usingInitializerExprContext::fromClassReflection($classReflection)(the using class) instead ofInitializerExprContext::fromClass()with the trait's own nametests/PHPStan/Rules/Traits/data/bug-11351.phpwith three scenarios:self::used as array key, as array value, and as both key and valuetestBug11351()intests/PHPStan/Rules/Traits/ConflictingTraitConstantsRuleTest.phpRoot cause
In
ConflictingTraitConstantsRule, the trait constant's value expression was resolved usingInitializerExprContext::fromClass()with the trait's declaring class name. This meant thatself::CAin the trait was resolved against the trait itself, which doesn't defineCA. The class constant's value, however, was correctly resolved against the using class. This caused a type mismatch (e.g.,non-empty-array<int|string, 'CA'>vsarray{abc: 'CA'}), triggering a false positive.The fix resolves both the class and trait constant values in the context of the using class, which matches PHP's runtime behavior where
self::in a trait refers to the class using the trait.Test
Added
tests/PHPStan/Rules/Traits/data/bug-11351.phpwith three test cases:self::as array keyself::as array valueself::as both key and valueAll expect no errors (the false positives should be gone).
Fixes phpstan/phpstan#11351
Closes phpstan/phpstan#11088