From a4ac86524cf80c6f6f815a13f0a83463af7c48cd Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 2 Jun 2026 10:41:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor(compiler):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=85=B3=E9=94=AE=E5=AD=97=E6=96=B9=E6=B3=95=E5=88=86=E6=B4=BE?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 简化了关键字方法和扩展方法的检测流程 - 统一了接收器类型的获取方式 - 提取了重复的空值检查逻辑 - 改进了代码结构和可读性 - 为关键字扩展方法添加了测试用例 --- src/Php/CompilerBase.php | 25 +++++++------------------ tests/aot/keyword_extension/001.phpt | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 tests/aot/keyword_extension/001.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 9e25d66a..358b4a51 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -5444,34 +5444,23 @@ class CompilerBase extends \PhpAot\Core\Translator $magicMethod = false; $method = $this->identifierToStr($expr->name, literal: true); - // to* conversion methods are language keywords — always dispatched directly + // keyword methods (to* builtins + __ extensions) — dispatched before type-specific logic if ($this->isNamedMethod($expr->name)) { $methodName = $expr->name->toString(); + $receiverType = $this->isVarExpr($expr->var) ? $this->getVarType($object) : $this->detectTypeOfExpr($expr->var); + if ($receiverType === self::TYPE_VOID) { + $this->fatalError($expr->var, 'Cannot call method on void'); + } + // to* builtins if (isset(self::KEYWORD_METHOD_MAP[$methodName])) { - if ($this->isVarExpr($expr->var)) { - $receiverType = $this->getVarType($object); - } else { - $receiverType = $this->detectTypeOfExpr($expr->var); - } - if ($receiverType === self::TYPE_VOID) { - $this->fatalError($expr->var, 'Cannot call method on void'); - } if ($methodName === 'toObject') { return $this->genToObjectCall($expr, $object); } return $this->genToConvertCall($object, $methodName, $receiverType); } - // keyword extension methods (__ functions) — dispatched before type-specific logic + // __ keyword extensions $kwExt = $this->findKeywordExtensionMethod($methodName); if ($kwExt) { - if ($this->isVarExpr($expr->var)) { - $receiverType = $this->getVarType($object); - } else { - $receiverType = $this->detectTypeOfExpr($expr->var); - } - if ($receiverType === self::TYPE_VOID) { - $this->fatalError($expr->var, 'Cannot call method on void'); - } return $this->parseUniversalMethodCall($expr, $object, $methodName, $kwExt, $this->isVarExpr($expr->var)); } } diff --git a/tests/aot/keyword_extension/001.phpt b/tests/aot/keyword_extension/001.phpt new file mode 100644 index 00000000..a33f39a4 --- /dev/null +++ b/tests/aot/keyword_extension/001.phpt @@ -0,0 +1,26 @@ +--TEST-- +keyword extension method: varDump() +--FILE-- +varDump(); + + $int_val = 42; + $int_val->varDump(); + + $float_val = 3.14; + $float_val->varDump(); +} +?> +--EXPECT-- +string(11) "hello world" +int(42) +float(3.14)