vendor/symfony/dependency-injection/Compiler/AutowirePass.php line 617

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\Config\Resource\ClassExistenceResource;
  12. use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
  13. use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
  14. use Symfony\Component\DependencyInjection\Attribute\Autowire;
  15. use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
  16. use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
  17. use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
  18. use Symfony\Component\DependencyInjection\Attribute\Target;
  19. use Symfony\Component\DependencyInjection\ContainerBuilder;
  20. use Symfony\Component\DependencyInjection\ContainerInterface;
  21. use Symfony\Component\DependencyInjection\Definition;
  22. use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
  23. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  24. use Symfony\Component\DependencyInjection\Reference;
  25. use Symfony\Component\DependencyInjection\TypedReference;
  26. use Symfony\Component\VarExporter\ProxyHelper;
  27. use Symfony\Contracts\Service\Attribute\SubscribedService;
  28. /**
  29.  * Inspects existing service definitions and wires the autowired ones using the type hints of their classes.
  30.  *
  31.  * @author Kévin Dunglas <dunglas@gmail.com>
  32.  * @author Nicolas Grekas <p@tchwork.com>
  33.  */
  34. class AutowirePass extends AbstractRecursivePass
  35. {
  36.     private array $types;
  37.     private array $ambiguousServiceTypes;
  38.     private array $autowiringAliases;
  39.     private ?string $lastFailure null;
  40.     private bool $throwOnAutowiringException;
  41.     private ?string $decoratedClass null;
  42.     private ?string $decoratedId null;
  43.     private ?array $methodCalls null;
  44.     private object $defaultArgument;
  45.     private ?\Closure $getPreviousValue null;
  46.     private ?int $decoratedMethodIndex null;
  47.     private ?int $decoratedMethodArgumentIndex null;
  48.     private ?self $typesClone null;
  49.     public function __construct(bool $throwOnAutowireException true)
  50.     {
  51.         $this->throwOnAutowiringException $throwOnAutowireException;
  52.         $this->defaultArgument = new class() {
  53.             public $value;
  54.             public $names;
  55.         };
  56.     }
  57.     public function process(ContainerBuilder $container)
  58.     {
  59.         try {
  60.             $this->typesClone = clone $this;
  61.             parent::process($container);
  62.         } finally {
  63.             $this->decoratedClass null;
  64.             $this->decoratedId null;
  65.             $this->methodCalls null;
  66.             $this->defaultArgument->names null;
  67.             $this->getPreviousValue null;
  68.             $this->decoratedMethodIndex null;
  69.             $this->decoratedMethodArgumentIndex null;
  70.             $this->typesClone null;
  71.         }
  72.     }
  73.     protected function processValue(mixed $valuebool $isRoot false): mixed
  74.     {
  75.         try {
  76.             return $this->doProcessValue($value$isRoot);
  77.         } catch (AutowiringFailedException $e) {
  78.             if ($this->throwOnAutowiringException) {
  79.                 throw $e;
  80.             }
  81.             $this->container->getDefinition($this->currentId)->addError($e->getMessageCallback() ?? $e->getMessage());
  82.             return parent::processValue($value$isRoot);
  83.         }
  84.     }
  85.     private function doProcessValue(mixed $valuebool $isRoot false): mixed
  86.     {
  87.         if ($value instanceof TypedReference) {
  88.             if ($attributes $value->getAttributes()) {
  89.                 $attribute array_pop($attributes);
  90.                 if ($attributes) {
  91.                     throw new AutowiringFailedException($this->currentIdsprintf('Using multiple attributes with "%s" is not supported.'SubscribedService::class));
  92.                 }
  93.                 if (!$attribute instanceof Target) {
  94.                     return $this->processAttribute($attributeContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $value->getInvalidBehavior());
  95.                 }
  96.                 $value = new TypedReference($value->getType(), $value->getType(), $value->getInvalidBehavior(), $attribute->name);
  97.             }
  98.             if ($ref $this->getAutowiredReference($valuetrue)) {
  99.                 return $ref;
  100.             }
  101.             if (ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $value->getInvalidBehavior()) {
  102.                 $message $this->createTypeNotFoundMessageCallback($value'it');
  103.                 // since the error message varies by referenced id and $this->currentId, so should the id of the dummy errored definition
  104.                 $this->container->register($id sprintf('.errored.%s.%s'$this->currentId, (string) $value), $value->getType())
  105.                     ->addError($message);
  106.                 return new TypedReference($id$value->getType(), $value->getInvalidBehavior(), $value->getName());
  107.             }
  108.         }
  109.         $value parent::processValue($value$isRoot);
  110.         if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
  111.             return $value;
  112.         }
  113.         if (!$reflectionClass $this->container->getReflectionClass($value->getClass(), false)) {
  114.             $this->container->log($thissprintf('Skipping service "%s": Class or interface "%s" cannot be loaded.'$this->currentId$value->getClass()));
  115.             return $value;
  116.         }
  117.         $this->methodCalls $value->getMethodCalls();
  118.         try {
  119.             $constructor $this->getConstructor($valuefalse);
  120.         } catch (RuntimeException $e) {
  121.             throw new AutowiringFailedException($this->currentId$e->getMessage(), 0$e);
  122.         }
  123.         if ($constructor) {
  124.             array_unshift($this->methodCalls, [$constructor$value->getArguments()]);
  125.         }
  126.         $checkAttributes = !$value->hasTag('container.ignore_attributes');
  127.         $this->methodCalls $this->autowireCalls($reflectionClass$isRoot$checkAttributes);
  128.         if ($constructor) {
  129.             [, $arguments] = array_shift($this->methodCalls);
  130.             if ($arguments !== $value->getArguments()) {
  131.                 $value->setArguments($arguments);
  132.             }
  133.         }
  134.         if ($this->methodCalls !== $value->getMethodCalls()) {
  135.             $value->setMethodCalls($this->methodCalls);
  136.         }
  137.         return $value;
  138.     }
  139.     private function processAttribute(object $attributebool $isOptional false): mixed
  140.     {
  141.         switch (true) {
  142.             case $attribute instanceof Autowire:
  143.                 $value $this->container->getParameterBag()->resolveValue($attribute->value);
  144.                 return $value instanceof Reference && $isOptional ? new Reference($valueContainerInterface::NULL_ON_INVALID_REFERENCE) : $value;
  145.             case $attribute instanceof TaggedIterator:
  146.                 return new TaggedIteratorArgument($attribute->tag$attribute->indexAttribute$attribute->defaultIndexMethodfalse$attribute->defaultPriorityMethod, (array) $attribute->exclude);
  147.             case $attribute instanceof TaggedLocator:
  148.                 return new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag$attribute->indexAttribute$attribute->defaultIndexMethodtrue$attribute->defaultPriorityMethod, (array) $attribute->exclude));
  149.             case $attribute instanceof MapDecorated:
  150.                 $definition $this->container->getDefinition($this->currentId);
  151.                 return new Reference($definition->innerServiceId ?? $this->currentId.'.inner'$definition->decorationOnInvalid ?? ContainerInterface::NULL_ON_INVALID_REFERENCE);
  152.         }
  153.         throw new AutowiringFailedException($this->currentIdsprintf('"%s" is an unsupported attribute.'$attribute::class));
  154.     }
  155.     private function autowireCalls(\ReflectionClass $reflectionClassbool $isRootbool $checkAttributes): array
  156.     {
  157.         $this->decoratedId null;
  158.         $this->decoratedClass null;
  159.         $this->getPreviousValue null;
  160.         if ($isRoot && ($definition $this->container->getDefinition($this->currentId)) && null !== ($this->decoratedId $definition->innerServiceId) && $this->container->has($this->decoratedId)) {
  161.             $this->decoratedClass $this->container->findDefinition($this->decoratedId)->getClass();
  162.         }
  163.         $patchedIndexes = [];
  164.         foreach ($this->methodCalls as $i => $call) {
  165.             [$method$arguments] = $call;
  166.             if ($method instanceof \ReflectionFunctionAbstract) {
  167.                 $reflectionMethod $method;
  168.             } else {
  169.                 $definition = new Definition($reflectionClass->name);
  170.                 try {
  171.                     $reflectionMethod $this->getReflectionMethod($definition$method);
  172.                 } catch (RuntimeException $e) {
  173.                     if ($definition->getFactory()) {
  174.                         continue;
  175.                     }
  176.                     throw $e;
  177.                 }
  178.             }
  179.             $arguments $this->autowireMethod($reflectionMethod$arguments$checkAttributes$i);
  180.             if ($arguments !== $call[1]) {
  181.                 $this->methodCalls[$i][1] = $arguments;
  182.                 $patchedIndexes[] = $i;
  183.             }
  184.         }
  185.         // use named arguments to skip complex default values
  186.         foreach ($patchedIndexes as $i) {
  187.             $namedArguments null;
  188.             $arguments $this->methodCalls[$i][1];
  189.             foreach ($arguments as $j => $value) {
  190.                 if ($namedArguments && !$value instanceof $this->defaultArgument) {
  191.                     unset($arguments[$j]);
  192.                     $arguments[$namedArguments[$j]] = $value;
  193.                 }
  194.                 if ($namedArguments || !$value instanceof $this->defaultArgument) {
  195.                     continue;
  196.                 }
  197.                 if (\is_array($value->value) ? $value->value \is_object($value->value)) {
  198.                     unset($arguments[$j]);
  199.                     $namedArguments $value->names;
  200.                 } else {
  201.                     $arguments[$j] = $value->value;
  202.                 }
  203.             }
  204.             $this->methodCalls[$i][1] = $arguments;
  205.         }
  206.         return $this->methodCalls;
  207.     }
  208.     /**
  209.      * Autowires the constructor or a method.
  210.      *
  211.      * @throws AutowiringFailedException
  212.      */
  213.     private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, array $argumentsbool $checkAttributesint $methodIndex): array
  214.     {
  215.         $class $reflectionMethod instanceof \ReflectionMethod $reflectionMethod->class $this->currentId;
  216.         $method $reflectionMethod->name;
  217.         $parameters $reflectionMethod->getParameters();
  218.         if ($reflectionMethod->isVariadic()) {
  219.             array_pop($parameters);
  220.         }
  221.         $this->defaultArgument->names = new \ArrayObject();
  222.         foreach ($parameters as $index => $parameter) {
  223.             $this->defaultArgument->names[$index] = $parameter->name;
  224.             if (\array_key_exists($parameter->name$arguments)) {
  225.                 $arguments[$index] = $arguments[$parameter->name];
  226.                 unset($arguments[$parameter->name]);
  227.             }
  228.             if (\array_key_exists($index$arguments) && '' !== $arguments[$index]) {
  229.                 continue;
  230.             }
  231.             if ($checkAttributes) {
  232.                 foreach ($parameter->getAttributes() as $attribute) {
  233.                     if (\in_array($attribute->getName(), [TaggedIterator::class, TaggedLocator::class, Autowire::class, MapDecorated::class], true)) {
  234.                         $arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull());
  235.                         continue 2;
  236.                     }
  237.                 }
  238.             }
  239.             if (!$type ProxyHelper::exportType($parametertrue)) {
  240.                 if (isset($arguments[$index])) {
  241.                     continue;
  242.                 }
  243.                 // no default value? Then fail
  244.                 if (!$parameter->isDefaultValueAvailable()) {
  245.                     // For core classes, isDefaultValueAvailable() can
  246.                     // be false when isOptional() returns true. If the
  247.                     // argument *is* optional, allow it to be missing
  248.                     if ($parameter->isOptional()) {
  249.                         --$index;
  250.                         break;
  251.                     }
  252.                     $type ProxyHelper::exportType($parameter);
  253.                     $type $type sprintf('is type-hinted "%s"'preg_replace('/(^|[(|&])\\\\|^\?\\\\?/''\1'$type)) : 'has no type-hint';
  254.                     throw new AutowiringFailedException($this->currentIdsprintf('Cannot autowire service "%s": argument "$%s" of method "%s()" %s, you should configure its value explicitly.'$this->currentId$parameter->name$class !== $this->currentId $class.'::'.$method $method$type));
  255.                 }
  256.                 // specifically pass the default value
  257.                 $arguments[$index] = clone $this->defaultArgument;
  258.                 $arguments[$index]->value $parameter->getDefaultValue();
  259.                 continue;
  260.             }
  261.             $getValue = function () use ($type$parameter$class$method) {
  262.                 if (!$value $this->getAutowiredReference($ref = new TypedReference($type$typeContainerBuilder::EXCEPTION_ON_INVALID_REFERENCETarget::parseName($parameter)), false)) {
  263.                     $failureMessage $this->createTypeNotFoundMessageCallback($refsprintf('argument "$%s" of method "%s()"'$parameter->name$class !== $this->currentId $class.'::'.$method $method));
  264.                     if ($parameter->isDefaultValueAvailable()) {
  265.                         $value = clone $this->defaultArgument;
  266.                         $value->value $parameter->getDefaultValue();
  267.                     } elseif (!$parameter->allowsNull()) {
  268.                         throw new AutowiringFailedException($this->currentId$failureMessage);
  269.                     }
  270.                 }
  271.                 return $value;
  272.             };
  273.             if ($this->decoratedClass && $isDecorated is_a($this->decoratedClass$typetrue)) {
  274.                 if ($this->getPreviousValue) {
  275.                     // The inner service is injected only if there is only 1 argument matching the type of the decorated class
  276.                     // across all arguments of all autowired methods.
  277.                     // If a second matching argument is found, the default behavior is restored.
  278.                     $getPreviousValue $this->getPreviousValue;
  279.                     $this->methodCalls[$this->decoratedMethodIndex][1][$this->decoratedMethodArgumentIndex] = $getPreviousValue();
  280.                     $this->decoratedClass null// Prevent further checks
  281.                 } else {
  282.                     $arguments[$index] = new TypedReference($this->decoratedId$this->decoratedClass);
  283.                     $this->getPreviousValue $getValue;
  284.                     $this->decoratedMethodIndex $methodIndex;
  285.                     $this->decoratedMethodArgumentIndex $index;
  286.                     continue;
  287.                 }
  288.             }
  289.             $arguments[$index] = $getValue();
  290.         }
  291.         if ($parameters && !isset($arguments[++$index])) {
  292.             while (<= --$index) {
  293.                 if (!$arguments[$index] instanceof $this->defaultArgument) {
  294.                     break;
  295.                 }
  296.                 unset($arguments[$index]);
  297.             }
  298.         }
  299.         // it's possible index 1 was set, then index 0, then 2, etc
  300.         // make sure that we re-order so they're injected as expected
  301.         ksort($arguments\SORT_NATURAL);
  302.         return $arguments;
  303.     }
  304.     /**
  305.      * Returns a reference to the service matching the given type, if any.
  306.      */
  307.     private function getAutowiredReference(TypedReference $referencebool $filterType): ?TypedReference
  308.     {
  309.         $this->lastFailure null;
  310.         $type $reference->getType();
  311.         if ($type !== (string) $reference) {
  312.             return $reference;
  313.         }
  314.         if ($filterType && false !== $m strpbrk($type'&|')) {
  315.             $types array_diff(explode($m[0], $type), ['int''string''array''bool''float''iterable''object''callable''null']);
  316.             sort($types);
  317.             $type implode($m[0], $types);
  318.         }
  319.         if (null !== $name $reference->getName()) {
  320.             if ($this->container->has($alias $type.' $'.$name) && !$this->container->findDefinition($alias)->isAbstract()) {
  321.                 return new TypedReference($alias$type$reference->getInvalidBehavior());
  322.             }
  323.             if (null !== ($alias $this->getCombinedAlias($type$name) ?? null) && !$this->container->findDefinition($alias)->isAbstract()) {
  324.                 return new TypedReference($alias$type$reference->getInvalidBehavior());
  325.             }
  326.             if ($this->container->has($name) && !$this->container->findDefinition($name)->isAbstract()) {
  327.                 foreach ($this->container->getAliases() as $id => $alias) {
  328.                     if ($name === (string) $alias && str_starts_with($id$type.' $')) {
  329.                         return new TypedReference($name$type$reference->getInvalidBehavior());
  330.                     }
  331.                 }
  332.             }
  333.         }
  334.         if ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract()) {
  335.             return new TypedReference($type$type$reference->getInvalidBehavior());
  336.         }
  337.         if (null !== ($alias $this->getCombinedAlias($type) ?? null) && !$this->container->findDefinition($alias)->isAbstract()) {
  338.             return new TypedReference($alias$type$reference->getInvalidBehavior());
  339.         }
  340.         return null;
  341.     }
  342.     /**
  343.      * Populates the list of available types.
  344.      */
  345.     private function populateAvailableTypes(ContainerBuilder $container)
  346.     {
  347.         $this->types = [];
  348.         $this->ambiguousServiceTypes = [];
  349.         $this->autowiringAliases = [];
  350.         foreach ($container->getDefinitions() as $id => $definition) {
  351.             $this->populateAvailableType($container$id$definition);
  352.         }
  353.         foreach ($container->getAliases() as $id => $alias) {
  354.             $this->populateAutowiringAlias($id);
  355.         }
  356.     }
  357.     /**
  358.      * Populates the list of available types for a given definition.
  359.      */
  360.     private function populateAvailableType(ContainerBuilder $containerstring $idDefinition $definition)
  361.     {
  362.         // Never use abstract services
  363.         if ($definition->isAbstract()) {
  364.             return;
  365.         }
  366.         if ('' === $id || '.' === $id[0] || $definition->isDeprecated() || !$reflectionClass $container->getReflectionClass($definition->getClass(), false)) {
  367.             return;
  368.         }
  369.         foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
  370.             $this->set($reflectionInterface->name$id);
  371.         }
  372.         do {
  373.             $this->set($reflectionClass->name$id);
  374.         } while ($reflectionClass $reflectionClass->getParentClass());
  375.         $this->populateAutowiringAlias($id);
  376.     }
  377.     /**
  378.      * Associates a type and a service id if applicable.
  379.      */
  380.     private function set(string $typestring $id)
  381.     {
  382.         // is this already a type/class that is known to match multiple services?
  383.         if (isset($this->ambiguousServiceTypes[$type])) {
  384.             $this->ambiguousServiceTypes[$type][] = $id;
  385.             return;
  386.         }
  387.         // check to make sure the type doesn't match multiple services
  388.         if (!isset($this->types[$type]) || $this->types[$type] === $id) {
  389.             $this->types[$type] = $id;
  390.             return;
  391.         }
  392.         // keep an array of all services matching this type
  393.         if (!isset($this->ambiguousServiceTypes[$type])) {
  394.             $this->ambiguousServiceTypes[$type] = [$this->types[$type]];
  395.             unset($this->types[$type]);
  396.         }
  397.         $this->ambiguousServiceTypes[$type][] = $id;
  398.     }
  399.     private function createTypeNotFoundMessageCallback(TypedReference $referencestring $label): \Closure
  400.     {
  401.         if (null === $this->typesClone->container) {
  402.             $this->typesClone->container = new ContainerBuilder($this->container->getParameterBag());
  403.             $this->typesClone->container->setAliases($this->container->getAliases());
  404.             $this->typesClone->container->setDefinitions($this->container->getDefinitions());
  405.             $this->typesClone->container->setResourceTracking(false);
  406.         }
  407.         $currentId $this->currentId;
  408.         return (function () use ($reference$label$currentId) {
  409.             return $this->createTypeNotFoundMessage($reference$label$currentId);
  410.         })->bindTo($this->typesClone);
  411.     }
  412.     private function createTypeNotFoundMessage(TypedReference $referencestring $labelstring $currentId): string
  413.     {
  414.         $type $reference->getType();
  415.         $i null;
  416.         $namespace $type;
  417.         do {
  418.             $namespace substr($namespace0$i);
  419.             if ($this->container->hasDefinition($namespace) && $tag $this->container->getDefinition($namespace)->getTag('container.excluded')) {
  420.                 return sprintf('Cannot autowire service "%s": %s needs an instance of "%s" but this type has been excluded %s.'$currentId$label$type$tag[0]['source'] ?? 'from autowiring');
  421.             }
  422.         } while (false !== $i strrpos($namespace'\\'));
  423.         if (!$r $this->container->getReflectionClass($typefalse)) {
  424.             // either $type does not exist or a parent class does not exist
  425.             try {
  426.                 $resource = new ClassExistenceResource($typefalse);
  427.                 // isFresh() will explode ONLY if a parent class/trait does not exist
  428.                 $resource->isFresh(0);
  429.                 $parentMsg false;
  430.             } catch (\ReflectionException $e) {
  431.                 $parentMsg $e->getMessage();
  432.             }
  433.             $message sprintf('has type "%s" but this class %s.'$type$parentMsg sprintf('is missing a parent class (%s)'$parentMsg) : 'was not found');
  434.         } else {
  435.             $alternatives $this->createTypeAlternatives($this->container$reference);
  436.             $message $this->container->has($type) ? 'this service is abstract' 'no such service exists';
  437.             $message sprintf('references %s "%s" but %s.%s'$r->isInterface() ? 'interface' 'class'$type$message$alternatives);
  438.             if ($r->isInterface() && !$alternatives) {
  439.                 $message .= ' Did you create a class that implements this interface?';
  440.             }
  441.         }
  442.         $message sprintf('Cannot autowire service "%s": %s %s'$currentId$label$message);
  443.         if (null !== $this->lastFailure) {
  444.             $message $this->lastFailure."\n".$message;
  445.             $this->lastFailure null;
  446.         }
  447.         return $message;
  448.     }
  449.     private function createTypeAlternatives(ContainerBuilder $containerTypedReference $reference): string
  450.     {
  451.         // try suggesting available aliases first
  452.         if ($message $this->getAliasesSuggestionForType($container$type $reference->getType())) {
  453.             return ' '.$message;
  454.         }
  455.         if (!isset($this->ambiguousServiceTypes)) {
  456.             $this->populateAvailableTypes($container);
  457.         }
  458.         $servicesAndAliases $container->getServiceIds();
  459.         if (null !== ($autowiringAliases $this->autowiringAliases[$type] ?? null) && !isset($autowiringAliases[''])) {
  460.             return sprintf(' Available autowiring aliases for this %s are: "$%s".'class_exists($typefalse) ? 'class' 'interface'implode('", "$'$autowiringAliases));
  461.         }
  462.         if (!$container->has($type) && false !== $key array_search(strtolower($type), array_map('strtolower'$servicesAndAliases))) {
  463.             return sprintf(' Did you mean "%s"?'$servicesAndAliases[$key]);
  464.         } elseif (isset($this->ambiguousServiceTypes[$type])) {
  465.             $message sprintf('one of these existing services: "%s"'implode('", "'$this->ambiguousServiceTypes[$type]));
  466.         } elseif (isset($this->types[$type])) {
  467.             $message sprintf('the existing "%s" service'$this->types[$type]);
  468.         } else {
  469.             return '';
  470.         }
  471.         return sprintf(' You should maybe alias this %s to %s.'class_exists($typefalse) ? 'class' 'interface'$message);
  472.     }
  473.     private function getAliasesSuggestionForType(ContainerBuilder $containerstring $type): ?string
  474.     {
  475.         $aliases = [];
  476.         foreach (class_parents($type) + class_implements($type) as $parent) {
  477.             if ($container->has($parent) && !$container->findDefinition($parent)->isAbstract()) {
  478.                 $aliases[] = $parent;
  479.             }
  480.         }
  481.         if ($len \count($aliases)) {
  482.             $message 'Try changing the type-hint to one of its parents: ';
  483.             for ($i 0, --$len$i $len; ++$i) {
  484.                 $message .= sprintf('%s "%s", 'class_exists($aliases[$i], false) ? 'class' 'interface'$aliases[$i]);
  485.             }
  486.             $message .= sprintf('or %s "%s".'class_exists($aliases[$i], false) ? 'class' 'interface'$aliases[$i]);
  487.             return $message;
  488.         }
  489.         if ($aliases) {
  490.             return sprintf('Try changing the type-hint to "%s" instead.'$aliases[0]);
  491.         }
  492.         return null;
  493.     }
  494.     private function populateAutowiringAlias(string $id): void
  495.     {
  496.         if (!preg_match('/(?(DEFINE)(?<V>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^((?&V)(?:\\\\(?&V))*+)(?: \$((?&V)))?$/'$id$m)) {
  497.             return;
  498.         }
  499.         $type $m[2];
  500.         $name $m[3] ?? '';
  501.         if (class_exists($typefalse) || interface_exists($typefalse)) {
  502.             $this->autowiringAliases[$type][$name] = $name;
  503.         }
  504.     }
  505.     private function getCombinedAlias(string $typestring $name null): ?string
  506.     {
  507.         if (str_contains($type'&')) {
  508.             $types explode('&'$type);
  509.         } elseif (str_contains($type'|')) {
  510.             $types explode('|'$type);
  511.         } else {
  512.             return null;
  513.         }
  514.         $alias null;
  515.         $suffix $name ' $'.$name '';
  516.         foreach ($types as $type) {
  517.             if (!$this->container->hasAlias($type.$suffix)) {
  518.                 return null;
  519.             }
  520.             if (null === $alias) {
  521.                 $alias = (string) $this->container->getAlias($type.$suffix);
  522.             } elseif ((string) $this->container->getAlias($type.$suffix) !== $alias) {
  523.                 return null;
  524.             }
  525.         }
  526.         return $alias;
  527.     }
  528. }