Skip to content

Commit 9e0d021

Browse files
phpstan-botVincentLangletstaabm
authored
Fix phpstan/phpstan#11351: Defining Array Constants in PHP Traits Causes Errors When Using Class Constants as Keys or Values (#5115)
Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
1 parent 84639d0 commit 9e0d021

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,22 @@ public function testNativeTypes(): void
7979
]);
8080
}
8181

82+
#[RequiresPhp('>= 8.2')]
83+
public function testBug11088(): void
84+
{
85+
$this->analyse([__DIR__ . '/data/bug-11088.php'], []);
86+
}
87+
8288
#[RequiresPhp('>= 8.3')]
8389
public function testBug13119(): void
8490
{
8591
$this->analyse([__DIR__ . '/data/bug-13119.php'], []);
8692
}
8793

94+
#[RequiresPhp('>= 8.2')]
95+
public function testBug11351(): void
96+
{
97+
$this->analyse([__DIR__ . '/data/bug-11351.php'], []);
98+
}
99+
88100
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.2
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug11088;
6+
7+
trait Foo
8+
{
9+
protected const ARR1 = [
10+
self::KEY => 'int',
11+
];
12+
}
13+
14+
class HelloWorld
15+
{
16+
use Foo;
17+
18+
protected const KEY = 'k1';
19+
20+
protected const ARR = self::ARR1 + [
21+
'a.b' => 'int',
22+
];
23+
}
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)