Skip to content

Commit dfc25e1

Browse files
committed
Fix ConflictingTraitConstantsRule when trait constants reference class constants
1 parent 23771d1 commit dfc25e1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Rules/Traits/ConflictingTraitConstantsRule.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public function processNode(Node $node, Scope $scope): array
4343
return [];
4444
}
4545

46+
// Even though isInClass() is true, we still need to check isInTrait() because
47+
// both can be true simultaneously when analyzing trait code.
48+
if ($scope->isInTrait()) {
49+
return [];
50+
}
51+
4652
$classReflection = $scope->getClassReflection();
4753
$traitConstants = [];
4854
foreach ($classReflection->getTraits(true) as $trait) {

tests/PHPStan/Rules/Traits/data/conflicting-trait-constants.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,21 @@ class Bar10
103103

104104
final public const PUBLIC_FINAL_CONSTANT = 1;
105105
}
106+
107+
trait Foo1
108+
{
109+
protected const ARR1 = [
110+
self::KEY => 'int',
111+
];
112+
}
113+
114+
class Bar11
115+
{
116+
use Foo1;
117+
118+
protected const KEY = 'k1';
119+
120+
protected const ARR = self::ARR1 + [
121+
'a.b' => 'int',
122+
];
123+
}

0 commit comments

Comments
 (0)