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

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