diff --git a/src/Parser/RichParser.php b/src/Parser/RichParser.php index 4d3f0bdb4b..b5cb957e8f 100644 --- a/src/Parser/RichParser.php +++ b/src/Parser/RichParser.php @@ -12,6 +12,7 @@ use PHPStan\Analyser\Ignore\IgnoreParseException; use PHPStan\DependencyInjection\Container; use PHPStan\File\FileReader; +use PHPStan\Php\PhpVersion; use PHPStan\ShouldNotHappenException; use function array_filter; use function array_key_last; @@ -49,6 +50,7 @@ public function __construct( private NameResolver $nameResolver, private Container $container, private IgnoreLexer $ignoreLexer, + private PhpVersion $phpVersion, ) { } @@ -82,6 +84,10 @@ public function parseString(string $sourceCode): array throw new ShouldNotHappenException(); } + $phpVersionIdCleaner = new NodeTraverser(new RemoveUnusedCodeByPhpVersionIdVisitor($this->phpVersion->getVersionString())); + /** @var array */ + $nodes = $phpVersionIdCleaner->traverse($nodes); + $pipeTransformer = new NodeTraverser(new PipeTransformerVisitor()); /** @var array */ $nodes = $pipeTransformer->traverse($nodes); diff --git a/tests/PHPStan/Analyser/AnalyserTest.php b/tests/PHPStan/Analyser/AnalyserTest.php index 27f9daddb0..e2b1424a6e 100644 --- a/tests/PHPStan/Analyser/AnalyserTest.php +++ b/tests/PHPStan/Analyser/AnalyserTest.php @@ -19,6 +19,7 @@ use PHPStan\Node\Printer\ExprPrinter; use PHPStan\Node\Printer\Printer; use PHPStan\Parser\RichParser; +use PHPStan\Php\PhpVersion; use PHPStan\PhpDoc\PhpDocInheritanceResolver; use PHPStan\Reflection\ClassReflectionFactory; use PHPStan\Reflection\InitializerExprTypeResolver; @@ -842,6 +843,7 @@ private function createAnalyser(): Analyser new NameResolver(), $container, new IgnoreLexer(), + $container->getByType(PhpVersion::class), ), new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($reflectionProvider, $fileTypeMapper, new ExprPrinter(new Printer())), $fileTypeMapper), new IgnoreErrorExtensionProvider(new NetteContainer(new Container([]))), diff --git a/tests/PHPStan/Rules/Constants/NativeTypedClassConstantRuleBug13133Test.php b/tests/PHPStan/Rules/Constants/NativeTypedClassConstantRuleBug13133Test.php new file mode 100644 index 0000000000..7c49d7c73f --- /dev/null +++ b/tests/PHPStan/Rules/Constants/NativeTypedClassConstantRuleBug13133Test.php @@ -0,0 +1,32 @@ + + */ +class NativeTypedClassConstantRuleBug13133Test extends RuleTestCase +{ + + protected function getRule(): TRule + { + return new NativeTypedClassConstantRule(new PhpVersion(80200)); + } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/bug-13133.neon', + ]; + } + + public function testBug13133(): void + { + $this->analyse([__DIR__ . '/data/bug-13133.php'], []); + } + +} diff --git a/tests/PHPStan/Rules/Constants/bug-13133.neon b/tests/PHPStan/Rules/Constants/bug-13133.neon new file mode 100644 index 0000000000..636d772d2c --- /dev/null +++ b/tests/PHPStan/Rules/Constants/bug-13133.neon @@ -0,0 +1,2 @@ +parameters: + phpVersion: 80200 diff --git a/tests/PHPStan/Rules/Constants/data/bug-13133.php b/tests/PHPStan/Rules/Constants/data/bug-13133.php new file mode 100644 index 0000000000..00c51776e3 --- /dev/null +++ b/tests/PHPStan/Rules/Constants/data/bug-13133.php @@ -0,0 +1,17 @@ += 8.3 + +declare(strict_types = 1); + +namespace Bug13133; + +if (PHP_VERSION_ID >= 80300) { + class Foo { + public const string BAR = 'bar'; + } +} else { + class Foo { + public const BAR = 'bar'; + } +} + +echo Foo::BAR; diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 99480a4061..3e44d11533 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -1818,12 +1818,15 @@ public function testDisallowNamedArgumentsInPhpVersionScope(): void $this->checkNullables = true; $this->checkUnionTypes = true; - $this->analyse([__DIR__ . '/data/disallow-named-arguments-php-version-scope.php'], [ - [ + $errors = []; + if (PHP_VERSION_ID < 80000) { + $errors[] = [ 'Named arguments are supported only on PHP 8.0 and later.', 26, - ], - ]); + ]; + } + + $this->analyse([__DIR__ . '/data/disallow-named-arguments-php-version-scope.php'], $errors); } #[RequiresPhp('>= 8.0')]