66use PHPStan \Analyser \Generics \TemplateArgumentConstraints ;
77use PHPStan \Analyser \Generics \TemplateArgumentFrame ;
88use PHPStan \DependencyInjection \Container ;
9- use PHPStan \DependencyInjection \ExtensionsCollection ;
109use PHPStan \DependencyInjection \GenerateFactory ;
1110use PHPStan \Node \Printer \ExprPrinter ;
1211use 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