-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Hi Nikita, I'm trying to adapt tests in phpstorm-stubs repo for new PHP 8.4 and face a problem that parser fails to parse declarations for new PHP functions exit and die. The case is next: stubs contain a file with functions declarations
function exit(string|int $status = 0): never {}
function die(string|int $status = 0): never {}
But when I try to parse them, parser throws exception Syntax error, unexpected T_EXIT, expecting T_STRING or T_READONLY or '('
Parser is configured to use the latest available version $parser = new ParserFactory()->createForNewestSupportedVersion(); but still it doesn't work. The simplified code of parser is next
<?php
include __DIR__ . '/vendor/autoload.php';
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
class ASTVisitor extends \PhpParser\NodeVisitorAbstract
{
public function enterNode(\PhpParser\Node $node)
{
if ($node instanceof Function_) {
$name = $node->name;
}
}
}
class Parser {
public static function parse()
{
$parser = new ParserFactory()->createForNewestSupportedVersion();
$stubsIterator =
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(__DIR__ . '/src', FilesystemIterator::SKIP_DOTS)
);
$visitor = new ASTVisitor();
$traverser = new NodeTraverser();
$traverser->addVisitor($visitor);
foreach ($stubsIterator as $file) {
$code = file_get_contents($file->getRealPath());
$traverser->traverse($parser->parse($code));
}
}
}
Parser::parse();
Am I doing something wrong? Can I somehow workaround the issue to be able to parse these functions declarations? Is it possible to adapt parser to properly parse declarations of functions exit and die in stubs?
Thank you in advance!