Skip to content

Commit e08a7c8

Browse files
authored
Merge pull request #30 from PHP-DI/optional-parameters
Support optional parameters before required ones
2 parents f33afc3 + c57a43c commit e08a7c8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ParameterResolver/DefaultValueResolver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getParameters(
2424

2525
foreach ($parameters as $index => $parameter) {
2626
\assert($parameter instanceof \ReflectionParameter);
27-
if ($parameter->isOptional()) {
27+
if ($parameter->isDefaultValueAvailable()) {
2828
try {
2929
$resolvedParameters[$index] = $parameter->getDefaultValue();
3030
} catch (ReflectionException $e) {

tests/InvokerTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public function should_invoke_callable_with_null_for_nullable_parameters()
192192
}
193193

194194
/**
195+
* @see https://github.com/PHP-DI/Slim-Bridge/issues/37
195196
* @test
196197
*/
197198
public function should_invoke_callable_with_null_for_non_optional_nullable_parameters()
@@ -203,6 +204,21 @@ public function should_invoke_callable_with_null_for_non_optional_nullable_param
203204
$this->assertNull($result);
204205
}
205206

207+
/**
208+
* @see https://github.com/PHP-DI/PHP-DI/issues/562
209+
* @test
210+
*/
211+
public function should_invoke_callable_with_optional_parameter_before_required_parameter()
212+
{
213+
$result = $this->invoker->call(function ($baz = 'abc', $foo) {
214+
return [$baz, $foo];
215+
}, [
216+
'foo' => 'bar',
217+
]);
218+
219+
$this->assertSame(['abc', 'bar'], $result);
220+
}
221+
206222
/**
207223
* @test
208224
*/

0 commit comments

Comments
 (0)