diff --git a/src/Rules/Traits/ConflictingTraitConstantsRule.php b/src/Rules/Traits/ConflictingTraitConstantsRule.php index 63b1d047a2..ff1802eede 100644 --- a/src/Rules/Traits/ConflictingTraitConstantsRule.php +++ b/src/Rules/Traits/ConflictingTraitConstantsRule.php @@ -43,6 +43,12 @@ public function processNode(Node $node, Scope $scope): array return []; } + // Even though isInClass() is true, we still need to check isInTrait() because + // both can be true simultaneously when analyzing trait code. + if ($scope->isInTrait()) { + return []; + } + $classReflection = $scope->getClassReflection(); $traitConstants = []; foreach ($classReflection->getTraits(true) as $trait) { diff --git a/tests/PHPStan/Rules/Traits/data/conflicting-trait-constants.php b/tests/PHPStan/Rules/Traits/data/conflicting-trait-constants.php index dc68ba0758..21a13026a3 100644 --- a/tests/PHPStan/Rules/Traits/data/conflicting-trait-constants.php +++ b/tests/PHPStan/Rules/Traits/data/conflicting-trait-constants.php @@ -103,3 +103,21 @@ class Bar10 final public const PUBLIC_FINAL_CONSTANT = 1; } + +trait Foo1 +{ + protected const ARR1 = [ + self::KEY => 'int', + ]; +} + +class Bar11 +{ + use Foo1; + + protected const KEY = 'k1'; + + protected const ARR = self::ARR1 + [ + 'a.b' => 'int', + ]; +}