Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PhpParser] Fix crash read jetbrains/phpstorm-stubs included in phpstan.phar on PHP 8.0 and PHP 7.4 #5001

Merged
merged 5 commits into from
Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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