Skip to content

Commit 10852a0

Browse files
committed
Use ReflectionProviderStaticAccessor instead of Broker::getInstance()
1 parent 629ccf6 commit 10852a0

18 files changed

+118
-84
lines changed

src/DependencyInjection/ContainerFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use PHPStan\Command\CommandHelper;
1111
use PHPStan\File\FileHelper;
1212
use PHPStan\Php\PhpVersion;
13+
use PHPStan\Reflection\ReflectionProvider;
14+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
1315
use Symfony\Component\Finder\Finder;
1416
use function sys_get_temp_dir;
1517

@@ -120,6 +122,7 @@ public function create(
120122
/** @var Broker $broker */
121123
$broker = $container->getByType(Broker::class);
122124
Broker::registerInstance($broker);
125+
ReflectionProviderStaticAccessor::registerInstance($container->getByType(ReflectionProvider::class));
123126
$container->getService('typeSpecifier');
124127

125128
BleedingEdgeToggle::setBleedingEdge($container->parameters['featureToggles']['bleedingEdge']);

src/PhpDoc/StubValidator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\PhpDocParser\Lexer\Lexer;
1313
use PHPStan\PhpDocParser\Parser\PhpDocParser;
1414
use PHPStan\Reflection\ReflectionProvider;
15+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
1516
use PHPStan\Rules\ClassCaseSensitivityCheck;
1617
use PHPStan\Rules\Classes\ExistingClassesInClassImplementsRule;
1718
use PHPStan\Rules\Classes\ExistingClassesInInterfaceExtendsRule;
@@ -74,6 +75,7 @@ public function validate(array $stubFiles, bool $debug): array
7475
}
7576

7677
$originalBroker = Broker::getInstance();
78+
$originalReflectionProvider = ReflectionProviderStaticAccessor::getInstance();
7779
$container = $this->derivativeContainerFactory->create([
7880
__DIR__ . '/../../conf/config.stubValidator.neon',
7981
]);
@@ -113,6 +115,7 @@ static function (): void {
113115
}
114116

115117
Broker::registerInstance($originalBroker);
118+
ReflectionProviderStaticAccessor::registerInstance($originalReflectionProvider);
116119
ObjectType::resetCaches();
117120

118121
return $errors;

src/Reflection/Dummy/DummyConstantReflection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace PHPStan\Reflection\Dummy;
44

5-
use PHPStan\Broker\Broker;
65
use PHPStan\Reflection\ClassReflection;
76
use PHPStan\Reflection\ConstantReflection;
7+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
88
use PHPStan\TrinaryLogic;
99
use PHPStan\Type\MixedType;
1010
use PHPStan\Type\Type;
@@ -21,9 +21,9 @@ public function __construct(string $name)
2121

2222
public function getDeclaringClass(): ClassReflection
2323
{
24-
$broker = Broker::getInstance();
24+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
2525

26-
return $broker->getClass(\stdClass::class);
26+
return $reflectionProvider->getClass(\stdClass::class);
2727
}
2828

2929
public function getFileName(): ?string

src/Reflection/Dummy/DummyMethodReflection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace PHPStan\Reflection\Dummy;
44

5-
use PHPStan\Broker\Broker;
65
use PHPStan\Reflection\ClassMemberReflection;
76
use PHPStan\Reflection\ClassReflection;
87
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
99
use PHPStan\Reflection\TrivialParametersAcceptor;
1010
use PHPStan\TrinaryLogic;
1111
use PHPStan\Type\Type;
@@ -22,9 +22,9 @@ public function __construct(string $name)
2222

2323
public function getDeclaringClass(): ClassReflection
2424
{
25-
$broker = Broker::getInstance();
25+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
2626

27-
return $broker->getClass(\stdClass::class);
27+
return $reflectionProvider->getClass(\stdClass::class);
2828
}
2929

3030
public function isStatic(): bool

src/Reflection/Dummy/DummyPropertyReflection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace PHPStan\Reflection\Dummy;
44

5-
use PHPStan\Broker\Broker;
65
use PHPStan\Reflection\ClassReflection;
76
use PHPStan\Reflection\PropertyReflection;
7+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
88
use PHPStan\TrinaryLogic;
99
use PHPStan\Type\MixedType;
1010
use PHPStan\Type\Type;
@@ -14,9 +14,9 @@ class DummyPropertyReflection implements PropertyReflection
1414

1515
public function getDeclaringClass(): ClassReflection
1616
{
17-
$broker = Broker::getInstance();
17+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
1818

19-
return $broker->getClass(\stdClass::class);
19+
return $reflectionProvider->getClass(\stdClass::class);
2020
}
2121

2222
public function isStatic(): bool
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection;
4+
5+
class ReflectionProviderStaticAccessor
6+
{
7+
8+
private static ?ReflectionProvider $instance = null;
9+
10+
private function __construct()
11+
{
12+
}
13+
14+
public static function registerInstance(ReflectionProvider $reflectionProvider): void
15+
{
16+
self::$instance = $reflectionProvider;
17+
}
18+
19+
public static function getInstance(): ReflectionProvider
20+
{
21+
if (self::$instance === null) {
22+
throw new \PHPStan\ShouldNotHappenException();
23+
}
24+
return self::$instance;
25+
}
26+
27+
}

src/Type/ClassStringType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PHPStan\Type;
44

5-
use PHPStan\Broker\Broker;
5+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
66
use PHPStan\TrinaryLogic;
77
use PHPStan\Type\Constant\ConstantStringType;
88

@@ -28,8 +28,8 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
2828
}
2929

3030
if ($type instanceof ConstantStringType) {
31-
$broker = Broker::getInstance();
32-
return TrinaryLogic::createFromBoolean($broker->hasClass($type->getValue()));
31+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
32+
return TrinaryLogic::createFromBoolean($reflectionProvider->hasClass($type->getValue()));
3333
}
3434

3535
if ($type instanceof self) {
@@ -46,8 +46,8 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
4646
public function isSuperTypeOf(Type $type): TrinaryLogic
4747
{
4848
if ($type instanceof ConstantStringType) {
49-
$broker = Broker::getInstance();
50-
return TrinaryLogic::createFromBoolean($broker->hasClass($type->getValue()));
49+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
50+
return TrinaryLogic::createFromBoolean($reflectionProvider->hasClass($type->getValue()));
5151
}
5252

5353
if ($type instanceof self) {

src/Type/Constant/ConstantArrayType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace PHPStan\Type\Constant;
44

5-
use PHPStan\Broker\Broker;
65
use PHPStan\Reflection\ClassMemberAccessAnswerer;
76
use PHPStan\Reflection\InaccessibleMethod;
7+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
88
use PHPStan\Reflection\TrivialParametersAcceptor;
99
use PHPStan\TrinaryLogic;
1010
use PHPStan\Type\Accessory\NonEmptyArrayType;
@@ -368,11 +368,11 @@ public function findTypeAndMethodName(): ?ConstantArrayTypeAndMethod
368368
}
369369

370370
if ($classOrObject instanceof ConstantStringType) {
371-
$broker = Broker::getInstance();
372-
if (!$broker->hasClass($classOrObject->getValue())) {
371+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
372+
if (!$reflectionProvider->hasClass($classOrObject->getValue())) {
373373
return ConstantArrayTypeAndMethod::createUnknown();
374374
}
375-
$type = new ObjectType($broker->getClass($classOrObject->getValue())->getName());
375+
$type = new ObjectType($reflectionProvider->getClass($classOrObject->getValue())->getName());
376376
} elseif ($classOrObject instanceof GenericClassStringType) {
377377
$type = $classOrObject->getGenericType();
378378
} elseif ((new \PHPStan\Type\ObjectWithoutClassType())->isSuperTypeOf($classOrObject)->yes()) {

src/Type/Constant/ConstantStringType.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace PHPStan\Type\Constant;
44

55
use PhpParser\Node\Name;
6-
use PHPStan\Broker\Broker;
76
use PHPStan\Reflection\ClassMemberAccessAnswerer;
87
use PHPStan\Reflection\InaccessibleMethod;
8+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
99
use PHPStan\Reflection\TrivialParametersAcceptor;
1010
use PHPStan\TrinaryLogic;
1111
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
@@ -118,9 +118,9 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
118118
return TrinaryLogic::createNo();
119119
}
120120
if ($type instanceof ClassStringType) {
121-
$broker = Broker::getInstance();
121+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
122122

123-
return $broker->hasClass($this->getValue()) ? TrinaryLogic::createMaybe() : TrinaryLogic::createNo();
123+
return $reflectionProvider->hasClass($this->getValue()) ? TrinaryLogic::createMaybe() : TrinaryLogic::createNo();
124124
}
125125

126126
if ($type instanceof self) {
@@ -144,21 +144,21 @@ public function isCallable(): TrinaryLogic
144144
return TrinaryLogic::createNo();
145145
}
146146

147-
$broker = Broker::getInstance();
147+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
148148

149149
// 'my_function'
150-
if ($broker->hasFunction(new Name($this->value), null)) {
150+
if ($reflectionProvider->hasFunction(new Name($this->value), null)) {
151151
return TrinaryLogic::createYes();
152152
}
153153

154154
// 'MyClass::myStaticFunction'
155155
$matches = \Nette\Utils\Strings::match($this->value, '#^([a-zA-Z_\\x7f-\\xff\\\\][a-zA-Z0-9_\\x7f-\\xff\\\\]*)::([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\z#');
156156
if ($matches !== null) {
157-
if (!$broker->hasClass($matches[1])) {
157+
if (!$reflectionProvider->hasClass($matches[1])) {
158158
return TrinaryLogic::createMaybe();
159159
}
160160

161-
$classRef = $broker->getClass($matches[1]);
161+
$classRef = $reflectionProvider->getClass($matches[1]);
162162
if ($classRef->hasMethod($matches[2])) {
163163
return TrinaryLogic::createYes();
164164
}
@@ -179,22 +179,22 @@ public function isCallable(): TrinaryLogic
179179
*/
180180
public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
181181
{
182-
$broker = Broker::getInstance();
182+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
183183

184184
// 'my_function'
185185
$functionName = new Name($this->value);
186-
if ($broker->hasFunction($functionName, null)) {
187-
return $broker->getFunction($functionName, null)->getVariants();
186+
if ($reflectionProvider->hasFunction($functionName, null)) {
187+
return $reflectionProvider->getFunction($functionName, null)->getVariants();
188188
}
189189

190190
// 'MyClass::myStaticFunction'
191191
$matches = \Nette\Utils\Strings::match($this->value, '#^([a-zA-Z_\\x7f-\\xff\\\\][a-zA-Z0-9_\\x7f-\\xff\\\\]*)::([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\z#');
192192
if ($matches !== null) {
193-
if (!$broker->hasClass($matches[1])) {
193+
if (!$reflectionProvider->hasClass($matches[1])) {
194194
return [new TrivialParametersAcceptor()];
195195
}
196196

197-
$classReflection = $broker->getClass($matches[1]);
197+
$classReflection = $reflectionProvider->getClass($matches[1]);
198198
if ($classReflection->hasMethod($matches[2])) {
199199
$method = $classReflection->getMethod($matches[2], $scope);
200200
if (!$scope->canCallMethod($method)) {

src/Type/Generic/GenericClassStringType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PHPStan\Type\Generic;
44

5-
use PHPStan\Broker\Broker;
5+
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
66
use PHPStan\TrinaryLogic;
77
use PHPStan\Type\ClassStringType;
88
use PHPStan\Type\CompoundType;
@@ -53,8 +53,8 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
5353
}
5454

5555
if ($type instanceof ConstantStringType) {
56-
$broker = Broker::getInstance();
57-
if (!$broker->hasClass($type->getValue())) {
56+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
57+
if (!$reflectionProvider->hasClass($type->getValue())) {
5858
return TrinaryLogic::createNo();
5959
}
6060

0 commit comments

Comments
 (0)