Skip to content

Commit c471896

Browse files
committed
Resolver: only optional parameters are autowired with default value (BC break)
1 parent 0944550 commit c471896

File tree

2 files changed

+7
-28
lines changed

2 files changed

+7
-28
lines changed

src/DI/Resolver.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,7 @@ public static function autowireArguments(
498498
$res[$num] = self::autowireArgument($param, $getter);
499499
}
500500

501-
$optCount += $param->isOptional() && $res[$num] === ($param->isDefaultValueAvailable() ? Reflection::getParameterDefaultValue($param) : null)
502-
? 1
503-
: 0;
501+
$optCount += $param->isOptional() && $res[$num] === Reflection::getParameterDefaultValue($param) ? 1 : 0;
504502
}
505503

506504
// extra parameters
@@ -573,27 +571,8 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
573571
) {
574572
return $getter($itemType, false);
575573

576-
} elseif (
577-
($type && $parameter->allowsNull())
578-
|| $parameter->isOptional()
579-
|| $parameter->isDefaultValueAvailable()
580-
) {
581-
// !optional + defaultAvailable, !optional + !defaultAvailable since 8.1.0 = func($a = null, $b)
582-
// optional + !defaultAvailable, optional + defaultAvailable since 8.0.0 = i.e. Exception::__construct, mysqli::mysqli, ...
583-
// optional + !defaultAvailable = variadics
584-
// in other cases the optional and defaultAvailable are identical
585-
$default = $parameter->isDefaultValueAvailable()
586-
? Reflection::getParameterDefaultValue($parameter)
587-
: null;
588-
589-
if (!$parameter->isOptional()) {
590-
trigger_error(sprintf(
591-
'The parameter %s should have a declared value in the configuration. Value %s is currently used.',
592-
$desc,
593-
var_export($default, true)
594-
), E_USER_DEPRECATED);
595-
}
596-
return $default;
574+
} elseif ($parameter->isOptional()) {
575+
return Reflection::getParameterDefaultValue($parameter);
597576

598577
} else {
599578
throw new ServiceCreationException(sprintf(

tests/DI/Resolver.autowireArguments.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ Assert::equal(
4545
Resolver::autowireArguments(new ReflectionMethod(Test::class, 'method'), [], fn($type) => $type === Test::class ? new Test : null),
4646
);
4747

48-
Assert::error(function () {
48+
Assert::exception(function () {
4949
Resolver::autowireArguments(new ReflectionMethod(Test::class, 'methodNullable'), [], fn($type) => $type === Test::class ? new Test : null);
50-
}, E_USER_DEPRECATED, 'The parameter $nullable2 in Test::methodNullable() should have a declared value in the configuration. Value NULL is currently used.');
50+
}, Nette\DI\ServiceCreationException::class, 'Parameter $nullable2 in Test::methodNullable() has union type and no default value, so its value must be specified.');
5151

5252

5353
Assert::exception(function () {
5454
Resolver::autowireArguments(new ReflectionMethod('Test', 'methodUnion'), [], function () {});
5555
}, Nette\InvalidStateException::class, 'Parameter $self in Test::methodUnion() has union type and no default value, so its value must be specified.');
5656

57-
Assert::error(function () {
57+
Assert::exception(function () {
5858
Resolver::autowireArguments(new ReflectionMethod(Test::class, 'methodUnionNullable'), [], function () {});
59-
}, E_USER_DEPRECATED, 'The parameter $nullable in Test::methodUnionNullable() should have a declared value in the configuration. Value NULL is currently used.');
59+
}, Nette\DI\ServiceCreationException::class, 'Parameter $nullable in Test::methodUnionNullable() has union type and no default value, so its value must be specified.');
6060

6161
Assert::same(
6262
[],

0 commit comments

Comments
 (0)