From 52fcc86660abb443e49f9e9f35c43d6297907810 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 1 Jun 2026 19:54:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9void?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=96=B9=E6=B3=95=E8=B0=83=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在CompilerBase.php中检测接收器类型为void时抛出致命错误 - 为变量表达式方法调用添加void类型检查 - 为命名方法调用添加void类型检查 - 在单元测试中添加void方法调用测试用例 - 创建void方法调用测试代码文件 --- phpunit/code/void-method-call.php | 9 +++++++++ phpunit/src/UniversalMethodCallTest.php | 5 +++++ src/Php/CompilerBase.php | 6 ++++++ 3 files changed, 20 insertions(+) create mode 100644 phpunit/code/void-method-call.php diff --git a/phpunit/code/void-method-call.php b/phpunit/code/void-method-call.php new file mode 100644 index 00000000..b02158b0 --- /dev/null +++ b/phpunit/code/void-method-call.php @@ -0,0 +1,9 @@ +toString()); +} diff --git a/phpunit/src/UniversalMethodCallTest.php b/phpunit/src/UniversalMethodCallTest.php index 71016eb1..b1e69007 100644 --- a/phpunit/src/UniversalMethodCallTest.php +++ b/phpunit/src/UniversalMethodCallTest.php @@ -16,4 +16,9 @@ class UniversalMethodCallTest extends \BaseTest { $this->exec('Cannot call mutating method `push()` on a non-variable expression', 'universal-method-mutating-expr.php'); } + + public function testVoidMethodCall() + { + $this->exec('Cannot call method on void', 'void-method-call.php'); + } } diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 13a3e17c..da9c7d26 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -5438,6 +5438,9 @@ class CompilerBase extends \PhpAot\Core\Translator } 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); } @@ -5479,6 +5482,9 @@ class CompilerBase extends \PhpAot\Core\Translator // 表达式返回值也可使用内置方法:fn()->method(), $obj->fn()->method(), Foo::fn()->method(), $obj->prop->method() if (!$this->isVarExpr($expr->var) and $this->isNamedMethod($expr->name)) { $type = $this->detectTypeOfExpr($expr->var); + if ($type === self::TYPE_VOID) { + $this->fatalError($expr->var, 'Cannot call method on void'); + } if ($type !== self::TYPE_VAR && !$this->checkArgType($type, self::TYPE_OBJECT)) { $methodName = $expr->name->toString(); $fn = $this->findUniversalMethodAnyType($type, $methodName);