Skip to content

Commit

Permalink
Optimization for < PHP 8.0 analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 23, 2020
1 parent 9131023 commit 4034017
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/Parser/RichParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use PHPStan\File\FileReader;
use PHPStan\NodeVisitor\StatementOrderVisitor;
use PHPStan\Php\PhpVersion;

class RichParser implements Parser
{
Expand All @@ -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;
Expand All @@ -40,6 +44,7 @@ public function __construct(
$this->nodeConnectingVisitor = $nodeConnectingVisitor;
$this->statementOrderVisitor = $statementOrderVisitor;
$this->nodeChildrenVisitor = $nodeChildrenVisitor;
$this->phpVersion = $phpVersion;
}

/**
Expand Down Expand Up @@ -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));

Expand Down
3 changes: 2 additions & 1 deletion tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'], [
Expand All @@ -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'], []);
}
Expand Down

0 comments on commit 4034017

Please sign in to comment.