Skip to content

Commit

Permalink
[PhpParser] Fix crash read jetbrains/phpstorm-stubs included in phpst…
Browse files Browse the repository at this point in the history
…an.phar on PHP 8.0 and PHP 7.4 (#5001)
  • Loading branch information
samsonasik committed Sep 12, 2023
1 parent a2f7005 commit ffa43a1
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpDocParser\PhpParser\SmartPhpParser;
use Throwable;

/**
* The nodes provided by this resolver is for read-only analysis only!
Expand Down Expand Up @@ -311,9 +312,21 @@ public function parseFileNameToDecoratedNodes(string $fileName): array
return $this->parsedFileNodes[$fileName];
}

$stmts = $this->smartPhpParser->parseFile($fileName);
if ($stmts === []) {
return $this->parsedFileNodes[$fileName] = [];
try {
$stmts = $this->smartPhpParser->parseFile($fileName);
} catch (Throwable $throwable) {
/**
* phpstan.phar contains jetbrains/phpstorm-stubs which the code is not downgraded
* that if read from lower php < 8.1 may cause crash
*
* @see https://github.com/rectorphp/rector/issues/8193 on php 8.0
* @see https://github.com/rectorphp/rector/issues/8145 on php 7.4
*/
if (str_contains($fileName, 'phpstan.phar')) {
return [];
}

throw $throwable;
}

return $this->parsedFileNodes[$fileName] = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile(
Expand Down

0 comments on commit ffa43a1

Please sign in to comment.