ResolveNamedArgumentsPass   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 42
eloc 64
dl 0
loc 111
rs 9.0399
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F processValue() 0 107 42

How to fix   Complexity   

Complex Class

Complex classes like ResolveNamedArgumentsPass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ResolveNamedArgumentsPass, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symfony\Component\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
15
use Symfony\Component\DependencyInjection\Definition;
16
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Symfony\Component\VarExporter\ProxyHelper;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\VarExporter\ProxyHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
/**
21
 * Resolves named arguments to their corresponding numeric index.
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 */
25
class ResolveNamedArgumentsPass extends AbstractRecursivePass
26
{
27
    protected bool $skipScalars = true;
28
29
    protected function processValue(mixed $value, bool $isRoot = false): mixed
30
    {
31
        if ($value instanceof AbstractArgument && $value->getText().'.' === $value->getTextWithContext()) {
32
            $value->setContext(\sprintf('A value found in service "%s"', $this->currentId));
33
        }
34
35
        if (!$value instanceof Definition) {
36
            return parent::processValue($value, $isRoot);
37
        }
38
39
        $calls = $value->getMethodCalls();
40
        $calls[] = ['__construct', $value->getArguments()];
41
42
        foreach ($calls as $i => $call) {
43
            [$method, $arguments] = $call;
44
            $parameters = null;
45
            $resolvedKeys = [];
46
            $resolvedArguments = [];
47
48
            foreach ($arguments as $key => $argument) {
49
                if ($argument instanceof AbstractArgument && $argument->getText().'.' === $argument->getTextWithContext()) {
50
                    $argument->setContext(\sprintf('Argument '.(\is_int($key) ? 1 + $key : '"%3$s"').' of '.('__construct' === $method ? 'service "%s"' : 'method call "%s::%s()"'), $this->currentId, $method, $key));
51
                }
52
53
                if (\is_int($key)) {
54
                    $resolvedKeys[$key] = $key;
55
                    $resolvedArguments[$key] = $argument;
56
                    continue;
57
                }
58
59
                if (null === $parameters) {
60
                    $r = $this->getReflectionMethod($value, $method);
61
                    $class = $r instanceof \ReflectionMethod ? $r->class : $this->currentId;
62
                    $method = $r->getName();
63
                    $parameters = $r->getParameters();
64
                }
65
66
                if (isset($key[0]) && '$' !== $key[0] && !class_exists($key) && !interface_exists($key, false)) {
67
                    throw new InvalidArgumentException(\sprintf('Invalid service "%s": did you forget to add the "$" prefix to argument "%s"?', $this->currentId, $key));
68
                }
69
70
                if (isset($key[0]) && '$' === $key[0]) {
71
                    foreach ($parameters as $j => $p) {
72
                        if ($key === '$'.$p->name) {
73
                            if ($p->isVariadic() && \is_array($argument)) {
74
                                foreach ($argument as $variadicArgument) {
75
                                    $resolvedKeys[$j] = $j;
76
                                    $resolvedArguments[$j++] = $variadicArgument;
77
                                }
78
                            } else {
79
                                $resolvedKeys[$j] = $p->name;
80
                                $resolvedArguments[$j] = $argument;
81
                            }
82
83
                            continue 2;
84
                        }
85
                    }
86
87
                    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));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $class does not seem to be defined for all execution paths leading up to this point.
Loading history...
88
                }
89
90
                if (null !== $argument && !$argument instanceof Reference && !$argument instanceof Definition) {
91
                    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, get_debug_type($argument)));
92
                }
93
94
                $typeFound = false;
95
                foreach ($parameters as $j => $p) {
96
                    if (!\array_key_exists($j, $resolvedArguments) && ProxyHelper::exportType($p, true) === $key) {
97
                        $resolvedKeys[$j] = $p->name;
98
                        $resolvedArguments[$j] = $argument;
99
                        $typeFound = true;
100
                    }
101
                }
102
103
                if (!$typeFound) {
104
                    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));
105
                }
106
            }
107
108
            if ($resolvedArguments !== $call[1]) {
109
                ksort($resolvedArguments);
110
111
                if (!$value->isAutowired() && !array_is_list($resolvedArguments)) {
0 ignored issues
show
Bug introduced by
The function array_is_list was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
                if (!$value->isAutowired() && !/** @scrutinizer ignore-call */ array_is_list($resolvedArguments)) {
Loading history...
112
                    ksort($resolvedKeys);
113
                    $resolvedArguments = array_combine($resolvedKeys, $resolvedArguments);
114
                }
115
116
                $calls[$i][1] = $resolvedArguments;
117
            }
118
        }
119
120
        [, $arguments] = array_pop($calls);
121
122
        if ($arguments !== $value->getArguments()) {
123
            $value->setArguments($arguments);
124
        }
125
        if ($calls !== $value->getMethodCalls()) {
126
            $value->setMethodCalls($calls);
127
        }
128
129
        foreach ($value->getProperties() as $key => $argument) {
130
            if ($argument instanceof AbstractArgument && $argument->getText().'.' === $argument->getTextWithContext()) {
131
                $argument->setContext(\sprintf('Property "%s" of service "%s"', $key, $this->currentId));
132
            }
133
        }
134
135
        return parent::processValue($value, $isRoot);
136
    }
137
}
138