From 2deec22f7d5f5df5c5f4da30a5d8b6b6d6ef4d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sat, 23 Sep 2023 01:56:55 +0200 Subject: [PATCH] Support list syntax for autowiring a collection of services (#293) And for this usage and this case it's basically the same thing as Type[] and as array, both supported. --- src/DI/Resolver.php | 2 +- tests/DI/ContainerBuilder.autowiring.type[].phpt | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/DI/Resolver.php b/src/DI/Resolver.php index 6db64d5f8..15ec7db1a 100644 --- a/src/DI/Resolver.php +++ b/src/DI/Resolver.php @@ -690,7 +690,7 @@ private static function isArrayOf(\ReflectionParameter $parameter, ?Nette\Utils\ && $type && $type->getSingleName() === 'array' && preg_match( - '#@param[ \t]+(?|([\w\\\\]+)\[\]|array)[ \t]+\$' . $parameter->name . '#', + '#@param[ \t]+(?|([\w\\\\]+)\[\]|list<([\w\\\\]+)>|array)[ \t]+\$' . $parameter->name . '#', (string) $method->getDocComment(), $m ) diff --git a/tests/DI/ContainerBuilder.autowiring.type[].phpt b/tests/DI/ContainerBuilder.autowiring.type[].phpt index 4f3f595b6..351b370a7 100644 --- a/tests/DI/ContainerBuilder.autowiring.type[].phpt +++ b/tests/DI/ContainerBuilder.autowiring.type[].phpt @@ -16,18 +16,21 @@ require __DIR__ . '/../bootstrap.php'; class Foo { public $bars; + public $waldos; public $foos; public $strings; /** * @param Service[] $bars + * @param list $waldos * @param array $foos * @param string[] $strings */ - public function __construct(array $bars = [], ?array $foos = null, array $strings = ['default']) + public function __construct(array $bars = [], array $waldos = [], ?array $foos = null, array $strings = ['default']) { $this->bars = $bars; + $this->waldos = $waldos; $this->foos = $foos; $this->strings = $strings; } @@ -67,6 +70,11 @@ Assert::same([ $container->getService('s2'), $container->getService('s3'), ], $foo->bars); +Assert::same([ + $container->getService('s1'), + $container->getService('s2'), + $container->getService('s3'), +], $foo->waldos); Assert::same([], $foo->foos); Assert::same(['default'], $foo->strings); @@ -80,5 +88,10 @@ Assert::same([ $container->getService('s2'), $container->getService('s3'), ], $foo2->bars); +Assert::same([ + $container->getService('s1'), + $container->getService('s2'), + $container->getService('s3'), +], $foo2->waldos); Assert::same([$foo], $foo2->foos); // difference Assert::same(['default'], $foo2->strings);