Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions phpunit/code/variadic-fixed-arguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
function fixedAndVariadic($head, ...$tail): array { return func_get_args(); }
21 changes: 21 additions & 0 deletions phpunit/src/VariadicFixedArgumentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use TypePhp\CompilerTest;

final class VariadicFixedArgumentsTest extends BaseTest
{
public function testFixedArgumentsInitializeTheArrayBeforeVariadicMerge(): void
{
global $translator;
$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH);
$translator = $compiler;
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/variadic-fixed-arguments.php';
$compiler->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,
);
}
}
2 changes: 1 addition & 1 deletion src/Optimizer/FuncCallOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ protected function genFuncGetArgs(string $name, Node\Expr\FuncCall $expr, array
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;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/compiler/functions/func-get-args-fixed-variadic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
func_get_args retains fixed parameters before the variadic tail
--FILE--
<?php
function fixedAndTail($head, ...$tail): array { return func_get_args(); }
function multipleFixed($first, $second, ...$tail): array { return func_get_args(); }
function onlyTail(...$tail): array { return func_get_args(); }
function changedFixed(&$head, ...$tail): array { $head = 7; return func_get_args(); }
function main(): void {
echo json_encode(fixedAndTail(1,2,3)), "\n";
echo json_encode(fixedAndTail(1)), "\n";
echo json_encode(multipleFixed('a','b',3,4)), "\n";
echo json_encode(onlyTail(1,2)), "\n";
$head = 1;
echo json_encode(changedFixed($head,2,3)), ':', $head, "\n";
}
?>
--EXPECT--
[1,2,3]
[1]
["a","b",3,4]
[1,2]
[7,2,3]:7
14 changes: 10 additions & 4 deletions tests/compiler/functions/func_get_args.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading