From 92bd1fe56c197cfb00d813e04e0625a0593ce954 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 5 Feb 2026 13:42:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E5=9F=BA=E7=A1=80=E7=B1=BB=E7=9A=84=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=A3=B0=E6=98=8E=E5=92=8C=E6=8E=A7=E5=88=B6=E6=B5=81?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将stop方法的返回类型从void改为never - 简化多个条件分支中的else语句和break语句 - 更新parseFunction方法参数类型并添加异常注解 - 修改parseIdentifier方法参数类型为NodeAbstract - 添加对continue语句的循环上下文检查 - 移除未使用的NodeAbstract导入 - 为Skip异常类添加文件头注释 - 重构函数调用优化器中的条件逻辑 --- src/Php/CompilerBase.php | 63 +++++++++++++++++------------------ src/Php/Exception/Skip.php | 9 +++-- src/Php/FuncCallOptimizer.php | 3 +- src/Php/Translator.php | 1 - 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index aeb7c735..6aeadbd2 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -451,7 +451,7 @@ class CompilerBase extends \PhpAot\Core\Translator return false; } - public function stop(string $string): void + public function stop(string $string): never { $this->climate->red($string . "\n"); exit(1); @@ -627,9 +627,8 @@ class CompilerBase extends \PhpAot\Core\Translator } if ($macro) { return $id . ', ' . $this->getLiteralString($funcName); - } else { - return 'php_get_func(' . $id . ', ' . $this->getLiteralString($funcName) . ')'; } + return 'php_get_func(' . $id . ', ' . $this->getLiteralString($funcName) . ')'; } protected function parseFunctionDeclaration(Node\Stmt\Function_|Node\Stmt\ClassMethod $v): FunctionDef @@ -646,7 +645,10 @@ class CompilerBase extends \PhpAot\Core\Translator return $functionDef; } - protected function parseFunction(FunctionLike $v): string + /** + * @throws \Exception + */ + protected function parseFunction(Node\Stmt\Function_|Node\Stmt\ClassMethod $v): string { $this->resetFunction(); $this->function = $this->parseIdentifier($v->name); @@ -748,7 +750,7 @@ class CompilerBase extends \PhpAot\Core\Translator } } - protected function parseIdentifier(Node $expr): string + protected function parseIdentifier(NodeAbstract $expr): string { $type = $expr->getType(); switch ($type) { @@ -910,7 +912,7 @@ class CompilerBase extends \PhpAot\Core\Translator break; case 'Stmt_Class': $this->fatalError($v, 'Cannot declare class in function'); - break; + // no break default: abort($v); } @@ -1404,15 +1406,14 @@ class CompilerBase extends \PhpAot\Core\Translator return self::TYPE_STR; case 'void': $this->fatalError($param, 'Cannot use `void` as a parameter type.'); - break; + // no break case 'mixed': return self::TYPE_VAR; case 'resource': $this->fatalError($param, 'Cannot use `resource` as a parameter type.'); - break; + // no break default: $this->objects[$var] = $name; - return self::TYPE_OBJECT; } } @@ -1661,14 +1662,14 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->parseAssignOp($expr, '**='); } - protected function error(string $msg): void + protected function error(string $msg): never { $this->climate->red("Fatal error: {$msg}"); debug_print_backtrace(); exit(255); } - protected function fatalError(Node $node, string $msg): void + protected function fatalError(Node $node, string $msg): never { $this->error("{$msg} in {$this->file}:{$node->getStartLine()}"); } @@ -2235,9 +2236,8 @@ class CompilerBase extends \PhpAot\Core\Translator $ns = explode('::', $name)[0]; $ce = $this->getClassEntryPtr($ns[0]); return 'php::constant(' . $ce . ', ' . $this->getLiteralString($ns[1]) . ')'; - } else { - return 'php::constant(nullptr, ' . $this->getLiteralString($name) . ')'; } + return 'php::constant(nullptr, ' . $this->getLiteralString($name) . ')'; } return 'php::constant("' . $this->escapeString($name) . '")'; } @@ -2685,6 +2685,17 @@ class CompilerBase extends \PhpAot\Core\Translator return 'break;'; } + protected function parseContinue(Node\Stmt\Continue_ $v): string + { + if (!$this->inLoop) { + $this->fatalError($v, 'Cannot continue outside loop'); + } + if ($v->num and $v->num->value > 1) { + $this->fatalError($v, 'Cannot continue more than 1 level'); + } + return 'continue;'; + } + protected function parseScalarFloat(Node $expr): string { $value = $expr->value; @@ -2720,9 +2731,8 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->isVarExpr($expr)) { if ($op === 'isset') { return $this->hasVar($this->parseIdentifier($expr)) ? 'true' : 'false'; - } else { - return 'php::' . $op . '(' . $this->parseExpr($expr) . ')'; } + return 'php::' . $op . '(' . $this->parseExpr($expr) . ')'; } $list = []; @@ -2880,9 +2890,8 @@ class CompilerBase extends \PhpAot\Core\Translator } if ($this->isNameExpr($node) or $this->isIdExpr($node)) { return '"' . $id . '"'; - } else { - return $id; } + return $id; } protected function requireVar($node, string $var): void @@ -2982,11 +2991,10 @@ class CompilerBase extends \PhpAot\Core\Translator } $ce = $this->getClassEntryPtr($class); return 'php::constant(' . $ce . ', ' . $this->getLiteralString($const) . ')'; - } else { - $name = $class . '::' . $const; - $name = $this->getLiteralString($name); - return 'php::constant(' . $name . ')'; } + $name = $class . '::' . $const; + $name = $this->getLiteralString($name); + return 'php::constant(' . $name . ')'; } protected function parseThrow(mixed $expr): string @@ -3066,17 +3074,13 @@ class CompilerBase extends \PhpAot\Core\Translator return 'php::call("shell_exec", {' . $this->parseInterpolatedString($expr) . '})'; } - protected function parseGoto(Node $v): string + protected function parseGoto(Node\Stmt\Goto_ $v): string { - $this->fatalError($v, 'Goto statement is not supported'); - return 'goto ' . $v->name->name . ';'; } - protected function parseLabel(Node $v): string + protected function parseLabel(Node\Stmt\Label $v): string { - $this->fatalError($v, 'Label statement is not supported'); - return $v->name->name . ':'; } @@ -3219,11 +3223,6 @@ class CompilerBase extends \PhpAot\Core\Translator abort($expr); } - protected function parseContinue(mixed $v): string - { - return 'continue;'; - } - protected function checkVar(NodeAbstract $node, string $name): void { if (!$this->hasVar($name)) { diff --git a/src/Php/Exception/Skip.php b/src/Php/Exception/Skip.php index 40077f38..ab23e198 100644 --- a/src/Php/Exception/Skip.php +++ b/src/Php/Exception/Skip.php @@ -1,8 +1,13 @@ isScalarString($funcName)) { $funcName = $this->getLiteralString(strtolower(trim($funcName->value, '\\'))); return 'php::fn::function_exists(' . $funcName . ', true)'; - } else { - return 'php::fn::function_exists(' . $this->parseIdentifier($funcName) . ')'; } + return 'php::fn::function_exists(' . $this->parseIdentifier($funcName) . ')'; } return false; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index ed178d2d..9652b3e0 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -20,7 +20,6 @@ use PhpAot\Php\Exception\Redo; use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\Stmt\Foreach_; -use PhpParser\NodeAbstract; use PhpParser\NodeTraverser; use Symfony\Component\Yaml\Yaml;