Skip to content

Commit ce3990c

Browse files
gen_stub: move handlePreprocessorConditions() into FileInfo()
Reduce the number of global functions by moving it to static method `FileInfo::handlePreprocessorConditions()`. Since it is only used by `FileInfo::handleStatements()`, also make it private.
1 parent 1c9b6b8 commit ce3990c

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

build/gen_stub.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,7 +4302,7 @@ public function shouldGenerateLegacyArginfo(): bool {
43024302
public function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrinter): void {
43034303
$conds = [];
43044304
foreach ($stmts as $stmt) {
4305-
$cond = handlePreprocessorConditions($conds, $stmt);
4305+
$cond = self::handlePreprocessorConditions($conds, $stmt);
43064306

43074307
if ($stmt instanceof Stmt\Nop) {
43084308
continue;
@@ -4352,7 +4352,7 @@ public function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrin
43524352
$methodInfos = [];
43534353
$enumCaseInfos = [];
43544354
foreach ($stmt->stmts as $classStmt) {
4355-
$cond = handlePreprocessorConditions($conds, $classStmt);
4355+
$cond = self::handlePreprocessorConditions($conds, $classStmt);
43564356
if ($classStmt instanceof Stmt\Nop) {
43574357
continue;
43584358
}
@@ -4442,6 +4442,34 @@ public function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrin
44424442
throw new Exception("Unterminated preprocessor conditions");
44434443
}
44444444
}
4445+
4446+
private static function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string {
4447+
foreach ($stmt->getComments() as $comment) {
4448+
$text = trim($comment->getText());
4449+
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
4450+
$conds[] = $matches[1];
4451+
} else if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches)) {
4452+
$conds[] = "defined($matches[1])";
4453+
} else if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches)) {
4454+
$conds[] = "!defined($matches[1])";
4455+
} else if (preg_match('/^#\s*else$/', $text)) {
4456+
if (empty($conds)) {
4457+
throw new Exception("Encountered else without corresponding #if");
4458+
}
4459+
$cond = array_pop($conds);
4460+
$conds[] = "!($cond)";
4461+
} else if (preg_match('/^#\s*endif$/', $text)) {
4462+
if (empty($conds)) {
4463+
throw new Exception("Encountered #endif without corresponding #if");
4464+
}
4465+
array_pop($conds);
4466+
} else if ($text[0] === '#') {
4467+
throw new Exception("Unrecognized preprocessor directive \"$text\"");
4468+
}
4469+
}
4470+
4471+
return empty($conds) ? null : implode(' && ', $conds);
4472+
}
44454473
}
44464474

44474475
class DocCommentTag {
@@ -5014,34 +5042,6 @@ function parseClass(
50145042
);
50155043
}
50165044

5017-
function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string {
5018-
foreach ($stmt->getComments() as $comment) {
5019-
$text = trim($comment->getText());
5020-
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
5021-
$conds[] = $matches[1];
5022-
} else if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches)) {
5023-
$conds[] = "defined($matches[1])";
5024-
} else if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches)) {
5025-
$conds[] = "!defined($matches[1])";
5026-
} else if (preg_match('/^#\s*else$/', $text)) {
5027-
if (empty($conds)) {
5028-
throw new Exception("Encountered else without corresponding #if");
5029-
}
5030-
$cond = array_pop($conds);
5031-
$conds[] = "!($cond)";
5032-
} else if (preg_match('/^#\s*endif$/', $text)) {
5033-
if (empty($conds)) {
5034-
throw new Exception("Encountered #endif without corresponding #if");
5035-
}
5036-
array_pop($conds);
5037-
} else if ($text[0] === '#') {
5038-
throw new Exception("Unrecognized preprocessor directive \"$text\"");
5039-
}
5040-
}
5041-
5042-
return empty($conds) ? null : implode(' && ', $conds);
5043-
}
5044-
50455045
/** @return DocComment[] */
50465046
function getFileDocComments(array $stmts): array {
50475047
if (empty($stmts)) {

0 commit comments

Comments
 (0)