From a510801d249d9ec969410762f975059b86f26ddd Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 2 Jun 2026 17:34:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=E4=BA=8C?= =?UTF-8?q?=E8=BF=9B=E5=88=B6=E8=BF=90=E7=AE=97=E7=AC=A6=E7=9B=B8=E7=AD=89?= =?UTF-8?q?=E6=80=A7=E6=AF=94=E8=BE=83=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除重复的类型检测和转换代码 - 引入 genBigNumericCmp 方法统一处理大数值比较 - 简化 Equal 和 NotEqual 运算符的实现 - 优化 Spaceship 运算符的比较逻辑 - 统一字符串转换方法的调用方式 --- src/Php/CompilerBase.php | 116 ++++++++------------------------------- 1 file changed, 23 insertions(+), 93 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index ee07e228..ba1dbbfa 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3979,82 +3979,14 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseBinaryOpEqual(Expr\BinaryOp\Equal $expr): string { - $leftType = $this->detectTypeOfExpr($expr->left); - $rightType = $this->detectTypeOfExpr($expr->right); - if ($leftType === self::TYPE_BIGFLOAT || $rightType === self::TYPE_BIGFLOAT) { - $leftExpr = $this->parseExpr($expr->left); - $rightExpr = $this->parseExpr($expr->right); - if ($leftType !== self::TYPE_BIGFLOAT) { - $leftExpr = $this->convertBigFloatExpr($leftExpr, $leftType); - } - if ($rightType !== self::TYPE_BIGFLOAT) { - $rightExpr = $this->convertBigFloatExpr($rightExpr, $rightType); - } - return 'php::BigFloat::cmp(' . $leftExpr . ', ' . $rightExpr . ') == 0'; - } - if ($leftType === self::TYPE_BIGINT || $rightType === self::TYPE_BIGINT) { - $leftExpr = $this->parseExpr($expr->left); - $rightExpr = $this->parseExpr($expr->right); - if ($leftType !== self::TYPE_BIGINT) { - $leftExpr = $this->convertBigIntExpr($leftExpr, $leftType); - } - if ($rightType !== self::TYPE_BIGINT) { - $rightExpr = $this->convertBigIntExpr($rightExpr, $rightType); - } - return 'php::BigInt::cmp(' . $leftExpr . ', ' . $rightExpr . ') == 0'; - } - if ($leftType === self::TYPE_DECIMAL || $rightType === self::TYPE_DECIMAL) { - $leftExpr = $this->parseExpr($expr->left); - $rightExpr = $this->parseExpr($expr->right); - if ($leftType !== self::TYPE_DECIMAL) { - $leftExpr = $this->convertDecimalExpr($leftExpr, $leftType, $expr->left); - } - if ($rightType !== self::TYPE_DECIMAL) { - $rightExpr = $this->convertDecimalExpr($rightExpr, $rightType, $expr->right); - } - return 'php::Decimal::cmp(' . $leftExpr . ', ' . $rightExpr . ') == 0'; - } - return 'php::equals(' . $this->parseCompareExpr($expr->left) . ', ' . $this->parseCompareExpr($expr->right) . ')'; + return $this->genBigNumericCmp($expr, ' == 0') + ?? 'php::equals(' . $this->parseCompareExpr($expr->left) . ', ' . $this->parseCompareExpr($expr->right) . ')'; } protected function parseBinaryOpNotEqual(Expr\BinaryOp\NotEqual $expr): string { - $leftType = $this->detectTypeOfExpr($expr->left); - $rightType = $this->detectTypeOfExpr($expr->right); - if ($leftType === self::TYPE_BIGFLOAT || $rightType === self::TYPE_BIGFLOAT) { - $leftExpr = $this->parseExpr($expr->left); - $rightExpr = $this->parseExpr($expr->right); - if ($leftType !== self::TYPE_BIGFLOAT) { - $leftExpr = $this->convertBigFloatExpr($leftExpr, $leftType); - } - if ($rightType !== self::TYPE_BIGFLOAT) { - $rightExpr = $this->convertBigFloatExpr($rightExpr, $rightType); - } - return 'php::BigFloat::cmp(' . $leftExpr . ', ' . $rightExpr . ') != 0'; - } - if ($leftType === self::TYPE_BIGINT || $rightType === self::TYPE_BIGINT) { - $leftExpr = $this->parseExpr($expr->left); - $rightExpr = $this->parseExpr($expr->right); - if ($leftType !== self::TYPE_BIGINT) { - $leftExpr = $this->convertBigIntExpr($leftExpr, $leftType); - } - if ($rightType !== self::TYPE_BIGINT) { - $rightExpr = $this->convertBigIntExpr($rightExpr, $rightType); - } - return 'php::BigInt::cmp(' . $leftExpr . ', ' . $rightExpr . ') != 0'; - } - if ($leftType === self::TYPE_DECIMAL || $rightType === self::TYPE_DECIMAL) { - $leftExpr = $this->parseExpr($expr->left); - $rightExpr = $this->parseExpr($expr->right); - if ($leftType !== self::TYPE_DECIMAL) { - $leftExpr = $this->convertDecimalExpr($leftExpr, $leftType, $expr->left); - } - if ($rightType !== self::TYPE_DECIMAL) { - $rightExpr = $this->convertDecimalExpr($rightExpr, $rightType, $expr->right); - } - return 'php::Decimal::cmp(' . $leftExpr . ', ' . $rightExpr . ') != 0'; - } - return '!php::equals(' . $this->parseCompareExpr($expr->left) . ', ' . $this->parseCompareExpr($expr->right) . ')'; + return $this->genBigNumericCmp($expr, ' != 0') + ?? '!php::equals(' . $this->parseCompareExpr($expr->left) . ', ' . $this->parseCompareExpr($expr->right) . ')'; } protected function parseBinaryOpIdentical(Expr\BinaryOp $expr): string @@ -4247,11 +4179,18 @@ class CompilerBase extends \PhpAot\Core\Translator } protected function parseBinaryOpSpaceship(Expr\BinaryOp\Spaceship $expr): string + { + return $this->genBigNumericCmp($expr) + ?? 'php::compare(' . $this->parseIdentifier($expr->left) . ', ' . $this->parseIdentifier($expr->right) . ')'; + } + + protected function genBigNumericCmp(Expr\BinaryOp $expr, string $suffix = ''): ?string { $leftType = $this->detectTypeOfExpr($expr->left); $rightType = $this->detectTypeOfExpr($expr->right); + if ($leftType === self::TYPE_BIGFLOAT || $rightType === self::TYPE_BIGFLOAT) { - $leftExpr = $this->parseExpr($expr->left); + $leftExpr = $this->parseExpr($expr->left); $rightExpr = $this->parseExpr($expr->right); if ($leftType !== self::TYPE_BIGFLOAT) { $leftExpr = $this->convertBigFloatExpr($leftExpr, $leftType); @@ -4259,10 +4198,10 @@ class CompilerBase extends \PhpAot\Core\Translator if ($rightType !== self::TYPE_BIGFLOAT) { $rightExpr = $this->convertBigFloatExpr($rightExpr, $rightType); } - return 'php::BigFloat::cmp(' . $leftExpr . ', ' . $rightExpr . ')'; + return 'php::BigFloat::cmp(' . $leftExpr . ', ' . $rightExpr . ')' . $suffix; } if ($leftType === self::TYPE_BIGINT || $rightType === self::TYPE_BIGINT) { - $leftExpr = $this->parseExpr($expr->left); + $leftExpr = $this->parseExpr($expr->left); $rightExpr = $this->parseExpr($expr->right); if ($leftType !== self::TYPE_BIGINT) { $leftExpr = $this->convertBigIntExpr($leftExpr, $leftType); @@ -4270,10 +4209,10 @@ class CompilerBase extends \PhpAot\Core\Translator if ($rightType !== self::TYPE_BIGINT) { $rightExpr = $this->convertBigIntExpr($rightExpr, $rightType); } - return 'php::BigInt::cmp(' . $leftExpr . ', ' . $rightExpr . ')'; + return 'php::BigInt::cmp(' . $leftExpr . ', ' . $rightExpr . ')' . $suffix; } if ($leftType === self::TYPE_DECIMAL || $rightType === self::TYPE_DECIMAL) { - $leftExpr = $this->parseExpr($expr->left); + $leftExpr = $this->parseExpr($expr->left); $rightExpr = $this->parseExpr($expr->right); if ($leftType !== self::TYPE_DECIMAL) { $leftExpr = $this->convertDecimalExpr($leftExpr, $leftType, $expr->left); @@ -4281,11 +4220,10 @@ class CompilerBase extends \PhpAot\Core\Translator if ($rightType !== self::TYPE_DECIMAL) { $rightExpr = $this->convertDecimalExpr($rightExpr, $rightType, $expr->right); } - return 'php::Decimal::cmp(' . $leftExpr . ', ' . $rightExpr . ')'; + return 'php::Decimal::cmp(' . $leftExpr . ', ' . $rightExpr . ')' . $suffix; } - $left = $this->parseIdentifier($expr->left); - $right = $this->parseIdentifier($expr->right); - return 'php::compare(' . $left . ', ' . $right . ')'; + + return null; } /** @@ -4429,18 +4367,10 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseCastString(Expr\Cast\String_ $node): string { - $type = $this->detectTypeOfExpr($node->expr); - $expr = $this->parseExpr($node->expr); - if ($type === self::TYPE_BIGINT) { - return 'php::BigInt::toString(' . $expr . ')'; - } - if ($type === self::TYPE_BIGFLOAT) { - return 'php::BigFloat::toString(' . $expr . ')'; - } - if ($type === self::TYPE_DECIMAL) { - return 'php::Decimal::toString(' . $expr . ')'; - } - return $this->convertStringExpr($expr); + return $this->convertExprToStringByType( + $this->parseExpr($node->expr), + $this->detectTypeOfExpr($node->expr) + ); } protected function parseCastBool(Expr\Cast\Bool_ $node): string