-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathConstantFetchCollector.php
213 lines (176 loc) · 6.71 KB
/
ConstantFetchCollector.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php declare(strict_types = 1);
namespace ShipMonk\PHPStan\DeadCode\Collector;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use ShipMonk\PHPStan\DeadCode\Excluder\MemberUsageExcluder;
use ShipMonk\PHPStan\DeadCode\Graph\ClassConstantRef;
use ShipMonk\PHPStan\DeadCode\Graph\ClassConstantUsage;
use ShipMonk\PHPStan\DeadCode\Graph\CollectedUsage;
use ShipMonk\PHPStan\DeadCode\Graph\UsageOriginDetector;
use function array_map;
use function count;
use function current;
use function explode;
use function strpos;
/**
* @implements Collector<Node, list<string>>
*/
class ConstantFetchCollector implements Collector
{
use BufferedUsageCollector;
private UsageOriginDetector $usageOriginDetector;
private ReflectionProvider $reflectionProvider;
private bool $trackMixedAccess;
/**
* @var list<MemberUsageExcluder>
*/
private array $memberUsageExcluders;
/**
* @param list<MemberUsageExcluder> $memberUsageExcluders
*/
public function __construct(
UsageOriginDetector $usageOriginDetector,
ReflectionProvider $reflectionProvider,
bool $trackMixedAccess,
array $memberUsageExcluders
)
{
$this->reflectionProvider = $reflectionProvider;
$this->trackMixedAccess = $trackMixedAccess;
$this->usageOriginDetector = $usageOriginDetector;
$this->memberUsageExcluders = $memberUsageExcluders;
}
public function getNodeType(): string
{
return Node::class;
}
/**
* @return non-empty-list<string>|null
*/
public function processNode(
Node $node,
Scope $scope
): ?array
{
if ($node instanceof ClassConstFetch) {
$this->registerFetch($node, $scope);
}
if ($node instanceof FuncCall) {
$this->registerFunctionCall($node, $scope);
}
return $this->tryFlushBuffer($node, $scope);
}
private function registerFunctionCall(FuncCall $node, Scope $scope): void
{
if (count($node->args) !== 1) {
return;
}
/** @var Arg $firstArg */
$firstArg = current($node->args);
if ($node->name instanceof Name) {
$functionNames = [$node->name->toString()];
} else {
$nameType = $scope->getType($node->name);
$functionNames = array_map(static fn (ConstantStringType $string): string => $string->getValue(), $nameType->getConstantStrings());
}
foreach ($functionNames as $functionName) {
if ($functionName !== 'constant') {
continue;
}
$argumentType = $scope->getType($firstArg->value);
foreach ($argumentType->getConstantStrings() as $constantString) {
if (strpos($constantString->getValue(), '::') === false) {
continue;
}
// @phpstan-ignore offsetAccess.notFound
[$className, $constantName] = explode('::', $constantString->getValue());
if ($this->reflectionProvider->hasClass($className)) {
$reflection = $this->reflectionProvider->getClass($className);
if ($reflection->hasConstant($constantName)) {
$className = $reflection->getConstant($constantName)->getDeclaringClass()->getName();
}
}
$this->registerUsage(
new ClassConstantUsage(
$this->usageOriginDetector->detectOrigin($scope),
new ClassConstantRef($className, $constantName, true),
),
$node,
$scope,
);
}
}
}
private function registerFetch(ClassConstFetch $node, Scope $scope): void
{
if ($node->class instanceof Expr) {
$ownerType = $scope->getType($node->class);
$possibleDescendantFetch = true;
} else {
$ownerType = $scope->resolveTypeByName($node->class);
$possibleDescendantFetch = $node->class->toString() === 'static';
}
$constantNames = $node->name instanceof Expr
? array_map(static fn (ConstantStringType $string): string => $string->getValue(), $scope->getType($node->name)->getConstantStrings())
: [$node->name->toString()];
foreach ($constantNames as $constantName) {
if ($constantName === 'class') {
continue; // reserved for class name fetching
}
foreach ($this->getDeclaringTypesWithConstant($ownerType, $constantName) as $className) {
$this->registerUsage(
new ClassConstantUsage(
$this->usageOriginDetector->detectOrigin($scope),
new ClassConstantRef($className, $constantName, $possibleDescendantFetch),
),
$node,
$scope,
);
}
}
}
/**
* @return list<class-string<object>|null>
*/
private function getDeclaringTypesWithConstant(
Type $type,
string $constantName
): array
{
$typeNormalized = TypeUtils::toBenevolentUnion($type); // extract possible calls even from Class|int
$classReflections = $typeNormalized->getObjectTypeOrClassStringObjectType()->getObjectClassReflections();
$result = [];
foreach ($classReflections as $classReflection) {
if ($classReflection->hasConstant($constantName)) {
$result[] = $classReflection->getConstant($constantName)->getDeclaringClass()->getName();
} else { // unknown constant fetch (might be present on children)
$result[] = $classReflection->getName();
}
}
if ($this->trackMixedAccess && $result === []) {
$result[] = null; // call over unknown type
}
return $result;
}
private function registerUsage(ClassConstantUsage $usage, Node $node, Scope $scope): void
{
$excluderName = null;
foreach ($this->memberUsageExcluders as $excludedUsageDecider) {
if ($excludedUsageDecider->shouldExclude($usage, $node, $scope)) {
$excluderName = $excludedUsageDecider->getIdentifier();
break;
}
}
$this->usageBuffer[] = new CollectedUsage($usage, $excluderName);
}
}