From ed45578ef1b4a9390b900c67c6338b4fa2a23459 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 5 Mar 2026 12:13:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=90=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC=E8=AE=A1=E6=95=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为parseParams方法添加完整的PHPDoc注释 - 将$params参数类型声明为array以增强类型安全 - 引入defaultValueCount变量来准确计算默认值参数数量 - 修复required参数计数逻辑,确保正确处理带默认值的参数 - 添加对classDef为空情况的处理,避免未定义行为 - 调整头文件包含顺序以符合代码风格要求 - 添加函数默认参数测试用例验证修复效果 --- src/Php/CompilerBase.php | 20 ++++++++++++++++---- src/cpp/main.cc | 2 +- tests/aot/func-default-param.phpt | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 tests/aot/func-default-param.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index be07b659..0ae0d98d 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -947,10 +947,16 @@ class CompilerBase extends \PhpAot\Core\Translator } } - protected function parseParams($params, FunctionDef $functionDef): void + /** + * @param $params array + * @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; diff --git a/src/cpp/main.cc b/src/cpp/main.cc index 32b5cefd..3e099294 100644 --- a/src/cpp/main.cc +++ b/src/cpp/main.cc @@ -1,9 +1,9 @@ -#include "sapi/embed/php_embed.h" #if PPROF_ON #include #endif #include +#include "sapi/embed/php_embed.h" extern zend_module_entry *php_embed_get_module(); diff --git a/tests/aot/func-default-param.phpt b/tests/aot/func-default-param.phpt new file mode 100644 index 00000000..3e121ab2 --- /dev/null +++ b/tests/aot/func-default-param.phpt @@ -0,0 +1,19 @@ +--TEST-- +static calls +--FILE-- + +--EXPECTF-- +int(999) +string(3) "foo"