From b7236ce38b7c0f79df89f437e832b222d6f4920d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 2 Apr 2026 17:27:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=E9=93=BE?= =?UTF-8?q?=E5=BC=8F=E8=A1=A8=E8=BE=BE=E5=BC=8F=E8=A7=A3=E6=9E=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将操作符匹配逻辑提取到独立的 getChainedFunc 方法中 - 简化 parseChainedExpr 方法中的条件判断结构 - 统一链式函数调用的处理方式 - 移除重复的操作符匹配代码 - 提高代码可读性和维护性 --- src/Php/CompilerBase.php | 22 ++++++++++++---------- tests/aot/{ref-005.phpt => ref/005.phpt} | 0 2 files changed, 12 insertions(+), 10 deletions(-) rename tests/aot/{ref-005.phpt => ref/005.phpt} (100%) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 775e44f7..c61e76b4 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3369,8 +3369,8 @@ class CompilerBase extends \PhpAot\Core\Translator $code = ''; $expr = $this->parseIdentifier($node->expr); - $code .= self::TYPE_ARRAY . " {$iteratorVar} = " . $expr . ';' . PHP_EOL; $code .= $this->parseBeforeStmtLines() . PHP_EOL; + $code .= self::TYPE_ARRAY . " {$iteratorVar} = " . $expr . ';' . PHP_EOL; $code .= $this->parseForeachArray($node, $iteratorVar); return $code; @@ -3637,6 +3637,15 @@ class CompilerBase extends \PhpAot\Core\Translator } } + protected function getChainedFunc(string $op): string + { + return match ($op) { + self::OP_ISSET => 'php::exists', + self::OP_NOT_EMPTY => '!php::empty', + default => 'php::' . $op, + }; + } + protected function parseChainedExpr(NodeAbstract $node, string $op, bool $getValue = false): string { $expr = $node; @@ -3644,11 +3653,8 @@ class CompilerBase extends \PhpAot\Core\Translator if ($op === self::OP_ISSET) { $var = $this->parseIdentifier($expr); return $this->hasVar($var) ? 'php::exists(' . $var . ')' : 'false'; - } elseif ($op === self::OP_NOT_EMPTY) { - return '!php::empty(' . $this->parseExpr($expr) . ')'; - } else { - return 'php::empty(' . $this->parseExpr($expr) . ')'; } + return $this->getChainedFunc($op) . '(' . $this->parseExpr($expr) . ')'; } $list = []; @@ -3675,11 +3681,7 @@ class CompilerBase extends \PhpAot\Core\Translator } $list = array_reverse($list); - $fn = match ($op) { - self::OP_ISSET => 'php::exists', - self::OP_NOT_EMPTY => '!php::empty', - default => 'php::empty', - }; + $fn = $this->getChainedFunc($op); if ($getValue) { $result = $this->addTmpVar(self::TYPE_VAR); diff --git a/tests/aot/ref-005.phpt b/tests/aot/ref/005.phpt similarity index 100% rename from tests/aot/ref-005.phpt rename to tests/aot/ref/005.phpt