Skip to content

Commit

Permalink
Resolver::autowireArguments() throws an exception when combining a na…
Browse files Browse the repository at this point in the history
…med and positional parameter
  • Loading branch information
dg committed Sep 29, 2021
1 parent 3c615cb commit 4f0cb0b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,15 @@ public static function autowireArguments(
foreach ($method->getParameters() as $num => $param) {
$paramName = $param->name;
if (!$param->isVariadic() && array_key_exists($paramName, $arguments)) {
if (array_key_exists($num, $arguments)) {
throw new ServiceCreationException(sprintf(
'Named parameter $%s used at the same time as a positional in %s.',
$paramName,
Reflection::toString($method)
));
}
$res[$num] = $arguments[$paramName];
unset($arguments[$paramName], $arguments[$num]);
unset($arguments[$paramName]);

} elseif (array_key_exists($num, $arguments)) {
$res[$num] = $arguments[$num];
Expand Down
4 changes: 2 additions & 2 deletions tests/DI/Compiler.services.factory.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Assert::type(Ipsum::class, $container->getService('two'));
Assert::same(1, $container->getService('two')->arg);

Assert::type(Lorem::class, $container->getService('three'));
Assert::same('Factory::createLorem 5 2', $container->getService('three')->arg);
Assert::same('Factory::createLorem 1 2', $container->getService('three')->arg);

Assert::type(Lorem::class, $container->getService('four'));
Assert::same('Factory::createLorem 1', $container->getService('four')->arg);
Expand All @@ -74,7 +74,7 @@ Assert::type(Lorem::class, $container->getService('seven'));
Assert::type(Lorem::class, $container->getService('eight'));

Assert::type(Lorem::class, $container->getService('nine'));
Assert::same('Factory::createLorem 5 2', $container->getService('nine')->arg);
Assert::same('Factory::createLorem 1 2', $container->getService('nine')->arg);
Assert::same([], $container->getService('nine')->foo);

Assert::type(Ipsum::class, $container->getService('referencedService'));
Expand Down
10 changes: 10 additions & 0 deletions tests/DI/Resolver.autowireArguments.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ Assert::exception(function () {
Assert::exception(function () {
Resolver::autowireArguments(new ReflectionFunction(function (int $x) {}), [], function () {});
}, Nette\DI\ServiceCreationException::class, 'Parameter $x in {closure}() has no class type or default value, so its value must be specified.');


Assert::exception(function () {
Resolver::autowireArguments(new ReflectionFunction(function (int $x) {}), [10, 'x' => 10], function () {});
}, Nette\DI\ServiceCreationException::class, 'Named parameter $x used at the same time as a positional in {closure}%a?%.');


Assert::exception(function () {
Resolver::autowireArguments(new ReflectionFunction(function (...$args) {}), ['args' => []], function () {});
}, Nette\DI\ServiceCreationException::class, 'Unable to pass specified arguments to {closure}%a?%.');
12 changes: 12 additions & 0 deletions tests/DI/Resolver.autowireArguments.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ Assert::equal(
return $type === Test::class ? new Test : null;
})
);


// variadics
Assert::equal(
[],
Resolver::autowireArguments(new ReflectionFunction(function (...$args) {}), [], function () {})
);

Assert::equal(
[1, 2, 3],
Resolver::autowireArguments(new ReflectionFunction(function (...$args) {}), [1, 2, 3], function () {})
);
2 changes: 1 addition & 1 deletion tests/DI/files/compiler.services.factory.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ services:
three:
type: Lorem
factory: Factory::createLorem
arguments: [arg: 5, 1, 2]
arguments: [1, 2]

four:
factory: Factory::createLorem(1)
Expand Down

0 comments on commit 4f0cb0b

Please sign in to comment.