@@ -103,7 +103,7 @@ protected function processValue($value, bool $isRoot = false)
103
103
private function doProcessValue ($ value , bool $ isRoot = false )
104
104
{
105
105
if ($ value instanceof TypedReference) {
106
- if ($ ref = $ this ->getAutowiredReference ($ value )) {
106
+ if ($ ref = $ this ->getAutowiredReference ($ value, true )) {
107
107
return $ ref ;
108
108
}
109
109
if (ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $ value ->getInvalidBehavior ()) {
@@ -294,7 +294,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
294
294
}
295
295
296
296
$ getValue = function () use ($ type , $ parameter , $ class , $ method ) {
297
- if (!$ value = $ this ->getAutowiredReference ($ ref = new TypedReference ($ type , $ type , ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE , Target::parseName ($ parameter )))) {
297
+ if (!$ value = $ this ->getAutowiredReference ($ ref = new TypedReference ($ type , $ type , ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE , Target::parseName ($ parameter )), true )) {
298
298
$ failureMessage = $ this ->createTypeNotFoundMessageCallback ($ ref , sprintf ('argument "$%s" of method "%s()" ' , $ parameter ->name , $ class !== $ this ->currentId ? $ class .':: ' .$ method : $ method ));
299
299
300
300
if ($ parameter ->isDefaultValueAvailable ()) {
@@ -349,7 +349,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
349
349
/**
350
350
* Returns a reference to the service matching the given type, if any.
351
351
*/
352
- private function getAutowiredReference (TypedReference $ reference ): ?TypedReference
352
+ private function getAutowiredReference (TypedReference $ reference, bool $ filterType ): ?TypedReference
353
353
{
354
354
$ this ->lastFailure = null ;
355
355
$ type = $ reference ->getType ();
@@ -358,6 +358,19 @@ private function getAutowiredReference(TypedReference $reference): ?TypedReferen
358
358
return $ reference ;
359
359
}
360
360
361
+ if ($ filterType && preg_match ('/([&|])/ ' , $ type , $ m )) {
362
+ $ types = explode ($ m [0 ], $ type );
363
+
364
+ if ('| ' === $ m [0 ]) {
365
+ // remove built-in types
366
+ $ types = array_filter ($ types , function ($ type ) { return !\in_array (strtolower ($ type ), ['int ' , 'string ' , 'array ' , 'bool ' , 'float ' , 'iterable ' , 'object ' , 'mixed ' , 'null ' ], true ); });
367
+ }
368
+
369
+ sort ($ types );
370
+
371
+ $ types = implode ($ m [0 ], $ types );
372
+ }
373
+
361
374
if (null !== $ name = $ reference ->getName ()) {
362
375
if ($ this ->container ->has ($ alias = $ type .' $ ' .$ name ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
363
376
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
0 commit comments