Skip to content

Commit

Permalink
fix variadic
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Feb 9, 2025
1 parent 87163d6 commit 3af8b21
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
23 changes: 14 additions & 9 deletions src/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private function assertMinimumOptional(): void
/**
* @infection-ignore-all
*/
private function assertSetArgument(string $name, mixed $argument, ?string $key = null): void
private function assertSetArgument(string $name, mixed $argument, null|int|string $key = null): void
{
$parameter = $this->parameters()->get($name);
$property = $name;
Expand Down Expand Up @@ -373,15 +373,20 @@ private function assertValues(): void
$lastPos = array_key_last($this->parameters()->keys());
foreach ($this->parameters()->keys() as $pos => $name) {
if ($pos === $lastPos && $this->parameters->isVariadic()) {
$variadicKeys = array_diff_key(
$this->arguments,
array_flip($this->parameters->keys())
);
foreach ($variadicKeys as $key => $value) {
$key = strval($key);

if ($this->isPositional) {
$variadicKeys = array_slice(
array_keys($this->arguments),
$pos
);
} else {
$variadicKeys = array_diff(
array_keys($this->arguments),
$this->parameters->keys()
);
}
foreach ($variadicKeys as $id) {
try {
$this->assertSetArgument($name, $value, $key);
$this->assertSetArgument($name, $this->arguments[$id], $id);
} catch (Throwable $e) {
$this->errors[] = $e->getMessage();
}
Expand Down
5 changes: 1 addition & 4 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,8 @@ public function testValidatedReturnError(): void
public function testValidatedFunction(): void
{
$function = 'Chevere\Tests\src\validates';
$base = 100;
$times = 1;
$name = 'Test';
$this->expectNotToPerformAssertions();
validated($function, $base, $times, $name);
validated($function, 100, 1, 'Test'); // base, times, name
}

#[DataProvider('dataProviderValMd')]
Expand Down

0 comments on commit 3af8b21

Please sign in to comment.