forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConflictingTraitMethodsRule.php
More file actions
155 lines (132 loc) · 4.01 KB
/
ConflictingTraitMethodsRule.php
File metadata and controls
155 lines (132 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Classes;
use PhpParser\Node;
use PhpParser\Node\Stmt\TraitUseAdaptation\Precedence;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\InClassNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function array_key_exists;
use function array_keys;
use function count;
use function reset;
use function sprintf;
use function strtolower;
/**
* @implements Rule<InClassNode>
*/
#[RegisteredRule(level: 0)]
final class ConflictingTraitMethodsRule implements Rule
{
public function __construct(private ReflectionProvider $reflectionProvider)
{
}
public function getNodeType(): string
{
return InClassNode::class;
}
/**
* @return list<IdentifierRuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $node->getClassReflection();
$classLike = $node->getOriginalNode();
$traitUses = $classLike->getTraitUses();
if ($traitUses === []) {
return [];
}
// Collect methods defined directly on the class (not from traits)
$classOwnMethods = [];
foreach ($classLike->getMethods() as $method) {
$classOwnMethods[strtolower($method->name->name)] = true;
}
// Collect all insteadof adaptations
// Key: "traitName::methodName" that is being overridden
$insteadofResolutions = [];
foreach ($traitUses as $traitUse) {
foreach ($traitUse->adaptations as $adaptation) {
if (!$adaptation instanceof Precedence) {
continue;
}
$methodName = strtolower($adaptation->method->name);
foreach ($adaptation->insteadof as $insteadofTrait) {
$insteadofResolutions[strtolower((string) $insteadofTrait) . '::' . $methodName] = true;
}
}
}
// Collect methods from each trait
// Map: lowercased method name => [traitName => true]
$methodTraitMap = [];
foreach ($traitUses as $traitUse) {
foreach ($traitUse->traits as $traitName) {
$traitNameStr = (string) $traitName;
if (!$this->reflectionProvider->hasClass($traitNameStr)) {
continue;
}
$traitReflection = $this->reflectionProvider->getClass($traitNameStr);
if (!$traitReflection->isTrait()) {
continue;
}
foreach ($traitReflection->getNativeReflection()->getMethods() as $method) {
$lowerMethodName = strtolower($method->getName());
$methodTraitMap[$lowerMethodName][$traitReflection->getName()] = [
'name' => $method->getName(),
'abstract' => $method->isAbstract(),
];
}
}
}
$errors = [];
foreach ($methodTraitMap as $lowerMethodName => $traits) {
if (count($traits) <= 1) {
continue;
}
// If the class defines the method itself, no conflict
if (array_key_exists($lowerMethodName, $classOwnMethods)) {
continue;
}
// Filter out abstract methods - PHP allows abstract + concrete without conflict
$concreteTraits = [];
foreach ($traits as $traitName => $methodInfo) {
if ($methodInfo['abstract']) {
continue;
}
$concreteTraits[$traitName] = $methodInfo;
}
if (count($concreteTraits) <= 1) {
continue;
}
// Check which traits still have unresolved conflicts
$unresolvedTraits = [];
foreach ($concreteTraits as $traitName => $methodInfo) {
$key = strtolower($traitName) . '::' . $lowerMethodName;
if (array_key_exists($key, $insteadofResolutions)) {
continue;
}
$unresolvedTraits[$traitName] = $methodInfo['name'];
}
if (count($unresolvedTraits) <= 1) {
continue;
}
$traitNames = array_keys($unresolvedTraits);
$methodName = reset($unresolvedTraits);
$errors[] = RuleErrorBuilder::message(sprintf(
'Trait method %s::%s() has not been applied as %s::%s(), because of collision with %s::%s().',
$traitNames[1],
$methodName,
$classReflection->getDisplayName(),
$methodName,
$traitNames[0],
$methodName,
))
->identifier('class.traitMethodCollision')
->nonIgnorable()
->build();
}
return $errors;
}
}