Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Parser/RichParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function __construct(
private NameResolver $nameResolver,
private Container $container,
private IgnoreLexer $ignoreLexer,
private PhpVersion $phpVersion,
)
{
}
Expand Down Expand Up @@ -82,6 +84,10 @@ public function parseString(string $sourceCode): array
throw new ShouldNotHappenException();
}

$phpVersionIdCleaner = new NodeTraverser(new RemoveUnusedCodeByPhpVersionIdVisitor($this->phpVersion->getVersionString()));
/** @var array<Node\Stmt> */
$nodes = $phpVersionIdCleaner->traverse($nodes);

$pipeTransformer = new NodeTraverser(new PipeTransformerVisitor());
/** @var array<Node\Stmt> */
$nodes = $pipeTransformer->traverse($nodes);
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([]))),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Constants;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule as TRule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<NativeTypedClassConstantRule>
*/
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'], []);
}

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Rules/Constants/bug-13133.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
phpVersion: 80200
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Constants/data/bug-13133.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php // lint >= 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;
11 changes: 7 additions & 4 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down
Loading