feat(php): 添加对void类型方法调用的检查

- 在CompilerBase.php中检测接收器类型为void时抛出致命错误
- 为变量表达式方法调用添加void类型检查
- 为命名方法调用添加void类型检查
- 在单元测试中添加void方法调用测试用例
- 创建void方法调用测试代码文件
pull/1/head
韩天峰 3 months ago
parent 057e25a950
commit 52fcc86660
  1. 9
      phpunit/code/void-method-call.php
  2. 5
      phpunit/src/UniversalMethodCallTest.php
  3. 6
      src/Php/CompilerBase.php

@ -0,0 +1,9 @@
<?php
function bar(): void {
var_dump(__FUNCTION__);
}
function main()
{
var_dump(bar()->toString());
}

@ -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');
}
}

@ -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);

Loading…
Cancel
Save