Handle preprocessor conditions inside classes

Also remove the dead parseClass() function.
This commit is contained in:
Nikita Popov 2019-08-10 16:28:57 +02:00
parent 9c02c58481
commit ae721c488d

View file

@ -194,38 +194,7 @@ function parseFunctionLike(string $name, Node\FunctionLike $func, ?string $cond)
return new FuncInfo($name, $args, $return, $numRequiredArgs, $cond); return new FuncInfo($name, $args, $return, $numRequiredArgs, $cond);
} }
function parseClass(Stmt\Class_ $class): ClassInfo { function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string {
$funcs = [];
$className = $class->name->toString();
foreach ($class as $stmt) {
if (!$stmt instanceof Stmt\ClassMethod) {
throw new Exception("Not implemented class statement");
}
$funcs[] = parseFunctionLike($className . '_' . $stmt->name->toString(), $stmt);
}
return new ClassInfo($className, $funcs);
}
/** @return FuncInfo[] */
function parseStubFile(string $fileName) {
if (!file_exists($fileName)) {
throw new Exception("File $fileName does not exist");
}
$code = file_get_contents($fileName);
$lexer = new PhpParser\Lexer();
$parser = new PhpParser\Parser\Php7($lexer);
$nodeTraverser = new PhpParser\NodeTraverser;
$nodeTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
$stmts = $parser->parse($code);
$nodeTraverser->traverse($stmts);
$funcInfos = [];
$conds = [];
foreach ($stmts as $stmt) {
foreach ($stmt->getComments() as $comment) { foreach ($stmt->getComments() as $comment) {
$text = trim($comment->getText()); $text = trim($comment->getText());
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) { if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
@ -250,8 +219,29 @@ function parseStubFile(string $fileName) {
} }
} }
$cond = empty($conds) ? null : implode(' && ', $conds); return empty($conds) ? null : implode(' && ', $conds);
}
/** @return FuncInfo[] */
function parseStubFile(string $fileName) {
if (!file_exists($fileName)) {
throw new Exception("File $fileName does not exist");
}
$code = file_get_contents($fileName);
$lexer = new PhpParser\Lexer();
$parser = new PhpParser\Parser\Php7($lexer);
$nodeTraverser = new PhpParser\NodeTraverser;
$nodeTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
$stmts = $parser->parse($code);
$nodeTraverser->traverse($stmts);
$funcInfos = [];
$conds = [];
foreach ($stmts as $stmt) {
$cond = handlePreprocessorConditions($conds, $stmt);
if ($stmt instanceof Stmt\Nop) { if ($stmt instanceof Stmt\Nop) {
continue; continue;
} }
@ -264,6 +254,7 @@ function parseStubFile(string $fileName) {
if ($stmt instanceof Stmt\ClassLike) { if ($stmt instanceof Stmt\ClassLike) {
$className = $stmt->name->toString(); $className = $stmt->name->toString();
foreach ($stmt->stmts as $classStmt) { foreach ($stmt->stmts as $classStmt) {
$cond = handlePreprocessorConditions($conds, $classStmt);
if ($classStmt instanceof Stmt\Nop) { if ($classStmt instanceof Stmt\Nop) {
continue; continue;
} }