Skip to content

Commit 8b0dba5

Browse files
committed
Use ReflectionProvider as much as possible
1 parent 98bf387 commit 8b0dba5

File tree

46 files changed

+340
-304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+340
-304
lines changed

src/Analyser/MutatingScope.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
3737
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
3838
use PHPStan\Reflection\PropertyReflection;
39+
use PHPStan\Reflection\ReflectionProvider;
3940
use PHPStan\Rules\Properties\PropertyReflectionFinder;
4041
use PHPStan\TrinaryLogic;
4142
use PHPStan\Type\ArrayType;
@@ -99,6 +100,9 @@ class MutatingScope implements Scope
99100
/** @var \PHPStan\Broker\Broker */
100101
private $broker;
101102

103+
/** @var \PHPStan\Reflection\ReflectionProvider */
104+
private $reflectionProvider;
105+
102106
/** @var \PhpParser\PrettyPrinter\Standard */
103107
private $printer;
104108

@@ -147,6 +151,7 @@ class MutatingScope implements Scope
147151
/**
148152
* @param \PHPStan\Analyser\ScopeFactory $scopeFactory
149153
* @param \PHPStan\Broker\Broker $broker
154+
* @param ReflectionProvider $reflectionProvider
150155
* @param \PhpParser\PrettyPrinter\Standard $printer
151156
* @param \PHPStan\Analyser\TypeSpecifier $typeSpecifier
152157
* @param \PHPStan\Rules\Properties\PropertyReflectionFinder $propertyReflectionFinder
@@ -165,6 +170,7 @@ class MutatingScope implements Scope
165170
public function __construct(
166171
ScopeFactory $scopeFactory,
167172
Broker $broker,
173+
ReflectionProvider $reflectionProvider,
168174
\PhpParser\PrettyPrinter\Standard $printer,
169175
TypeSpecifier $typeSpecifier,
170176
PropertyReflectionFinder $propertyReflectionFinder,
@@ -187,6 +193,7 @@ public function __construct(
187193

188194
$this->scopeFactory = $scopeFactory;
189195
$this->broker = $broker;
196+
$this->reflectionProvider = $reflectionProvider;
190197
$this->printer = $printer;
191198
$this->typeSpecifier = $typeSpecifier;
192199
$this->propertyReflectionFinder = $propertyReflectionFinder;
@@ -1383,11 +1390,11 @@ private function resolveType(Expr $node): Type
13831390
$referencedClasses = TypeUtils::getDirectClassNames($constantClassType);
13841391
$types = [];
13851392
foreach ($referencedClasses as $referencedClass) {
1386-
if (!$this->broker->hasClass($referencedClass)) {
1393+
if (!$this->reflectionProvider->hasClass($referencedClass)) {
13871394
continue;
13881395
}
13891396

1390-
$propertyClassReflection = $this->broker->getClass($referencedClass);
1397+
$propertyClassReflection = $this->reflectionProvider->getClass($referencedClass);
13911398
if (!$propertyClassReflection->hasConstant($constantName)) {
13921399
continue;
13931400
}
@@ -1656,11 +1663,11 @@ private function resolveType(Expr $node): Type
16561663
)->getReturnType();
16571664
}
16581665

1659-
if (!$this->broker->hasFunction($node->name, $this)) {
1666+
if (!$this->reflectionProvider->hasFunction($node->name, $this)) {
16601667
return new ErrorType();
16611668
}
16621669

1663-
$functionReflection = $this->broker->getFunction($node->name, $this);
1670+
$functionReflection = $this->reflectionProvider->getFunction($node->name, $this);
16641671
foreach ($this->broker->getDynamicFunctionReturnTypeExtensions() as $dynamicFunctionReturnTypeExtension) {
16651672
if (!$dynamicFunctionReturnTypeExtension->isFunctionSupported($functionReflection)) {
16661673
continue;
@@ -3171,8 +3178,8 @@ private function canAccessClassMember(ClassMemberReflection $classMemberReflecti
31713178
return true;
31723179
}
31733180

3174-
if ($this->inClosureBindScopeClass !== null && $this->broker->hasClass($this->inClosureBindScopeClass)) {
3175-
$currentClassReflection = $this->broker->getClass($this->inClosureBindScopeClass);
3181+
if ($this->inClosureBindScopeClass !== null && $this->reflectionProvider->hasClass($this->inClosureBindScopeClass)) {
3182+
$currentClassReflection = $this->reflectionProvider->getClass($this->inClosureBindScopeClass);
31763183
} elseif ($this->isInClass()) {
31773184
$currentClassReflection = $this->getClassReflection();
31783185
} else {
@@ -3225,11 +3232,11 @@ private function exactInstantiation(New_ $node, string $className): ?Type
32253232
return null;
32263233
}
32273234

3228-
if (!$this->broker->hasClass($resolvedClassName)) {
3235+
if (!$this->reflectionProvider->hasClass($resolvedClassName)) {
32293236
return null;
32303237
}
32313238

3232-
$classReflection = $this->broker->getClass($resolvedClassName);
3239+
$classReflection = $this->reflectionProvider->getClass($resolvedClassName);
32333240
if ($classReflection->hasConstructor()) {
32343241
$constructorMethod = $classReflection->getConstructor();
32353242
} else {

src/Analyser/NodeScopeResolver.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use PhpParser\Node\Stmt\TryCatch;
4646
use PhpParser\Node\Stmt\Unset_;
4747
use PhpParser\Node\Stmt\While_;
48-
use PHPStan\Broker\Broker;
4948
use PHPStan\File\FileHelper;
5049
use PHPStan\Node\ClosureReturnStatementsNode;
5150
use PHPStan\Node\ExecutionEndNode;
@@ -69,6 +68,7 @@
6968
use PHPStan\Reflection\ParametersAcceptorSelector;
7069
use PHPStan\Reflection\PassedByReference;
7170
use PHPStan\Reflection\Php\DummyParameter;
71+
use PHPStan\Reflection\ReflectionProvider;
7272
use PHPStan\Type\Accessory\NonEmptyArrayType;
7373
use PHPStan\Type\ArrayType;
7474
use PHPStan\Type\CallableType;
@@ -102,8 +102,8 @@ class NodeScopeResolver
102102
private const LOOP_SCOPE_ITERATIONS = 3;
103103
private const GENERALIZE_AFTER_ITERATION = 1;
104104

105-
/** @var \PHPStan\Broker\Broker */
106-
private $broker;
105+
/** @var \PHPStan\Reflection\ReflectionProvider */
106+
private $reflectionProvider;
107107

108108
/** @var \PHPStan\Parser\Parser */
109109
private $parser;
@@ -136,7 +136,7 @@ class NodeScopeResolver
136136
private $analysedFiles;
137137

138138
/**
139-
* @param Broker $broker
139+
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
140140
* @param Parser $parser
141141
* @param FileTypeMapper $fileTypeMapper
142142
* @param FileHelper $fileHelper
@@ -148,7 +148,7 @@ class NodeScopeResolver
148148
* @param array<int, string> $earlyTerminatingFunctionCalls
149149
*/
150150
public function __construct(
151-
Broker $broker,
151+
ReflectionProvider $reflectionProvider,
152152
Parser $parser,
153153
FileTypeMapper $fileTypeMapper,
154154
FileHelper $fileHelper,
@@ -160,7 +160,7 @@ public function __construct(
160160
array $earlyTerminatingFunctionCalls
161161
)
162162
{
163-
$this->broker = $broker;
163+
$this->reflectionProvider = $reflectionProvider;
164164
$this->parser = $parser;
165165
$this->fileTypeMapper = $fileTypeMapper;
166166
$this->fileHelper = $fileHelper;
@@ -490,12 +490,12 @@ private function processStmtNode(
490490
} elseif ($stmt instanceof Node\Stmt\ClassLike) {
491491
$hasYield = false;
492492
if (isset($stmt->namespacedName)) {
493-
$classScope = $scope->enterClass($this->broker->getClass((string) $stmt->namespacedName));
493+
$classScope = $scope->enterClass($this->reflectionProvider->getClass((string) $stmt->namespacedName));
494494
} elseif ($stmt instanceof Class_) {
495495
if ($stmt->name === null) {
496496
throw new \PHPStan\ShouldNotHappenException();
497497
}
498-
$classScope = $scope->enterClass($this->broker->getClass($stmt->name->toString()));
498+
$classScope = $scope->enterClass($this->reflectionProvider->getClass($stmt->name->toString()));
499499
} else {
500500
throw new \PHPStan\ShouldNotHappenException();
501501
}
@@ -1188,11 +1188,11 @@ private function findEarlyTerminatingExpr(Expr $expr, Scope $scope): ?Expr
11881188

11891189
$directClassNames = TypeUtils::getDirectClassNames($methodCalledOnType);
11901190
foreach ($directClassNames as $referencedClass) {
1191-
if (!$this->broker->hasClass($referencedClass)) {
1191+
if (!$this->reflectionProvider->hasClass($referencedClass)) {
11921192
continue;
11931193
}
11941194

1195-
$classReflection = $this->broker->getClass($referencedClass);
1195+
$classReflection = $this->reflectionProvider->getClass($referencedClass);
11961196
foreach (array_merge([$referencedClass], $classReflection->getParentClassesNames(), $classReflection->getNativeReflection()->getInterfaceNames()) as $className) {
11971197
if (!isset($this->earlyTerminatingMethodCalls[$className])) {
11981198
continue;
@@ -1345,8 +1345,8 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
13451345
$functionReflection = null;
13461346
if ($expr->name instanceof Expr) {
13471347
$scope = $this->processExprNode($expr->name, $scope, $nodeCallback, $context->enterDeep())->getScope();
1348-
} elseif ($this->broker->hasFunction($expr->name, $scope)) {
1349-
$functionReflection = $this->broker->getFunction($expr->name, $scope);
1348+
} elseif ($this->reflectionProvider->hasFunction($expr->name, $scope)) {
1349+
$functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope);
13501350
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
13511351
$scope,
13521352
$expr->args,
@@ -1509,8 +1509,8 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
15091509
$scope = $result->getScope();
15101510
} elseif ($expr->class instanceof Name) {
15111511
$className = $scope->resolveName($expr->class);
1512-
if ($this->broker->hasClass($className)) {
1513-
$classReflection = $this->broker->getClass($className);
1512+
if ($this->reflectionProvider->hasClass($className)) {
1513+
$classReflection = $this->reflectionProvider->getClass($className);
15141514
if (is_string($expr->name)) {
15151515
$methodName = $expr->name;
15161516
} else {
@@ -1777,10 +1777,10 @@ static function () use ($expr, $rightResult): MutatingScope {
17771777
$scope = $result->getScope();
17781778
$hasYield = $result->hasYield();
17791779
} elseif ($expr->class instanceof Class_) {
1780-
$this->broker->getAnonymousClassReflection($expr->class, $scope); // populates $expr->class->name
1780+
$this->reflectionProvider->getAnonymousClassReflection($expr->class, $scope); // populates $expr->class->name
17811781
$this->processStmtNode($expr->class, $scope, $nodeCallback);
1782-
} elseif ($this->broker->hasClass($expr->class->toString())) {
1783-
$classReflection = $this->broker->getClass($expr->class->toString());
1782+
} elseif ($this->reflectionProvider->hasClass($expr->class->toString())) {
1783+
$classReflection = $this->reflectionProvider->getClass($expr->class->toString());
17841784
if ($classReflection->hasConstructor()) {
17851785
$constructorReflection = $classReflection->getConstructor();
17861786
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
@@ -2458,10 +2458,10 @@ private function processTraitUse(Node\Stmt\TraitUse $node, MutatingScope $classS
24582458
{
24592459
foreach ($node->traits as $trait) {
24602460
$traitName = (string) $trait;
2461-
if (!$this->broker->hasClass($traitName)) {
2461+
if (!$this->reflectionProvider->hasClass($traitName)) {
24622462
continue;
24632463
}
2464-
$traitReflection = $this->broker->getClass($traitName);
2464+
$traitReflection = $this->reflectionProvider->getClass($traitName);
24652465
$traitFileName = $traitReflection->getFileName();
24662466
if ($traitFileName === false) {
24672467
continue; // trait from eval or from PHP itself

src/Analyser/ScopeFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Broker\Broker;
66
use PHPStan\DependencyInjection\Container;
77
use PHPStan\Reflection\ParametersAcceptor;
8+
use PHPStan\Reflection\ReflectionProvider;
89
use PHPStan\Rules\Properties\PropertyReflectionFinder;
910

1011
class ScopeFactory
@@ -16,6 +17,9 @@ class ScopeFactory
1617
/** @var \PHPStan\Broker\Broker */
1718
private $broker;
1819

20+
/** @var \PHPStan\Reflection\ReflectionProvider */
21+
private $reflectionProvider;
22+
1923
/** @var \PhpParser\PrettyPrinter\Standard */
2024
private $printer;
2125

@@ -31,6 +35,7 @@ class ScopeFactory
3135
public function __construct(
3236
string $scopeClass,
3337
Broker $broker,
38+
ReflectionProvider $reflectionProvider,
3439
\PhpParser\PrettyPrinter\Standard $printer,
3540
TypeSpecifier $typeSpecifier,
3641
PropertyReflectionFinder $propertyReflectionFinder,
@@ -39,6 +44,7 @@ public function __construct(
3944
{
4045
$this->scopeClass = $scopeClass;
4146
$this->broker = $broker;
47+
$this->reflectionProvider = $reflectionProvider;
4248
$this->printer = $printer;
4349
$this->typeSpecifier = $typeSpecifier;
4450
$this->propertyReflectionFinder = $propertyReflectionFinder;
@@ -80,6 +86,7 @@ public function create(
8086
return new $scopeClass(
8187
$this,
8288
$this->broker,
89+
$this->reflectionProvider,
8390
$this->printer,
8491
$this->typeSpecifier,
8592
$this->propertyReflectionFinder,

src/Analyser/TypeSpecifier.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use PhpParser\Node\Expr\StaticCall;
1919
use PhpParser\Node\Expr\StaticPropertyFetch;
2020
use PhpParser\Node\Name;
21-
use PHPStan\Broker\Broker;
21+
use PHPStan\Reflection\ReflectionProvider;
2222
use PHPStan\Type\Accessory\HasOffsetType;
2323
use PHPStan\Type\Accessory\HasPropertyType;
2424
use PHPStan\Type\Accessory\NonEmptyArrayType;
@@ -53,8 +53,8 @@ class TypeSpecifier
5353
/** @var \PhpParser\PrettyPrinter\Standard */
5454
private $printer;
5555

56-
/** @var \PHPStan\Broker\Broker */
57-
private $broker;
56+
/** @var ReflectionProvider */
57+
private $reflectionProvider;
5858

5959
/** @var \PHPStan\Type\FunctionTypeSpecifyingExtension[] */
6060
private $functionTypeSpecifyingExtensions = [];
@@ -73,21 +73,21 @@ class TypeSpecifier
7373

7474
/**
7575
* @param \PhpParser\PrettyPrinter\Standard $printer
76-
* @param \PHPStan\Broker\Broker $broker
76+
* @param ReflectionProvider $reflectionProvider
7777
* @param \PHPStan\Type\FunctionTypeSpecifyingExtension[] $functionTypeSpecifyingExtensions
7878
* @param \PHPStan\Type\MethodTypeSpecifyingExtension[] $methodTypeSpecifyingExtensions
7979
* @param \PHPStan\Type\StaticMethodTypeSpecifyingExtension[] $staticMethodTypeSpecifyingExtensions
8080
*/
8181
public function __construct(
8282
\PhpParser\PrettyPrinter\Standard $printer,
83-
Broker $broker,
83+
ReflectionProvider $reflectionProvider,
8484
array $functionTypeSpecifyingExtensions,
8585
array $methodTypeSpecifyingExtensions,
8686
array $staticMethodTypeSpecifyingExtensions
8787
)
8888
{
8989
$this->printer = $printer;
90-
$this->broker = $broker;
90+
$this->reflectionProvider = $reflectionProvider;
9191

9292
foreach (array_merge($functionTypeSpecifyingExtensions, $methodTypeSpecifyingExtensions, $staticMethodTypeSpecifyingExtensions) as $extension) {
9393
if (!($extension instanceof TypeSpecifierAwareExtension)) {
@@ -418,8 +418,8 @@ public function specifyTypesInCondition(
418418
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\SmallerOrEqual($expr->right, $expr->left), $context, $defaultHandleFunctions);
419419

420420
} elseif ($expr instanceof FuncCall && $expr->name instanceof Name) {
421-
if ($this->broker->hasFunction($expr->name, $scope)) {
422-
$functionReflection = $this->broker->getFunction($expr->name, $scope);
421+
if ($this->reflectionProvider->hasFunction($expr->name, $scope)) {
422+
$functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope);
423423
foreach ($this->getFunctionTypeSpecifyingExtensions() as $extension) {
424424
if (!$extension->isFunctionSupported($functionReflection, $expr, $context)) {
425425
continue;
@@ -437,9 +437,9 @@ public function specifyTypesInCondition(
437437
$referencedClasses = TypeUtils::getDirectClassNames($methodCalledOnType);
438438
if (
439439
count($referencedClasses) === 1
440-
&& $this->broker->hasClass($referencedClasses[0])
440+
&& $this->reflectionProvider->hasClass($referencedClasses[0])
441441
) {
442-
$methodClassReflection = $this->broker->getClass($referencedClasses[0]);
442+
$methodClassReflection = $this->reflectionProvider->getClass($referencedClasses[0]);
443443
if ($methodClassReflection->hasMethod($expr->name->name)) {
444444
$methodReflection = $methodClassReflection->getMethod($expr->name->name, $scope);
445445
foreach ($this->getMethodTypeSpecifyingExtensionsForClass($methodClassReflection->getName()) as $extension) {
@@ -467,9 +467,9 @@ public function specifyTypesInCondition(
467467
$referencedClasses = TypeUtils::getDirectClassNames($calleeType);
468468
if (
469469
count($referencedClasses) === 1
470-
&& $this->broker->hasClass($referencedClasses[0])
470+
&& $this->reflectionProvider->hasClass($referencedClasses[0])
471471
) {
472-
$staticMethodClassReflection = $this->broker->getClass($referencedClasses[0]);
472+
$staticMethodClassReflection = $this->reflectionProvider->getClass($referencedClasses[0]);
473473
foreach ($this->getStaticMethodTypeSpecifyingExtensionsForClass($staticMethodClassReflection->getName()) as $extension) {
474474
if (!$extension->isStaticMethodSupported($staticMethodReflection, $expr, $context)) {
475475
continue;
@@ -760,7 +760,7 @@ private function getStaticMethodTypeSpecifyingExtensionsForClass(string $classNa
760760
private function getTypeSpecifyingExtensionsForType(array $extensions, string $className): array
761761
{
762762
$extensionsForClass = [[]];
763-
$class = $this->broker->getClass($className);
763+
$class = $this->reflectionProvider->getClass($className);
764764
foreach (array_merge([$className], $class->getParentClassesNames(), $class->getNativeReflection()->getInterfaceNames()) as $extensionClassName) {
765765
if (!isset($extensions[$extensionClassName])) {
766766
continue;

src/Analyser/TypeSpecifierFactory.php

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

55
use PhpParser\PrettyPrinter\Standard;
6-
use PHPStan\Broker\Broker;
76
use PHPStan\Broker\BrokerFactory;
87
use PHPStan\DependencyInjection\Container;
8+
use PHPStan\Reflection\ReflectionProvider;
99

1010
class TypeSpecifierFactory
1111
{
@@ -26,7 +26,7 @@ public function create(): TypeSpecifier
2626
{
2727
$typeSpecifier = new TypeSpecifier(
2828
$this->container->getByType(Standard::class),
29-
$this->container->getByType(Broker::class),
29+
$this->container->getByType(ReflectionProvider::class),
3030
$this->container->getServicesByTag(self::FUNCTION_TYPE_SPECIFYING_EXTENSION_TAG),
3131
$this->container->getServicesByTag(self::METHOD_TYPE_SPECIFYING_EXTENSION_TAG),
3232
$this->container->getServicesByTag(self::STATIC_METHOD_TYPE_SPECIFYING_EXTENSION_TAG)

0 commit comments

Comments
 (0)