Skip to content

Commit

Permalink
Merge pull request #30 from PHP-DI/optional-parameters
Browse files Browse the repository at this point in the history
Support optional parameters before required ones
  • Loading branch information
mnapoli authored Oct 12, 2020
2 parents f33afc3 + c57a43c commit e08a7c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ParameterResolver/DefaultValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getParameters(

foreach ($parameters as $index => $parameter) {
\assert($parameter instanceof \ReflectionParameter);
if ($parameter->isOptional()) {
if ($parameter->isDefaultValueAvailable()) {
try {
$resolvedParameters[$index] = $parameter->getDefaultValue();
} catch (ReflectionException $e) {
Expand Down
16 changes: 16 additions & 0 deletions tests/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public function should_invoke_callable_with_null_for_nullable_parameters()
}

/**
* @see https://github.com/PHP-DI/Slim-Bridge/issues/37
* @test
*/
public function should_invoke_callable_with_null_for_non_optional_nullable_parameters()
Expand All @@ -203,6 +204,21 @@ public function should_invoke_callable_with_null_for_non_optional_nullable_param
$this->assertNull($result);
}

/**
* @see https://github.com/PHP-DI/PHP-DI/issues/562
* @test
*/
public function should_invoke_callable_with_optional_parameter_before_required_parameter()
{
$result = $this->invoker->call(function ($baz = 'abc', $foo) {
return [$baz, $foo];
}, [
'foo' => 'bar',
]);

$this->assertSame(['abc', 'bar'], $result);
}

/**
* @test
*/
Expand Down

0 comments on commit e08a7c8

Please sign in to comment.