Skip to content

Commit 6043798

Browse files
committed
Resolver::autowireArguments() throws an exception when combining a named and positional parameter
1 parent 3c615cb commit 6043798

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

src/DI/Resolver.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,13 @@ public static function autowireArguments(
520520
$paramName = $param->name;
521521
if (!$param->isVariadic() && array_key_exists($paramName, $arguments)) {
522522
$res[$num] = $arguments[$paramName];
523-
unset($arguments[$paramName], $arguments[$num]);
523+
if (array_key_exists($num, $arguments)) {
524+
throw new ServiceCreationException(sprintf(
525+
'Named parameter $%s used at the same time as a positional in %s.',
526+
$paramName,
527+
Reflection::toString($method)
528+
));
529+
}
524530

525531
} elseif (array_key_exists($num, $arguments)) {
526532
$res[$num] = $arguments[$num];

tests/DI/Compiler.services.factory.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Assert::type(Ipsum::class, $container->getService('two'));
5858
Assert::same(1, $container->getService('two')->arg);
5959

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

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

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

8080
Assert::type(Ipsum::class, $container->getService('referencedService'));

tests/DI/Resolver.autowireArguments.errors.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ Assert::exception(function () {
3131
Assert::exception(function () {
3232
Resolver::autowireArguments(new ReflectionFunction(function (int $x) {}), [], function () {});
3333
}, Nette\DI\ServiceCreationException::class, 'Parameter $x in {closure}() has no class type or default value, so its value must be specified.');
34+
35+
36+
Assert::exception(function () {
37+
Resolver::autowireArguments(new ReflectionFunction(function (int $x) {}), [10, 'x' => 10], function () {});
38+
}, Nette\DI\ServiceCreationException::class, 'Named parameter $x used at the same time as a positional in {closure}%a?%.');
39+
40+
41+
Assert::exception(function () {
42+
Resolver::autowireArguments(new ReflectionFunction(function (...$args) {}), ['args' => []], function () {});
43+
}, Nette\DI\ServiceCreationException::class, 'Unable to pass specified arguments to {closure}%a?%.');

tests/DI/Resolver.autowireArguments.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ Assert::equal(
3838
return $type === Test::class ? new Test : null;
3939
})
4040
);
41+
42+
43+
// variadics
44+
Assert::equal(
45+
[],
46+
Resolver::autowireArguments(new ReflectionFunction(function (...$args) {}), [], function () {})
47+
);
48+
49+
Assert::equal(
50+
[1, 2, 3],
51+
Resolver::autowireArguments(new ReflectionFunction(function (...$args) {}), [1, 2, 3], function () {})
52+
);

tests/DI/files/compiler.services.factory.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ services:
2525
three:
2626
type: Lorem
2727
factory: Factory::createLorem
28-
arguments: [arg: 5, 1, 2]
28+
arguments: [1, 2]
2929

3030
four:
3131
factory: Factory::createLorem(1)

0 commit comments

Comments
 (0)