mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
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.
This commit is contained in:
parent
1c9b6b84df
commit
ce3990c1d3
1 changed files with 30 additions and 30 deletions
|
@ -4302,7 +4302,7 @@ class FileInfo {
|
|||
public function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrinter): void {
|
||||
$conds = [];
|
||||
foreach ($stmts as $stmt) {
|
||||
$cond = handlePreprocessorConditions($conds, $stmt);
|
||||
$cond = self::handlePreprocessorConditions($conds, $stmt);
|
||||
|
||||
if ($stmt instanceof Stmt\Nop) {
|
||||
continue;
|
||||
|
@ -4352,7 +4352,7 @@ class FileInfo {
|
|||
$methodInfos = [];
|
||||
$enumCaseInfos = [];
|
||||
foreach ($stmt->stmts as $classStmt) {
|
||||
$cond = handlePreprocessorConditions($conds, $classStmt);
|
||||
$cond = self::handlePreprocessorConditions($conds, $classStmt);
|
||||
if ($classStmt instanceof Stmt\Nop) {
|
||||
continue;
|
||||
}
|
||||
|
@ -4442,6 +4442,34 @@ class FileInfo {
|
|||
throw new Exception("Unterminated preprocessor conditions");
|
||||
}
|
||||
}
|
||||
|
||||
private static function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string {
|
||||
foreach ($stmt->getComments() as $comment) {
|
||||
$text = trim($comment->getText());
|
||||
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
|
||||
$conds[] = $matches[1];
|
||||
} else if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches)) {
|
||||
$conds[] = "defined($matches[1])";
|
||||
} else if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches)) {
|
||||
$conds[] = "!defined($matches[1])";
|
||||
} else if (preg_match('/^#\s*else$/', $text)) {
|
||||
if (empty($conds)) {
|
||||
throw new Exception("Encountered else without corresponding #if");
|
||||
}
|
||||
$cond = array_pop($conds);
|
||||
$conds[] = "!($cond)";
|
||||
} else if (preg_match('/^#\s*endif$/', $text)) {
|
||||
if (empty($conds)) {
|
||||
throw new Exception("Encountered #endif without corresponding #if");
|
||||
}
|
||||
array_pop($conds);
|
||||
} else if ($text[0] === '#') {
|
||||
throw new Exception("Unrecognized preprocessor directive \"$text\"");
|
||||
}
|
||||
}
|
||||
|
||||
return empty($conds) ? null : implode(' && ', $conds);
|
||||
}
|
||||
}
|
||||
|
||||
class DocCommentTag {
|
||||
|
@ -5014,34 +5042,6 @@ function parseClass(
|
|||
);
|
||||
}
|
||||
|
||||
function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string {
|
||||
foreach ($stmt->getComments() as $comment) {
|
||||
$text = trim($comment->getText());
|
||||
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
|
||||
$conds[] = $matches[1];
|
||||
} else if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches)) {
|
||||
$conds[] = "defined($matches[1])";
|
||||
} else if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches)) {
|
||||
$conds[] = "!defined($matches[1])";
|
||||
} else if (preg_match('/^#\s*else$/', $text)) {
|
||||
if (empty($conds)) {
|
||||
throw new Exception("Encountered else without corresponding #if");
|
||||
}
|
||||
$cond = array_pop($conds);
|
||||
$conds[] = "!($cond)";
|
||||
} else if (preg_match('/^#\s*endif$/', $text)) {
|
||||
if (empty($conds)) {
|
||||
throw new Exception("Encountered #endif without corresponding #if");
|
||||
}
|
||||
array_pop($conds);
|
||||
} else if ($text[0] === '#') {
|
||||
throw new Exception("Unrecognized preprocessor directive \"$text\"");
|
||||
}
|
||||
}
|
||||
|
||||
return empty($conds) ? null : implode(' && ', $conds);
|
||||
}
|
||||
|
||||
/** @return DocComment[] */
|
||||
function getFileDocComments(array $stmts): array {
|
||||
if (empty($stmts)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue