Skip to content

Commit 001d465

Browse files
authored
Faster LazyInternalScopeFactory->create() (#5043)
1 parent 479ff62 commit 001d465

1 file changed

Lines changed: 53 additions & 12 deletions

File tree

src/Analyser/LazyInternalScopeFactory.php

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use PHPStan\Reflection\ReflectionProvider;
1818
use PHPStan\Rules\Properties\PropertyReflectionFinder;
1919
use PHPStan\Type\ClosureType;
20+
use PHPStan\Type\DynamicReturnTypeExtensionRegistry;
21+
use PHPStan\Type\ExpressionTypeResolverExtensionRegistry;
2022

2123
#[GenerateFactory(interface: InternalScopeFactoryFactory::class, resultType: LazyInternalScopeFactory::class)]
2224
final class LazyInternalScopeFactory implements InternalScopeFactory
@@ -27,6 +29,30 @@ final class LazyInternalScopeFactory implements InternalScopeFactory
2729

2830
private Parser $currentSimpleVersionParser;
2931

32+
private ?ReflectionProvider $reflectionProvider = null;
33+
34+
private ?InitializerExprTypeResolver $initializerExprTypeResolver = null;
35+
36+
private ?DynamicReturnTypeExtensionRegistry $dynamicReturnTypeExtensionRegistry = null;
37+
38+
private ?ExpressionTypeResolverExtensionRegistry $expressionTypeResolverExtensionRegistry = null;
39+
40+
private ?ExprPrinter $exprPrinter = null;
41+
42+
private ?TypeSpecifier $typeSpecifier = null;
43+
44+
private ?PropertyReflectionFinder $propertyReflectionFinder = null;
45+
46+
private ?NodeScopeResolver $nodeScopeResolver = null;
47+
48+
private ?RicherScopeGetTypeHelper $richerScopeGetTypeHelper = null;
49+
50+
private ?ConstantResolver $constantResolver = null;
51+
52+
private ?PhpVersion $phpVersionType = null;
53+
54+
private ?AttributeReflectionFactory $attributeReflectionFactory = null;
55+
3056
/**
3157
* @param callable(Node $node, Scope $scope): void|null $nodeCallback
3258
*/
@@ -64,22 +90,37 @@ public function create(
6490
$className = FiberScope::class;
6591
}
6692

93+
$this->reflectionProvider ??= $this->container->getByType(ReflectionProvider::class);
94+
$this->initializerExprTypeResolver ??= $this->container->getByType(InitializerExprTypeResolver::class);
95+
$this->dynamicReturnTypeExtensionRegistry ??= $this->container->getByType(DynamicReturnTypeExtensionRegistryProvider::class)->getRegistry();
96+
$this->expressionTypeResolverExtensionRegistry ??= $this->container->getByType(ExpressionTypeResolverExtensionRegistryProvider::class)->getRegistry();
97+
$this->exprPrinter ??= $this->container->getByType(ExprPrinter::class);
98+
$this->typeSpecifier ??= $this->container->getByType(TypeSpecifier::class);
99+
$this->propertyReflectionFinder ??= $this->container->getByType(PropertyReflectionFinder::class);
100+
101+
$this->nodeScopeResolver ??= $this->container->getByType(NodeScopeResolver::class);
102+
$this->richerScopeGetTypeHelper ??= $this->container->getByType(RicherScopeGetTypeHelper::class);
103+
$this->constantResolver ??= $this->container->getByType(ConstantResolver::class);
104+
105+
$this->phpVersionType ??= $this->container->getByType(PhpVersion::class);
106+
$this->attributeReflectionFactory ??= $this->container->getByType(AttributeReflectionFactory::class);
107+
67108
return new $className(
68109
$this,
69-
$this->container->getByType(ReflectionProvider::class),
70-
$this->container->getByType(InitializerExprTypeResolver::class),
71-
$this->container->getByType(DynamicReturnTypeExtensionRegistryProvider::class)->getRegistry(),
72-
$this->container->getByType(ExpressionTypeResolverExtensionRegistryProvider::class)->getRegistry(),
73-
$this->container->getByType(ExprPrinter::class),
74-
$this->container->getByType(TypeSpecifier::class),
75-
$this->container->getByType(PropertyReflectionFinder::class),
110+
$this->reflectionProvider,
111+
$this->initializerExprTypeResolver,
112+
$this->dynamicReturnTypeExtensionRegistry,
113+
$this->expressionTypeResolverExtensionRegistry,
114+
$this->exprPrinter,
115+
$this->typeSpecifier,
116+
$this->propertyReflectionFinder,
76117
$this->currentSimpleVersionParser,
77-
$this->container->getByType(NodeScopeResolver::class),
78-
$this->container->getByType(RicherScopeGetTypeHelper::class),
79-
$this->container->getByType(ConstantResolver::class),
118+
$this->nodeScopeResolver,
119+
$this->richerScopeGetTypeHelper,
120+
$this->constantResolver,
80121
$context,
81-
$this->container->getByType(PhpVersion::class),
82-
$this->container->getByType(AttributeReflectionFactory::class),
122+
$this->phpVersionType,
123+
$this->attributeReflectionFactory,
83124
$this->phpVersion,
84125
$this->nodeCallback,
85126
$declareStrictTypes,

0 commit comments

Comments
 (0)