Skip to content

Commit 65c10b7

Browse files
committed
Env variable PHPSTAN_GNSR=1 enables GNSR
1 parent 2dc57b3 commit 65c10b7

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

conf/config.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

241242
autowiredAttributeServices:
242243
level: null

src/Analyser/Generator/GeneratorNodeScopeResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\Analyser\Generator\NodeHandler\StmtsHandler;
1818
use PHPStan\Analyser\Scope;
1919
use PHPStan\Analyser\StatementContext;
20+
use PHPStan\DependencyInjection\AutowiredService;
2021
use PHPStan\DependencyInjection\Container;
2122
use PHPStan\NeverException;
2223
use PHPStan\Node\FunctionCallableNode;
@@ -73,6 +74,7 @@
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]
7678
final class GeneratorNodeScopeResolver
7779
{
7880

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

src/Testing/RuleTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPStan\File\FileHelper;
2626
use PHPStan\File\FileReader;
2727
use PHPStan\Fixable\Patcher;
28+
use PHPStan\Node\Printer\ExprPrinter;
2829
use PHPStan\Php\PhpVersion;
2930
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
3031
use PHPStan\Reflection\ClassReflectionFactory;
@@ -39,6 +40,7 @@
3940
use function array_map;
4041
use function array_merge;
4142
use function count;
43+
use function getenv;
4244
use function implode;
4345
use function sprintf;
4446
use 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();

src/Testing/TypeInferenceTestCase.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\File\FileHelper;
1919
use PHPStan\File\SystemAgnosticSimpleRelativePathHelper;
2020
use PHPStan\Node\InClassNode;
21+
use PHPStan\Node\Printer\ExprPrinter;
2122
use PHPStan\Php\PhpVersion;
2223
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
2324
use PHPStan\PhpDoc\TypeStringResolver;
@@ -38,6 +39,7 @@
3839
use function fclose;
3940
use function fgets;
4041
use function fopen;
42+
use function getenv;
4143
use function in_array;
4244
use function is_dir;
4345
use 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(

0 commit comments

Comments
 (0)