Skip to content

Commit a7e2b6a

Browse files
authored
Free and look up less: native fast paths, shared scope services, reasonless result sharing
2 parents 772986b + 0e198bb commit a7e2b6a

68 files changed

Lines changed: 2285 additions & 2014 deletions

File tree

Some content is hidden

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

‎src/Analyser/DirectInternalScopeFactory.php‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ final class DirectInternalScopeFactory implements InternalScopeFactory
2424

2525
private ExpressionResultStorageStack $expressionResultStorageStack;
2626

27+
/** The services the created scopes share, built on the first create(). */
28+
private ?ScopeServices $services = null;
29+
2730
/**
2831
* @param callable(Node $node, Scope $scope): void|null $nodeCallback
2932
* @param ExtensionsCollection<ExpressionTypeResolverExtension> $expressionTypeResolverExtensions
@@ -75,9 +78,8 @@ public function create(
7578
$className = NodeCallbackScope::class;
7679
}
7780

78-
return new $className(
81+
$this->services ??= new ScopeServices(
7982
$this->container,
80-
$this,
8183
$this->reflectionProvider,
8284
$this->initializerExprTypeResolver,
8385
$this->expressionTypeResolverExtensions,
@@ -87,11 +89,16 @@ public function create(
8789
$this->parser,
8890
$this->constantResolver,
8991
$this->expressionResultStorageStack,
90-
$context,
9192
$this->phpVersion,
9293
$this->attributeReflectionFactory,
9394
$this->configuredPhpVersionRangeHelper,
9495
$this->nodeCallback,
96+
);
97+
98+
return new $className(
99+
$this->services,
100+
$this,
101+
$context,
95102
$declareStrictTypes,
96103
$function,
97104
$namespace,

‎src/Analyser/LazyInternalScopeFactory.php‎

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Generics\TemplateArgumentConstraints;
77
use PHPStan\Analyser\Generics\TemplateArgumentFrame;
88
use PHPStan\DependencyInjection\Container;
9-
use PHPStan\DependencyInjection\ExtensionsCollection;
109
use PHPStan\DependencyInjection\GenerateFactory;
1110
use PHPStan\Node\Printer\ExprPrinter;
1211
use PHPStan\Parser\Parser;
@@ -29,28 +28,10 @@ final class LazyInternalScopeFactory implements InternalScopeFactory
2928

3029
private Parser $currentSimpleVersionParser;
3130

32-
private ?ReflectionProvider $reflectionProvider = null;
33-
34-
private ?InitializerExprTypeResolver $initializerExprTypeResolver = null;
35-
36-
/** @var ExtensionsCollection<ExpressionTypeResolverExtension>|null */
37-
private ?ExtensionsCollection $expressionTypeResolverExtensions = null;
38-
39-
private ?ExprPrinter $exprPrinter = null;
40-
41-
private ?TypeSpecifier $typeSpecifier = null;
42-
43-
private ?PropertyReflectionFinder $propertyReflectionFinder = null;
44-
45-
private ?ConstantResolver $constantResolver = null;
46-
4731
private ExpressionResultStorageStack $expressionResultStorageStack;
4832

49-
private ?PhpVersion $phpVersionType = null;
50-
51-
private ?AttributeReflectionFactory $attributeReflectionFactory = null;
52-
53-
private ?ConfiguredPhpVersionRangeHelper $configuredPhpVersionRangeHelper = null;
33+
/** The services the created scopes share, built on the first create(). */
34+
private ?ScopeServices $services = null;
5435

5536
private ?self $twin = null;
5637

@@ -97,36 +78,27 @@ public function create(
9778
$className = NodeCallbackScope::class;
9879
}
9980

100-
$this->reflectionProvider ??= $this->container->getByType(ReflectionProvider::class);
101-
$this->initializerExprTypeResolver ??= $this->container->getByType(InitializerExprTypeResolver::class);
102-
$this->expressionTypeResolverExtensions ??= $this->container->getExtensionsCollection(ExpressionTypeResolverExtension::class);
103-
$this->exprPrinter ??= $this->container->getByType(ExprPrinter::class);
104-
$this->typeSpecifier ??= $this->container->getByType(TypeSpecifier::class);
105-
$this->propertyReflectionFinder ??= $this->container->getByType(PropertyReflectionFinder::class);
106-
107-
$this->constantResolver ??= $this->container->getByType(ConstantResolver::class);
108-
109-
$this->phpVersionType ??= $this->container->getByType(PhpVersion::class);
110-
$this->attributeReflectionFactory ??= $this->container->getByType(AttributeReflectionFactory::class);
111-
$this->configuredPhpVersionRangeHelper ??= $this->container->getByType(ConfiguredPhpVersionRangeHelper::class);
112-
113-
return new $className(
81+
$this->services ??= new ScopeServices(
11482
$this->container,
115-
$this,
116-
$this->reflectionProvider,
117-
$this->initializerExprTypeResolver,
118-
$this->expressionTypeResolverExtensions,
119-
$this->exprPrinter,
120-
$this->typeSpecifier,
121-
$this->propertyReflectionFinder,
83+
$this->container->getByType(ReflectionProvider::class),
84+
$this->container->getByType(InitializerExprTypeResolver::class),
85+
$this->container->getExtensionsCollection(ExpressionTypeResolverExtension::class),
86+
$this->container->getByType(ExprPrinter::class),
87+
$this->container->getByType(TypeSpecifier::class),
88+
$this->container->getByType(PropertyReflectionFinder::class),
12289
$this->currentSimpleVersionParser,
123-
$this->constantResolver,
90+
$this->container->getByType(ConstantResolver::class),
12491
$this->expressionResultStorageStack,
125-
$context,
126-
$this->phpVersionType,
127-
$this->attributeReflectionFactory,
128-
$this->configuredPhpVersionRangeHelper,
92+
$this->container->getByType(PhpVersion::class),
93+
$this->container->getByType(AttributeReflectionFactory::class),
94+
$this->container->getByType(ConfiguredPhpVersionRangeHelper::class),
12995
$this->nodeCallback,
96+
);
97+
98+
return new $className(
99+
$this->services,
100+
$this,
101+
$context,
130102
$declareStrictTypes,
131103
$function,
132104
$namespace,
@@ -184,6 +156,9 @@ private function twin(): self
184156

185157
$this->twin = new self($this->container, $this->nodeCallback, !$this->createsNodeCallbackScopes, $this->expressionResultStorageStack);
186158
$this->twin->origin = WeakReference::create($this);
159+
// the same services for both flavours: the same container, node
160+
// callback and storage stack
161+
$this->twin->services = $this->services;
187162

188163
return $this->twin;
189164
}

0 commit comments

Comments
 (0)