Skip to content

Commit

Permalink
Resolver: only optional parameters are autowired with default value (…
Browse files Browse the repository at this point in the history
…BC break) [WIP]

TODO: nullable unknown class
  • Loading branch information
dg committed Apr 20, 2022
1 parent 986f010 commit 7f04701
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
10 changes: 1 addition & 9 deletions src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,7 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
) {
return $getter($itemType, false);

} elseif (
($type && $parameter->allowsNull())
|| $parameter->isOptional()
|| $parameter->isDefaultValueAvailable()
) {
// !optional + defaultAvailable, !optional + !defaultAvailable since 8.1.0 = func($a = null, $b)
// optional + !defaultAvailable, optional + defaultAvailable since 8.0.0 = i.e. Exception::__construct, mysqli::mysqli, ...
// optional + !defaultAvailable = variadics
// in other cases the optional and defaultAvailable are identical
} elseif ($parameter->isOptional()) {
return null;

} else {
Expand Down
8 changes: 4 additions & 4 deletions tests/DI/Resolver.autowireArguments.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ Assert::error(function () {


// nullable scalar
Assert::error(function () {
Assert::exception(function () {
Resolver::autowireArguments(
new ReflectionFunction(function (?int $arg) {}),
[],
fn($type) => $type === Test::class ? new Test : null,
);
}, E_USER_DEPRECATED, 'The parameter $arg in {closure}() should have a declared value in the configuration.');
}, Nette\DI\ServiceCreationException::class, 'Parameter $arg in {closure}() has union type and no default value, so its value must be specified.');


// union
Expand All @@ -86,13 +86,13 @@ Assert::exception(function () {


// nullable union
Assert::error(function () {
Assert::exception(function () {
Resolver::autowireArguments(
new ReflectionFunction(function (stdClass|Test|null $x) {}),
[],
function () {},
);
}, E_USER_DEPRECATED, 'The parameter $x in {closure}() should have a declared value in the configuration.');
}, Nette\DI\ServiceCreationException::class, 'Parameter $x in {closure}() has union type and no default value, so its value must be specified.');


// bad variadics (this is actually what PHP allows)
Expand Down

0 comments on commit 7f04701

Please sign in to comment.