From 248156b3b4ae958cff38533a1efa15ed8ce2b5eb Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 1 Jul 2026 11:06:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84PHP=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=99=A8=E4=B8=AD=E7=9A=84=E6=9D=A1=E4=BB=B6=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复BinaryOpTrait.php中的缩进问题,统一if语句的缩进格式 - 修改CompilerBase.php中接口常量处理逻辑,为数组类型常量添加原生名称前缀 - 重构多个辅助函数将引用传递改为返回值方式,包括appendStmtLines和相关返回函数 - 简化条件表达式和匹配表达式的代码生成逻辑,提高代码可读性 - 优化布尔表达式短路求值的实现方式 - 统一代码风格,减少不必要的变量赋值操作 --- src/Php/CompilerBase.php | 53 ++++++++++++++++++-------------- src/Php/Parser/BinaryOpTrait.php | 17 +++++----- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 05595d5f..4732941e 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2171,9 +2171,12 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont if (!$interfaceDef->hasConstant($const)) { continue; } - $classDef = $interfaceDef; - $constDef = $interfaceDef->constants[$const]; - break; + $interfaceConstDef = $interfaceDef->constants[$const]; + if ($interfaceConstDef->type === self::TYPE_ARRAY) { + return self::PREFIX . $this->getNativeName($interfaceConstDef->name, $interfaceDef->namespace, $interfaceDef->name); + } + $expr->setAttribute('nativeConst', $interfaceConstDef); + return $interfaceConstDef->value; } } if ($constDef === null) { @@ -3970,34 +3973,36 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont $else = 'php::Var(' . $else . ')'; } if ($hasBranchStmts) { - $appendStmtLines = function (string &$code, array $stmts): void { - if ($stmts) { - $code .= $this->getIndent() . implode(PHP_EOL . $this->getIndent(), $stmts) . PHP_EOL; + $appendStmtLines = function (array $stmts): string { + if (!$stmts) { + return ''; } + return $this->getIndent() . implode(PHP_EOL . $this->getIndent(), $stmts) . PHP_EOL; }; - $appendReturn = function (string &$code, string $value, array $beforeStmts, array $afterStmts) use ($appendStmtLines): void { - $appendStmtLines($code, $beforeStmts); + $appendReturn = function (string $value, array $beforeStmts, array $afterStmts) use ($appendStmtLines): string { + $code = $appendStmtLines($beforeStmts); if ($afterStmts) { $tmpVar = $this->addTmpVar(self::TYPE_VAR); $code .= $this->getIndent() . "{$tmpVar} = {$value};"; - $appendStmtLines($code, $afterStmts); + $code .= $appendStmtLines($afterStmts); $code .= $this->getIndent() . 'return ' . $tmpVar . ';'; } else { $code .= $this->getIndent() . 'return php::Var(' . $value . ');'; } + return $code; }; $code = '[&]() -> ' . self::TYPE_VAR . '{'; - $appendStmtLines($code, $condBeforeStmts); + $code .= $appendStmtLines($condBeforeStmts); if ($condAfterStmts) { $condTmpVar = $this->addTmpVar(self::TYPE_VAR); $code .= $this->getIndent() . "{$condTmpVar} = {$cond};"; - $appendStmtLines($code, $condAfterStmts); + $code .= $appendStmtLines($condAfterStmts); $cond = $condTmpVar; } $code .= $this->getIndent() . 'if (' . $cond . ') {'; - $appendReturn($code, $if, $ifBeforeStmts, $ifAfterStmts); + $code .= $appendReturn($if, $ifBeforeStmts, $ifAfterStmts); $code .= $this->getIndent() . '} else {'; - $appendReturn($code, $else, $elseBeforeStmts, $elseAfterStmts); + $code .= $appendReturn($else, $elseBeforeStmts, $elseAfterStmts); $code .= $this->getIndent() . '}'; $code .= $this->getIndent() . '}()'; return $code; @@ -4030,24 +4035,26 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont return [$value, $beforeStmts, $afterStmts]; }; - $appendStmtLines = function (string &$code, array $stmts): void { - if ($stmts) { - $code .= $this->getIndent() . implode(PHP_EOL . $this->getIndent(), $stmts) . PHP_EOL; + $appendStmtLines = function (array $stmts): string { + if (!$stmts) { + return ''; } + return $this->getIndent() . implode(PHP_EOL . $this->getIndent(), $stmts) . PHP_EOL; }; - $appendMatchReturn = function (string &$code, NodeAbstract $body) use ($parseExprWithStmts, $appendStmtLines): void { + $appendMatchReturn = function (NodeAbstract $body) use ($parseExprWithStmts, $appendStmtLines): string { $this->assertExprCanBeUsedAsValue($body, 'match arm'); [$value, $beforeStmts, $afterStmts] = $parseExprWithStmts($body); - $appendStmtLines($code, $beforeStmts); + $code = $appendStmtLines($beforeStmts); if ($afterStmts) { $tmpVar = $this->addTmpVar(self::TYPE_VAR); $code .= $this->getIndent() . "{$tmpVar} = {$value};"; - $appendStmtLines($code, $afterStmts); + $code .= $appendStmtLines($afterStmts); $code .= $this->getIndent() . 'return ' . $tmpVar . ';'; } else { $code .= $this->getIndent() . 'return ' . $value . ';'; } + return $code; }; $code = '[&]() -> ' . self::TYPE_VAR . '{'; @@ -4066,24 +4073,24 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont $this->assertExprCanBeUsedAsValue($cond, 'match arm condition'); [$condValue, $beforeStmts, $afterStmts] = $parseExprWithStmts($cond); $code .= $this->getIndent() . 'if (!' . $matched . ') {'; - $appendStmtLines($code, $beforeStmts); + $code .= $appendStmtLines($beforeStmts); if ($afterStmts) { $condTmpVar = $this->addTmpVar(self::TYPE_VAR); $code .= $this->getIndent() . "{$condTmpVar} = {$condValue};"; - $appendStmtLines($code, $afterStmts); + $code .= $appendStmtLines($afterStmts); $condValue = $condTmpVar; } $code .= $this->getIndent() . $matched . ' = php::same(' . $var . ', ' . $condValue . ');'; $code .= $this->getIndent() . '}'; } $code .= $this->getIndent() . 'if (' . $matched . ') {'; - $appendMatchReturn($code, $arm->body); + $code .= $appendMatchReturn($arm->body); $code .= $this->getIndent() . '}'; } if ($default) { $code .= $this->getIndent() . '{'; - $appendMatchReturn($code, $default); + $code .= $appendMatchReturn($default); $code .= $this->getIndent() . '}'; } else { $code .= $this->getIndent() . '{ return php::throwException("UnhandledMatchError", "Unhandled match case"); }'; diff --git a/src/Php/Parser/BinaryOpTrait.php b/src/Php/Parser/BinaryOpTrait.php index 92552473..976cebc6 100644 --- a/src/Php/Parser/BinaryOpTrait.php +++ b/src/Php/Parser/BinaryOpTrait.php @@ -39,7 +39,7 @@ trait BinaryOpTrait if ($leftType === self::TYPE_DECIMAL || $rightType === self::TYPE_DECIMAL) { $this->fatalError($left, 'Cannot mix BigFloat and Decimal implicitly. Use std::bigFloat() to convert explicitly.'); } - if ($leftType !== self::TYPE_BIGFLOAT) { + if ($leftType !== self::TYPE_BIGFLOAT) { $leftExpr = $this->convertBigFloatExpr($leftExpr, $leftType); } if ($rightType !== self::TYPE_BIGFLOAT) { @@ -61,7 +61,7 @@ trait BinaryOpTrait if ($leftType === self::TYPE_BIGINT || $rightType === self::TYPE_BIGINT) { $this->fatalError($left, 'Cannot mix BigInt and Decimal implicitly. Use std::decimal() or std::bigInt() to convert explicitly.'); } - if ($leftType !== self::TYPE_DECIMAL) { + if ($leftType !== self::TYPE_DECIMAL) { $leftExpr = $this->convertDecimalExpr($leftExpr, $leftType, $left); } if ($rightType !== self::TYPE_DECIMAL) { @@ -92,7 +92,7 @@ trait BinaryOpTrait $method = ($op === '<<') ? 'bitShiftLeft' : 'bitShiftRight'; return 'php::BigInt::' . $method . '(' . $leftExpr . ', ' . $rightExpr . ')'; } - if ($leftType !== self::TYPE_BIGINT) { + if ($leftType !== self::TYPE_BIGINT) { $leftExpr = $this->convertBigIntExpr($leftExpr, $leftType); } if ($rightType !== self::TYPE_BIGINT) { @@ -427,21 +427,22 @@ trait BinaryOpTrait return '(' . $leftBool . ' ' . $op . ' ' . $this->convertBoolExpr((string) $rightExpr) . ')'; } - $appendStmtLines = function (string &$code, array $stmts): void { - if ($stmts) { - $code .= $this->getIndent() . implode(PHP_EOL . $this->getIndent(), $stmts) . PHP_EOL; + $appendStmtLines = function (array $stmts): string { + if (!$stmts) { + return ''; } + return $this->getIndent() . implode(PHP_EOL . $this->getIndent(), $stmts) . PHP_EOL; }; $shortCircuitValue = $op === '&&' ? 'false' : 'true'; $rightCondition = $op === '&&' ? $leftBool : '!(' . $leftBool . ')'; $code = '[&]() -> bool {'; $code .= $this->getIndent() . 'if (' . $rightCondition . ') {'; - $appendStmtLines($code, $rightBeforeStmts); + $code .= $appendStmtLines($rightBeforeStmts); if ($rightAfterStmts) { $rightTmpVar = $this->addTmpVar(self::TYPE_VAR); $code .= $this->getIndent() . $rightTmpVar . ' = ' . $rightExpr . ';'; - $appendStmtLines($code, $rightAfterStmts); + $code .= $appendStmtLines($rightAfterStmts); $rightExpr = $rightTmpVar; } $code .= $this->getIndent() . 'return ' . $this->convertBoolExpr((string) $rightExpr) . ';';