Skip to content

Commit f76b0be

Browse files
VincentLangletphpstan-bot
authored andcommitted
Fix trait constant value comparison resolving self:: in wrong context
- 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
1 parent 4b2e7e5 commit f76b0be

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/Rules/Traits/ConflictingTraitConstantsRule.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ public function processNode(Node $node, Scope $scope): array
8080
$classConstantValueType = $this->initializerExprTypeResolver->getType($const->value, InitializerExprContext::fromClassReflection($classReflection));
8181
$traitConstantValueType = $this->initializerExprTypeResolver->getType(
8282
$immediateTraitConstant->getValueExpression(),
83-
InitializerExprContext::fromClass(
84-
$immediateTraitConstant->getDeclaringClass()->getName(),
85-
$immediateTraitConstant->getDeclaringClass()->getFileName() !== false ? $immediateTraitConstant->getDeclaringClass()->getFileName() : null,
86-
),
83+
InitializerExprContext::fromClassReflection($classReflection),
8784
);
8885
if ($classConstantValueType->equals($traitConstantValueType)) {
8986
continue;

tests/PHPStan/Rules/Traits/ConflictingTraitConstantsRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ public function testBug13119(): void
8585
$this->analyse([__DIR__ . '/data/bug-13119.php'], []);
8686
}
8787

88+
#[RequiresPhp('>= 8.2')]
89+
public function testBug11351(): void
90+
{
91+
$this->analyse([__DIR__ . '/data/bug-11351.php'], []);
92+
}
93+
8894
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php // lint >= 8.2
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug11351;
6+
7+
trait TA
8+
{
9+
protected const A = [
10+
self::CA => 'CA',
11+
];
12+
}
13+
14+
class A
15+
{
16+
use TA;
17+
public const CA = 'abc';
18+
}
19+
20+
trait TB
21+
{
22+
protected const B = [
23+
'key' => self::CB,
24+
];
25+
}
26+
27+
class B
28+
{
29+
use TB;
30+
public const CB = 'value';
31+
}
32+
33+
trait TC
34+
{
35+
protected const C = [
36+
self::CC1 => self::CC2,
37+
];
38+
}
39+
40+
class C
41+
{
42+
use TC;
43+
public const CC1 = 'key';
44+
public const CC2 = 'value';
45+
}

0 commit comments

Comments
 (0)