1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\DependencyInjection\Compiler;
13
14use Symfony\Component\DependencyInjection\Definition;
15use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
17use Symfony\Component\DependencyInjection\Reference;
18
19/**
20 * Resolves named arguments to their corresponding numeric index.
21 *
22 * @author Kévin Dunglas <dunglas@gmail.com>
23 */
24class ResolveNamedArgumentsPass extends AbstractRecursivePass
25{
26    /**
27     * {@inheritdoc}
28     */
29    protected function processValue($value, $isRoot = false)
30    {
31        if (!$value instanceof Definition) {
32            return parent::processValue($value, $isRoot);
33        }
34
35        $calls = $value->getMethodCalls();
36        $calls[] = ['__construct', $value->getArguments()];
37
38        foreach ($calls as $i => $call) {
39            list($method, $arguments) = $call;
40            $parameters = null;
41            $resolvedArguments = [];
42
43            foreach ($arguments as $key => $argument) {
44                if (\is_int($key)) {
45                    $resolvedArguments[$key] = $argument;
46                    continue;
47                }
48
49                if (null === $parameters) {
50                    $r = $this->getReflectionMethod($value, $method);
51                    $class = $r instanceof \ReflectionMethod ? $r->class : $this->currentId;
52                    $method = $r->getName();
53                    $parameters = $r->getParameters();
54                }
55
56                if (isset($key[0]) && '$' === $key[0]) {
57                    foreach ($parameters as $j => $p) {
58                        if ($key === '$'.$p->name) {
59                            $resolvedArguments[$j] = $argument;
60
61                            continue 2;
62                        }
63                    }
64
65                    throw new InvalidArgumentException(sprintf('Invalid service "%s": method "%s()" has no argument named "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $key));
66                }
67
68                if (null !== $argument && !$argument instanceof Reference && !$argument instanceof Definition) {
69                    throw new InvalidArgumentException(sprintf('Invalid service "%s": the value of argument "%s" of method "%s()" must be null, an instance of "%s" or an instance of "%s", "%s" given.', $this->currentId, $key, $class !== $this->currentId ? $class.'::'.$method : $method, Reference::class, Definition::class, \gettype($argument)));
70                }
71
72                $typeFound = false;
73                foreach ($parameters as $j => $p) {
74                    if (!\array_key_exists($j, $resolvedArguments) && ProxyHelper::getTypeHint($r, $p, true) === $key) {
75                        $resolvedArguments[$j] = $argument;
76                        $typeFound = true;
77                    }
78                }
79
80                if (!$typeFound) {
81                    throw new InvalidArgumentException(sprintf('Invalid service "%s": method "%s()" has no argument type-hinted as "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $key));
82                }
83            }
84
85            if ($resolvedArguments !== $call[1]) {
86                ksort($resolvedArguments);
87                $calls[$i][1] = $resolvedArguments;
88            }
89        }
90
91        list(, $arguments) = array_pop($calls);
92
93        if ($arguments !== $value->getArguments()) {
94            $value->setArguments($arguments);
95        }
96        if ($calls !== $value->getMethodCalls()) {
97            $value->setMethodCalls($calls);
98        }
99
100        return parent::processValue($value, $isRoot);
101    }
102}
103