gen_stub: move parseStubFile() into FileInfo

Reduce the number of global functions by moving it to static method
`FileInfo::parseStubFile()`. Additionally, make `FileInfo::handleStatements()`
private now that the only caller is part of the class.
This commit is contained in:
Daniel Scherzer 2025-05-08 12:12:12 -07:00 committed by DanielEScherzer
parent 05dbf0707a
commit 2b0cb760d4

View file

@ -95,7 +95,7 @@ function processStubFile(string $stubFile, Context $context, bool $includeOnly =
if (!$fileInfo = $context->parsedFiles[$stubFile] ?? null) {
initPhpParser();
$stubContent = $stubCode ?? file_get_contents($stubFile);
$fileInfo = parseStubFile($stubContent);
$fileInfo = FileInfo::parseStubFile($stubContent);
$context->parsedFiles[$stubFile] = $fileInfo;
foreach ($fileInfo->dependencies as $dependency) {
@ -4295,7 +4295,27 @@ class FileInfo {
return $legacyFileInfo;
}
public function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrinter): void {
public static function parseStubFile(string $code): FileInfo {
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative());
$nodeTraverser = new PhpParser\NodeTraverser;
$nodeTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
$prettyPrinter = new class extends Standard {
protected function pName_FullyQualified(Name\FullyQualified $node): string {
return implode('\\', $node->getParts());
}
};
$stmts = $parser->parse($code);
$nodeTraverser->traverse($stmts);
$fileTags = DocCommentTag::parseDocComments(getFileDocComments($stmts));
$fileInfo = new FileInfo($fileTags);
$fileInfo->handleStatements($stmts, $prettyPrinter);
return $fileInfo;
}
private function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrinter): void {
$conds = [];
foreach ($stmts as $stmt) {
$cond = self::handlePreprocessorConditions($conds, $stmt);
@ -5031,26 +5051,6 @@ function getFileDocComments(array $stmts): array {
);
}
function parseStubFile(string $code): FileInfo {
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative());
$nodeTraverser = new PhpParser\NodeTraverser;
$nodeTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
$prettyPrinter = new class extends Standard {
protected function pName_FullyQualified(Name\FullyQualified $node): string {
return implode('\\', $node->getParts());
}
};
$stmts = $parser->parse($code);
$nodeTraverser->traverse($stmts);
$fileTags = DocCommentTag::parseDocComments(getFileDocComments($stmts));
$fileInfo = new FileInfo($fileTags);
$fileInfo->handleStatements($stmts, $prettyPrinter);
return $fileInfo;
}
/**
* @template T
* @param iterable<T> $infos