fix(php): 修复函数参数解析中的默认值计数问题

- 为parseParams方法添加完整的PHPDoc注释
- 将$params参数类型声明为array以增强类型安全
- 引入defaultValueCount变量来准确计算默认值参数数量
- 修复required参数计数逻辑,确保正确处理带默认值的参数
- 添加对classDef为空情况的处理,避免未定义行为
- 调整头文件包含顺序以符合代码风格要求
- 添加函数默认参数测试用例验证修复效果
pull/1/head
韩天峰 6 months ago
parent d8e164b32c
commit ed45578ef1
  1. 20
      src/Php/CompilerBase.php
  2. 2
      src/cpp/main.cc
  3. 19
      tests/aot/func-default-param.phpt

@ -947,10 +947,16 @@ class CompilerBase extends \PhpAot\Core\Translator
}
}
protected function parseParams($params, FunctionDef $functionDef): void
/**
* @param $params array<Node\Param>
* @param FunctionDef $functionDef
* @return void
*/
protected function parseParams(array $params, FunctionDef $functionDef): void
{
$list = [];
$functionDef->argCountRequired = count($params);
$defaultValueCount = 0;
$last = array_key_last($params);
foreach ($params as $i => $param) {
@ -984,13 +990,14 @@ class CompilerBase extends \PhpAot\Core\Translator
$argInfo->byRef = $param->byRef;
$argInfo->variadic = $param->variadic;
$argInfo->property = $param->isPromoted();
if (isset($param->default)) {
$functionDef->argCountRequired = count($list) - 1;
if ($param->default) {
$defaultValueCount++;
$argInfo->default = $this->parseParamDefaultValue($param->default);
}
$functionDef->argInfoList[] = $argInfo;
}
$functionDef->params = implode(', ', $list);
$functionDef->argCountRequired -= $defaultValueCount;
}
protected function getComment(Node\Stmt $v, string $class): string
@ -3895,7 +3902,12 @@ class CompilerBase extends \PhpAot\Core\Translator
$class = $this->objects[$object];
$nativeFunc = $this->getNativeMethod($expr, $class, $method);
}
$fullMethodName = $classDef->getNamespacedName(false) . '::' . $method;
if ($classDef) {
$fullMethodName = $classDef->getNamespacedName(false) . '::' . $method;
} else {
$fullMethodName = $object . '::' . $method;
}
// 存在子类同名方法,需要转为动态调用
if (isset($this->classMethodOverride[$fullMethodName]) and $this->classMethodOverride[$fullMethodName]) {
return false;

@ -1,9 +1,9 @@
#include "sapi/embed/php_embed.h"
#if PPROF_ON
#include <gperftools/profiler.h>
#endif
#include <php_aot_helper.h>
#include "sapi/embed/php_embed.h"
extern zend_module_entry *php_embed_get_module();

@ -0,0 +1,19 @@
--TEST--
static calls
--FILE--
<?php
class Worker
{
public static function stopAll(int $code = 999, string $log = 'foo'): void
{
var_dump($code, $log);
}
}
function main() {
Worker::stopAll();
}
?>
--EXPECTF--
int(999)
string(3) "foo"
Loading…
Cancel
Save