-
Notifications
You must be signed in to change notification settings - Fork 428
/
ClassBlacklistReflectionProvider.php
169 lines (136 loc) · 4.61 KB
/
ClassBlacklistReflectionProvider.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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\ReflectionProvider;
use Nette\Utils\Strings;
use PHPStan\Analyser\Scope;
use PHPStan\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\GlobalConstantReflection;
use PHPStan\Reflection\ReflectionProvider;
class ClassBlacklistReflectionProvider implements ReflectionProvider
{
private ReflectionProvider $reflectionProvider;
private PhpStormStubsSourceStubber $phpStormStubsSourceStubber;
/** @var string[] */
private array $patterns;
private ?string $singleReflectionInsteadOfFile;
/**
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
* @param string[] $patterns
*/
public function __construct(
ReflectionProvider $reflectionProvider,
PhpStormStubsSourceStubber $phpStormStubsSourceStubber,
array $patterns,
?string $singleReflectionInsteadOfFile
)
{
$this->reflectionProvider = $reflectionProvider;
$this->phpStormStubsSourceStubber = $phpStormStubsSourceStubber;
$this->patterns = $patterns;
$this->singleReflectionInsteadOfFile = $singleReflectionInsteadOfFile;
}
public function hasClass(string $className): bool
{
if ($this->isClassBlacklisted($className)) {
return false;
}
$has = $this->reflectionProvider->hasClass($className);
if (!$has) {
return false;
}
$classReflection = $this->reflectionProvider->getClass($className);
if ($this->singleReflectionInsteadOfFile !== null) {
if ($classReflection->getFileName() === $this->singleReflectionInsteadOfFile) {
return false;
}
}
foreach ($classReflection->getParentClassesNames() as $parentClassName) {
if ($this->isClassBlacklisted($parentClassName)) {
return false;
}
}
foreach ($classReflection->getNativeReflection()->getInterfaceNames() as $interfaceName) {
if ($this->isClassBlacklisted($interfaceName)) {
return false;
}
}
return true;
}
private function isClassBlacklisted(string $className): bool
{
if ($this->phpStormStubsSourceStubber->hasClass($className)) {
// check that userland class isn't aliased to the same name as a class from stubs
if (!class_exists($className, false)) {
return true;
}
if (in_array(strtolower($className), ['reflectionuniontype', 'attribute', 'returntypewillchange'], true)) {
return true;
}
$reflection = new \ReflectionClass($className);
if ($reflection->getFileName() === false) {
return true;
}
}
foreach ($this->patterns as $pattern) {
if (Strings::match($className, $pattern) !== null) {
return true;
}
}
return false;
}
public function getClass(string $className): ClassReflection
{
if (!$this->hasClass($className)) {
throw new \PHPStan\Broker\ClassNotFoundException($className);
}
return $this->reflectionProvider->getClass($className);
}
public function getClassName(string $className): string
{
if (!$this->hasClass($className)) {
throw new \PHPStan\Broker\ClassNotFoundException($className);
}
return $this->reflectionProvider->getClassName($className);
}
public function supportsAnonymousClasses(): bool
{
return false;
}
public function getAnonymousClassReflection(\PhpParser\Node\Stmt\Class_ $classNode, Scope $scope): ClassReflection
{
throw new \PHPStan\ShouldNotHappenException();
}
public function hasFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope): bool
{
$has = $this->reflectionProvider->hasFunction($nameNode, $scope);
if (!$has) {
return false;
}
if ($this->singleReflectionInsteadOfFile === null) {
return true;
}
$functionReflection = $this->reflectionProvider->getFunction($nameNode, $scope);
return $functionReflection->getFileName() !== $this->singleReflectionInsteadOfFile;
}
public function getFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope): FunctionReflection
{
return $this->reflectionProvider->getFunction($nameNode, $scope);
}
public function resolveFunctionName(\PhpParser\Node\Name $nameNode, ?Scope $scope): ?string
{
return $this->reflectionProvider->resolveFunctionName($nameNode, $scope);
}
public function hasConstant(\PhpParser\Node\Name $nameNode, ?Scope $scope): bool
{
return $this->reflectionProvider->hasConstant($nameNode, $scope);
}
public function getConstant(\PhpParser\Node\Name $nameNode, ?Scope $scope): GlobalConstantReflection
{
return $this->reflectionProvider->getConstant($nameNode, $scope);
}
public function resolveConstantName(\PhpParser\Node\Name $nameNode, ?Scope $scope): ?string
{
return $this->reflectionProvider->resolveConstantName($nameNode, $scope);
}
}