From e8e0a2e95c91c1784452994fd327188a8b3f2ae2 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 23 May 2026 14:41:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8F=90=E5=8F=96=20parseBlockStmt?= =?UTF-8?q?s=20=E6=96=B9=E6=B3=95=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=BC=A9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Php/CompilerBase.php | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 39087cfe..58f52227 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1272,6 +1272,14 @@ class CompilerBase extends \PhpAot\Core\Translator return ''; } + protected function parseBlockStmts(array $stmts): string + { + $this->indentLevel++; + $code = $this->parseStmts($stmts); + $this->indentLevel--; + return $code; + } + protected function parseStmts(array $stmts): string { $this->context->enterScope(); @@ -2699,9 +2707,7 @@ class CompilerBase extends \PhpAot\Core\Translator $code .= implode(', ', $list_loop); $code .= ') {' . PHP_EOL; - $this->indentLevel++; - $code .= $this->parseStmts($stmts); - $this->indentLevel--; + $code .= $this->parseBlockStmts($stmts); $code .= $this->getIndent() . '}' . PHP_EOL; @@ -3480,27 +3486,21 @@ class CompilerBase extends \PhpAot\Core\Translator $code = $this->parseBeforeStmtLines() . PHP_EOL; $code .= 'if (' . $cond . ') {' . PHP_EOL; - $this->indentLevel++; - $code .= $this->parseStmts($v->stmts); - $this->indentLevel--; + $code .= $this->parseBlockStmts($v->stmts); $code .= $this->getIndent() . '}'; if ($v->elseifs) { foreach ($v->elseifs as $elseif) { $elseifCond = $this->parseExpr($elseif->cond); $code .= ' else if (' . $elseifCond . ') {' . PHP_EOL; - $this->indentLevel++; - $code .= $this->parseStmts($elseif->stmts); - $this->indentLevel--; + $code .= $this->parseBlockStmts($elseif->stmts); $code .= $this->getIndent() . '}'; } } if ($v->else) { $code .= ' else {' . PHP_EOL; - $this->indentLevel++; - $code .= $this->parseStmts($v->else->stmts); - $this->indentLevel--; + $code .= $this->parseBlockStmts($v->else->stmts); $code .= $this->getIndent() . '}'; } @@ -3566,9 +3566,7 @@ class CompilerBase extends \PhpAot\Core\Translator $code = $this->parseBeforeStmtLines() . PHP_EOL; $code .= 'while (' . $cond . ') {' . PHP_EOL; - $this->indentLevel++; - $code .= $this->parseStmts($stmts); - $this->indentLevel--; + $code .= $this->parseBlockStmts($stmts); $code .= $this->getIndent() . '}' . PHP_EOL; return $code; @@ -3652,9 +3650,7 @@ class CompilerBase extends \PhpAot\Core\Translator $cond = $this->parseExpr($v->cond); $code = $this->parseBeforeStmtLines() . PHP_EOL; $code .= 'do {' . PHP_EOL; - $this->indentLevel++; - $code .= $this->parseStmts($stmts); - $this->indentLevel--; + $code .= $this->parseBlockStmts($stmts); $code .= $this->getIndent() . '} while (' . $cond . ');' . PHP_EOL; return $code; @@ -4351,9 +4347,7 @@ class CompilerBase extends \PhpAot\Core\Translator } $code .= $this->getIndent() . 'case ' . $this->parseScalar($case->cond) . ': {' . PHP_EOL; } - $this->indentLevel++; - $code .= $this->parseStmts($case->stmts); - $this->indentLevel--; + $code .= $this->parseBlockStmts($case->stmts); $code .= $this->getIndent() . '}' . PHP_EOL; } $this->indentLevel--; @@ -5118,9 +5112,7 @@ class CompilerBase extends \PhpAot\Core\Translator $stmts = $v->stmts; $code .= PHP_EOL; - $this->indentLevel++; - $code .= $this->parseStmts($stmts); - $this->indentLevel--; + $code .= $this->parseBlockStmts($stmts); $code .= $this->getIndent() . '}' . PHP_EOL; $catches = $v->catches;