From 62ec66bb6ca23bbaf0c845b8e61a3b9c3e1c8358 Mon Sep 17 00:00:00 2001 From: yavon007 Date: Mon, 14 Sep 2026 13:36:55 +0800 Subject: [PATCH] Retain fixed arguments in func_get_args for variadic functions (#109) * Fix lost fixed arguments in variadic func_get_args * Fix closing tag in variadic argument PHPT fixture --- phpunit/code/variadic-fixed-arguments.php | 2 ++ phpunit/src/VariadicFixedArgumentsTest.php | 21 +++++++++++++++++ src/Optimizer/FuncCallOptimizer.php | 2 +- .../func-get-args-fixed-variadic.phpt | 23 +++++++++++++++++++ tests/compiler/functions/func_get_args.phpt | 14 +++++++---- 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 phpunit/code/variadic-fixed-arguments.php create mode 100644 phpunit/src/VariadicFixedArgumentsTest.php create mode 100644 tests/compiler/functions/func-get-args-fixed-variadic.phpt diff --git a/phpunit/code/variadic-fixed-arguments.php b/phpunit/code/variadic-fixed-arguments.php new file mode 100644 index 00000000..d8e78342 --- /dev/null +++ b/phpunit/code/variadic-fixed-arguments.php @@ -0,0 +1,2 @@ +addFiles([$source]); + $compiler->prepareFile($source); + $code = file_get_contents($compiler->convertFile($source)); + self::assertMatchesRegularExpression( + '/(tmp_var_\d+) = php::Array\{\s*head\s*\};\s*\1\.merge\(tail\);/', + $code, + ); + } +} diff --git a/src/Optimizer/FuncCallOptimizer.php b/src/Optimizer/FuncCallOptimizer.php index 2a28700a..cba6f0bc 100644 --- a/src/Optimizer/FuncCallOptimizer.php +++ b/src/Optimizer/FuncCallOptimizer.php @@ -1367,7 +1367,7 @@ trait FuncCallOptimizer foreach ($funcDef->argInfoList as $i => $argInfo) { if ($argInfo->variadic) { $tmpVar = $this->addTmpVar(Type::ARRAY); - $this->context->beforeStmtLines[] = $this->genArray($list) . ';'; + $this->context->beforeStmtLines[] = $tmpVar . ' = ' . $this->genArray($list) . ';'; $this->context->beforeStmtLines[] = $tmpVar . '.merge(' . $argInfo->name . ');'; return $tmpVar; } diff --git a/tests/compiler/functions/func-get-args-fixed-variadic.phpt b/tests/compiler/functions/func-get-args-fixed-variadic.phpt new file mode 100644 index 00000000..f1dd9e3b --- /dev/null +++ b/tests/compiler/functions/func-get-args-fixed-variadic.phpt @@ -0,0 +1,23 @@ +--TEST-- +func_get_args retains fixed parameters before the variadic tail +--FILE-- + +--EXPECT-- +[1,2,3] +[1] +["a","b",3,4] +[1,2] +[7,2,3]:7 diff --git a/tests/compiler/functions/func_get_args.phpt b/tests/compiler/functions/func_get_args.phpt index 6219fc7a..18ec89ee 100644 --- a/tests/compiler/functions/func_get_args.phpt +++ b/tests/compiler/functions/func_get_args.phpt @@ -44,11 +44,17 @@ array(5) { [4]=> int(10) } -array(3) { +array(6) { [0]=> - int(8) + int(1) [1]=> - int(10) + int(3) [2]=> + int(5) + [3]=> + int(8) + [4]=> + int(10) + [5]=> int(12) -} \ No newline at end of file +}