Skip to content

Commit 3124e70

Browse files
committed
Resolver: only optional parameters are autowired with default value (BC break) [WIP]
TODO: nullable unknown class
1 parent 1acc01a commit 3124e70

File tree

2 files changed

+5
-13
lines changed

2 files changed

+5
-13
lines changed

src/DI/Resolver.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -615,15 +615,7 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
615615
) {
616616
return $getter($itemType, false);
617617

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

629621
} else {

tests/DI/Resolver.autowireArguments.errors.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ Assert::error(function () {
6666

6767

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

7777

7878
// union
@@ -86,13 +86,13 @@ Assert::exception(function () {
8686

8787

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

9797

9898
// bad variadics (this is actually what PHP allows)

0 commit comments

Comments
 (0)