@@ -561,53 +561,52 @@ public static function autowireArguments(
561561 */
562562 private static function autowireArgument (\ReflectionParameter $ parameter , callable $ getter )
563563 {
564+ $ method = $ parameter ->getDeclaringFunction ();
564565 $ desc = Reflection::toString ($ parameter );
565- if ($ parameter ->getType () instanceof \ReflectionIntersectionType) {
566+ $ type = Nette \Utils \Type::fromReflection ($ parameter );
567+
568+ if ($ type && $ type ->isIntersection ()) {
566569 throw new ServiceCreationException (sprintf (
567570 'Parameter %s has intersection type, so its value must be specified. ' ,
568571 $ desc
569572 ));
570- }
571-
572- $ types = array_diff (Reflection::getParameterTypes ($ parameter ), ['null ' ]);
573- $ type = count ($ types ) === 1 ? reset ($ types ) : null ;
574- $ method = $ parameter ->getDeclaringFunction ();
575573
576- if ($ type && !Reflection::isBuiltinType ($ type )) {
574+ } elseif ($ type && $ type ->isClass ()) {
575+ $ class = $ type ->getSingleName ();
577576 try {
578- $ res = $ getter ($ type , true );
577+ $ res = $ getter ($ class , true );
579578 } catch (MissingServiceException $ e ) {
580579 $ res = null ;
581580 } catch (ServiceCreationException $ e ) {
582581 throw new ServiceCreationException ("{$ e ->getMessage ()} (required by $ desc) " , 0 , $ e );
583582 }
584583 if ($ res !== null || $ parameter ->allowsNull ()) {
585584 return $ res ;
586- } elseif (class_exists ($ type ) || interface_exists ($ type )) {
585+ } elseif (class_exists ($ class ) || interface_exists ($ class )) {
587586 throw new ServiceCreationException (sprintf (
588587 'Service of type %s required by %s not found. Did you add it to configuration file? ' ,
589- $ type ,
588+ $ class ,
590589 $ desc
591590 ));
592591 } else {
593592 throw new ServiceCreationException (sprintf (
594593 "Class '%s' required by %s not found. Check the parameter type and 'use' statements. " ,
595- $ type ,
594+ $ class ,
596595 $ desc
597596 ));
598597 }
599598
600599 } elseif (
601600 $ method instanceof \ReflectionMethod
602- && $ type === 'array '
601+ && $ type && $ type -> getSingleName () === 'array '
603602 && preg_match ('#@param[ \t]+([\w \\\\]+)\[\][ \t]+\$ ' . $ parameter ->name . '# ' , (string ) $ method ->getDocComment (), $ m )
604603 && ($ itemType = Reflection::expandClassName ($ m [1 ], $ method ->getDeclaringClass ()))
605604 && (class_exists ($ itemType ) || interface_exists ($ itemType ))
606605 ) {
607606 return $ getter ($ itemType , false );
608607
609608 } elseif (
610- ($ types && $ parameter ->allowsNull ())
609+ ($ type && $ parameter ->allowsNull ())
611610 || $ parameter ->isOptional ()
612611 || $ parameter ->isDefaultValueAvailable ()
613612 ) {
@@ -621,7 +620,7 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
621620 throw new ServiceCreationException (sprintf (
622621 'Parameter %s has %s, so its value must be specified. ' ,
623622 $ desc ,
624- count ( $ types ) > 1 ? 'union type and no default value ' : 'no class type or default value '
623+ $ type && $ type -> isUnion () ? 'union type and no default value ' : 'no class type or default value '
625624 ));
626625 }
627626 }
0 commit comments