From 4034017046e8cff99b0a4f8dc57de3856f48f79b Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 23 Oct 2020 09:56:58 +0200 Subject: [PATCH] Optimization for < PHP 8.0 analysis --- src/Parser/RichParser.php | 16 ++++++++++++++-- tests/PHPStan/Analyser/AnalyserTest.php | 3 ++- ...equireParenthesesForNestedTernaryRuleTest.php | 6 ++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Parser/RichParser.php b/src/Parser/RichParser.php index 11da86afc9..b6d38f5d65 100644 --- a/src/Parser/RichParser.php +++ b/src/Parser/RichParser.php @@ -9,6 +9,7 @@ use PhpParser\NodeVisitor\NodeConnectingVisitor; use PHPStan\File\FileReader; use PHPStan\NodeVisitor\StatementOrderVisitor; +use PHPStan\Php\PhpVersion; class RichParser implements Parser { @@ -25,13 +26,16 @@ class RichParser implements Parser private NodeChildrenVisitor $nodeChildrenVisitor; + private PhpVersion $phpVersion; + public function __construct( \PhpParser\Parser $parser, Lexer $lexer, NameResolver $nameResolver, NodeConnectingVisitor $nodeConnectingVisitor, StatementOrderVisitor $statementOrderVisitor, - NodeChildrenVisitor $nodeChildrenVisitor + NodeChildrenVisitor $nodeChildrenVisitor, + PhpVersion $phpVersion ) { $this->parser = $parser; @@ -40,6 +44,7 @@ public function __construct( $this->nodeConnectingVisitor = $nodeConnectingVisitor; $this->statementOrderVisitor = $statementOrderVisitor; $this->nodeChildrenVisitor = $nodeChildrenVisitor; + $this->phpVersion = $phpVersion; } /** @@ -75,11 +80,18 @@ public function parseString(string $sourceCode): array $nodeTraverser->addVisitor($this->nameResolver); $nodeTraverser->addVisitor($this->nodeConnectingVisitor); $nodeTraverser->addVisitor($this->statementOrderVisitor); - $nodeTraverser->addVisitor($this->nodeChildrenVisitor); + + if ($this->phpVersion->requiresParenthesesForNestedTernaries()) { + $nodeTraverser->addVisitor($this->nodeChildrenVisitor); + } /** @var array<\PhpParser\Node\Stmt> */ $nodes = $nodeTraverser->traverse($nodes); + if (!$this->phpVersion->requiresParenthesesForNestedTernaries()) { + return $nodes; + } + $tokensTraverser = new NodeTraverser(); $tokensTraverser->addVisitor(new NodeTokensVisitor($tokens)); diff --git a/tests/PHPStan/Analyser/AnalyserTest.php b/tests/PHPStan/Analyser/AnalyserTest.php index ba1cc663d8..00a482b6d9 100644 --- a/tests/PHPStan/Analyser/AnalyserTest.php +++ b/tests/PHPStan/Analyser/AnalyserTest.php @@ -527,7 +527,8 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): \PHPStan\An new \PhpParser\NodeVisitor\NameResolver(), new NodeConnectingVisitor(), new StatementOrderVisitor(), - new NodeChildrenVisitor() + new NodeChildrenVisitor(), + self::getContainer()->getByType(PhpVersion::class) ), new DependencyResolver($fileHelper, $broker, new ExportedNodeResolver($fileTypeMapper, $printer)), $reportUnmatchedIgnoredErrors diff --git a/tests/PHPStan/Rules/Ternary/RequireParenthesesForNestedTernaryRuleTest.php b/tests/PHPStan/Rules/Ternary/RequireParenthesesForNestedTernaryRuleTest.php index 1cfda5b3cc..60f08aee3b 100644 --- a/tests/PHPStan/Rules/Ternary/RequireParenthesesForNestedTernaryRuleTest.php +++ b/tests/PHPStan/Rules/Ternary/RequireParenthesesForNestedTernaryRuleTest.php @@ -27,6 +27,9 @@ public function testDoNotReportBeforePhp80(): void public function testReportOnPhp80(): void { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped('Test requires PHP 8.0'); + } $this->phpVersion = new PhpVersion(80000); $tip = 'See: https://wiki.php.net/rfc/ternary_associativity'; $this->analyse([__DIR__ . '/data/nested-ternary.php'], [ @@ -50,6 +53,9 @@ public function testReportOnPhp80(): void public function testBug(): void { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped('Test requires PHP 8.0'); + } $this->phpVersion = new PhpVersion(80000); $this->analyse([__DIR__ . '/data/require-parentheses-bug.php'], []); }