Skip to content

Commit

Permalink
Resolver::autowireArgument() fixed handling of new-initializer with t…
Browse files Browse the repository at this point in the history
…ypehint [Closes #302]
  • Loading branch information
dg committed Dec 14, 2023
1 parent d2ad016 commit 6c422d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
throw new ServiceCreationException("{$e->getMessage()} (required by $desc)", 0, $e);
}

if ($res !== null || $parameter->allowsNull()) {
if ($res !== null || $parameter->allowsNull() || $parameter->isOptional()) {
return $res;
} elseif (class_exists($class) || interface_exists($class)) {
throw new ServiceCreationException(sprintf(
Expand Down
10 changes: 10 additions & 0 deletions tests/DI/Resolver.autowireArguments.81.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ Assert::same(
function () {}
),
);

// object as default with typehint
Assert::same(
['b' => 10],
Resolver::autowireArguments(
new ReflectionFunction(function (stdClass $a = new stdClass, $b = null) {}),
[1 => 10],
function () {}
),
);

0 comments on commit 6c422d0

Please sign in to comment.