Skip to content

Commit

Permalink
[bug] fix optional parameter issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Jan 9, 2021
1 parent 6934887 commit 82cd9ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function invoke(...$arguments)
continue;
}

if (!\array_key_exists($key, $parameters) && !$argument->isOptional()) {
throw new \ArgumentCountError(\sprintf('No argument %d for callable. Expected type: "%s".', $key + 1, $argument->type()));
}
if (!\array_key_exists($key, $parameters)) {
if (!$argument->isOptional()) {
throw new \ArgumentCountError(\sprintf('No argument %d for callable. Expected type: "%s".', $key + 1, $argument->type()));
}

if ($argument->isOptional()) {
$arguments[$key] = null;

continue;
Expand Down
6 changes: 6 additions & 0 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ public function can_mark_invoke_parameter_arguments_as_optional(): void
;

$this->assertSame('ret', $actual);

$actual = Callback::createFor(static function(string $v) { return $v; })
->invoke(Parameter::typed('string', 'foobar')->optional())
;

$this->assertSame('foobar', $actual);
}
}

Expand Down

0 comments on commit 82cd9ac

Please sign in to comment.