Skip to content

Commit 33eb309

Browse files
phpstan-botclaude
andcommitted
Add comprehensive tests for duplicate declarations in traits (constants, properties, promoted properties, methods)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c816cfd commit 33eb309

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

tests/PHPStan/Rules/Classes/DuplicateTraitDeclarationRuleTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,44 @@ public function testBug14250(): void
2323
'Cannot redeclare method Bug14250\MyTrait::doSomething().',
2424
11,
2525
],
26+
[
27+
'Cannot redeclare constant Bug14250\TraitWithDuplicateConstants::CONST1.',
28+
24,
29+
],
30+
[
31+
'Cannot redeclare constant Bug14250\TraitWithDuplicateConstants::CONST2.',
32+
26,
33+
],
34+
[
35+
'Cannot redeclare property Bug14250\TraitWithDuplicateProperties::$prop1.',
36+
41,
37+
],
38+
[
39+
'Cannot redeclare property Bug14250\TraitWithDuplicateProperties::$prop2.',
40+
44,
41+
],
42+
[
43+
'Cannot redeclare method Bug14250\TraitWithDuplicateMethods::func1().',
44+
59,
45+
],
46+
[
47+
'Cannot redeclare method Bug14250\TraitWithDuplicateMethods::Func1().',
48+
69,
49+
],
50+
]);
51+
}
52+
53+
public function testDuplicatePromotedProperty(): void
54+
{
55+
$this->analyse([__DIR__ . '/data/bug-14250-promoted-properties.php'], [
56+
[
57+
'Cannot redeclare property Bug14250PromotedProperties\TraitWithDuplicatePromotedProperties::$foo.',
58+
10,
59+
],
60+
[
61+
'Cannot redeclare property Bug14250PromotedProperties\TraitWithDuplicatePromotedProperties::$bar.',
62+
12,
63+
],
2664
]);
2765
}
2866

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug14250PromotedProperties;
4+
5+
trait TraitWithDuplicatePromotedProperties
6+
{
7+
private $foo;
8+
9+
public function __construct(
10+
private $foo,
11+
private $bar,
12+
private $bar
13+
)
14+
{
15+
16+
}
17+
}
18+
19+
class Foo
20+
{
21+
use TraitWithDuplicatePromotedProperties;
22+
}

tests/PHPStan/Rules/Classes/data/bug-14250.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,59 @@ class Foo
1717
{
1818
use MyTrait;
1919
}
20+
21+
trait TraitWithDuplicateConstants
22+
{
23+
public const CONST1 = 1;
24+
public const CONST1 = 2;
25+
26+
public const CONST2 = 2, CONST2 = 1;
27+
28+
public const CONST3 = 1;
29+
}
30+
31+
class Bar
32+
{
33+
use TraitWithDuplicateConstants;
34+
}
35+
36+
trait TraitWithDuplicateProperties
37+
{
38+
/** @var int */
39+
public $prop1;
40+
/** @var int */
41+
public $prop1;
42+
43+
/** @var int */
44+
public $prop2, $prop2;
45+
46+
/** @var int */
47+
public $prop3;
48+
}
49+
50+
class Baz
51+
{
52+
use TraitWithDuplicateProperties;
53+
}
54+
55+
trait TraitWithDuplicateMethods
56+
{
57+
public function func1(): void {}
58+
59+
public function func1(): int
60+
{
61+
return 1;
62+
}
63+
64+
public function func2(): int
65+
{
66+
return 2;
67+
}
68+
69+
public function Func1(): void {}
70+
}
71+
72+
class Qux
73+
{
74+
use TraitWithDuplicateMethods;
75+
}

0 commit comments

Comments
 (0)