Skip to content

Commit

Permalink
[bug] allow default parameter values
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Jul 19, 2021
1 parent c9f7823 commit fef4acf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Callback/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ public function isUnionType(): bool
return \count($this->types()) > 1;
}

public function isOptional(): bool
{
return $this->parameter->isOptional();
}

/**
* @return mixed
*/
public function defaultValue()
{
return $this->parameter->getDefaultValue();
}

/**
* @param string $type The type to check if this argument supports
* @param int $options {@see EXACT}, {@see COVARIANCE}, {@see CONTRAVARIANCE}
Expand Down
10 changes: 9 additions & 1 deletion src/Callback/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ final public function optional(): self
*/
final public function resolve(Argument $argument)
{
$value = $this->valueFor($argument);
try {
$value = $this->valueFor($argument);
} catch (UnresolveableArgument $e) {
if ($argument->isOptional()) {
return $argument->defaultValue();
}

throw $e;
}

if ($value instanceof ValueFactory) {
$value = $value($argument);
Expand Down

0 comments on commit fef4acf

Please sign in to comment.