File tree Expand file tree Collapse file tree 5 files changed +64
-1
lines changed Expand file tree Collapse file tree 5 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -237,6 +237,7 @@ extensions:
237237 validateExcludePaths : PHPStan\DependencyInjection\ValidateExcludePathsExtension
238238 autowiredAttributeServices : PHPStan\DependencyInjection\AutowiredAttributeServicesExtension
239239 validateServiceTags : PHPStan\DependencyInjection\ValidateServiceTagsExtension
240+ gnsr : PHPStan\DependencyInjection\GnsrExtension
240241
241242autowiredAttributeServices :
242243 level : null
Original file line number Diff line number Diff line change 1717use PHPStan \Analyser \Generator \NodeHandler \StmtsHandler ;
1818use PHPStan \Analyser \Scope ;
1919use PHPStan \Analyser \StatementContext ;
20+ use PHPStan \DependencyInjection \AutowiredService ;
2021use PHPStan \DependencyInjection \Container ;
2122use PHPStan \NeverException ;
2223use PHPStan \Node \FunctionCallableNode ;
7374 * @phpstan-type GeneratorTValueType = ExprAnalysisRequest|StmtAnalysisRequest|StmtsAnalysisRequest|NodeCallbackRequest|AttrGroupsAnalysisRequest|TypeExprRequest|PersistStorageRequest|RestoreStorageRequest|RunInFiberRequest<mixed>
7475 * @phpstan-type GeneratorTSendType = ExprAnalysisResult|StmtAnalysisResult|TypeExprResult|ExprAnalysisResultStorage|RunInFiberResult<mixed>|null
7576 */
77+ #[AutowiredService]
7678final class GeneratorNodeScopeResolver
7779{
7880
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace PHPStan \DependencyInjection ;
4+
5+ use Nette \DI \CompilerExtension ;
6+ use Nette \DI \Definitions \ServiceDefinition ;
7+ use Override ;
8+ use PHPStan \Analyser \Analyser ;
9+ use PHPStan \Analyser \FileAnalyser ;
10+ use PHPStan \Analyser \Generator \GeneratorNodeScopeResolver ;
11+ use PHPStan \ShouldNotHappenException ;
12+ use function getenv ;
13+
14+ final class GnsrExtension extends CompilerExtension
15+ {
16+
17+ #[Override]
18+ public function beforeCompile ()
19+ {
20+ $ enable = getenv ('PHPSTAN_GNSR ' );
21+ if ($ enable !== '1 ' ) {
22+ return ;
23+ }
24+
25+ $ builder = $ this ->getContainerBuilder ();
26+ $ analyserDef = $ builder ->getDefinitionByType (Analyser::class);
27+ if (!$ analyserDef instanceof ServiceDefinition) {
28+ throw new ShouldNotHappenException ();
29+ }
30+ $ analyserDef ->setArgument ('nodeScopeResolver ' , '@ ' . GeneratorNodeScopeResolver::class);
31+
32+ $ fileAnalyserDef = $ builder ->getDefinitionByType (FileAnalyser::class);
33+ if (!$ fileAnalyserDef instanceof ServiceDefinition) {
34+ throw new ShouldNotHappenException ();
35+ }
36+ $ fileAnalyserDef ->setArgument ('nodeScopeResolver ' , '@ ' . GeneratorNodeScopeResolver::class);
37+ }
38+
39+ }
Original file line number Diff line number Diff line change 2525use PHPStan \File \FileHelper ;
2626use PHPStan \File \FileReader ;
2727use PHPStan \Fixable \Patcher ;
28+ use PHPStan \Node \Printer \ExprPrinter ;
2829use PHPStan \Php \PhpVersion ;
2930use PHPStan \PhpDoc \PhpDocInheritanceResolver ;
3031use PHPStan \Reflection \ClassReflectionFactory ;
3940use function array_map ;
4041use function array_merge ;
4142use function count ;
43+ use function getenv ;
4244use function implode ;
4345use function sprintf ;
4446use function str_replace ;
@@ -80,6 +82,15 @@ protected function getTypeSpecifier(): TypeSpecifier
8082
8183 protected function createNodeScopeResolver (): NodeScopeResolver |GeneratorNodeScopeResolver
8284 {
85+ $ enableGnsr = getenv ('PHPSTAN_GNSR ' );
86+ if ($ enableGnsr === '1 ' ) {
87+ $ container = self ::getContainer ();
88+
89+ return new GeneratorNodeScopeResolver (
90+ $ container ->getByType (ExprPrinter::class),
91+ $ container ,
92+ );
93+ }
8394 $ readWritePropertiesExtensions = $ this ->getReadWritePropertiesExtensions ();
8495 $ reflectionProvider = $ this ->createReflectionProvider ();
8596 $ typeSpecifier = $ this ->getTypeSpecifier ();
Original file line number Diff line number Diff line change 1818use PHPStan \File \FileHelper ;
1919use PHPStan \File \SystemAgnosticSimpleRelativePathHelper ;
2020use PHPStan \Node \InClassNode ;
21+ use PHPStan \Node \Printer \ExprPrinter ;
2122use PHPStan \Php \PhpVersion ;
2223use PHPStan \PhpDoc \PhpDocInheritanceResolver ;
2324use PHPStan \PhpDoc \TypeStringResolver ;
3839use function fclose ;
3940use function fgets ;
4041use function fopen ;
42+ use function getenv ;
4143use function in_array ;
4244use function is_dir ;
4345use function is_string ;
@@ -55,8 +57,16 @@ abstract class TypeInferenceTestCase extends PHPStanTestCase
5557
5658 protected static function createNodeScopeResolver (): NodeScopeResolver |GeneratorNodeScopeResolver
5759 {
58- $ reflectionProvider = self ::createReflectionProvider ();
5960 $ container = self ::getContainer ();
61+ $ enableGnsr = getenv ('PHPSTAN_GNSR ' );
62+ if ($ enableGnsr === '1 ' ) {
63+ return new GeneratorNodeScopeResolver (
64+ $ container ->getByType (ExprPrinter::class),
65+ $ container ,
66+ );
67+ }
68+
69+ $ reflectionProvider = self ::createReflectionProvider ();
6070 $ typeSpecifier = $ container ->getService ('typeSpecifier ' );
6171
6272 return new NodeScopeResolver (
You can’t perform that action at this time.
0 commit comments