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

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