Skip to content

Commit 0aeaa99

Browse files
committed
Add support for variadic
1 parent 9c56991 commit 0aeaa99

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/php/lang/reflection/Routine.class.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public function parameters(): Parameters {
9696
public static function pass($reflect, $args) {
9797
$pass= [];
9898
foreach ($reflect->getParameters() as $i => $param) {
99-
if (isset($args[$param->name])) {
99+
if ($param->isVariadic()) {
100+
while ($args) $pass[]= array_shift($args);
101+
} else if (isset($args[$param->name])) {
100102
$pass[]= $args[$param->name];
101103
unset($args[$param->name]);
102104
} else if (isset($args[$i])) {

src/test/php/lang/reflection/unittest/ArgumentPassingTest.class.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,16 @@ public function pass_without_optional() {
7272
$f= new ReflectionFunction(fn($a, $b= 0) => null);
7373
Assert::equals([1, 0], Routine::pass($f, [1]));
7474
}
75+
76+
#[Test]
77+
public function pass_variadic() {
78+
$f= new ReflectionFunction(fn(... $a) => null);
79+
Assert::equals([1, 2], Routine::pass($f, [1, 2]));
80+
}
81+
82+
#[Test]
83+
public function pass_variadic_after() {
84+
$f= new ReflectionFunction(fn($a, ... $b) => null);
85+
Assert::equals([1, 2, 3], Routine::pass($f, [1, 2, 3]));
86+
}
7587
}

0 commit comments

Comments
 (0)