ResolveChildDefinitionsPass::processValue()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
cc 5
nc 7
nop 2
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\ChildDefinition;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
18
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
19
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
20
21
/**
22
 * This replaces all ChildDefinition instances with their equivalent fully
23
 * merged Definition instance.
24
 *
25
 * @author Johannes M. Schmitt <[email protected]>
26
 * @author Nicolas Grekas <[email protected]>
27
 */
28
class ResolveChildDefinitionsPass extends AbstractRecursivePass
29
{
30
    protected bool $skipScalars = true;
31
32
    private array $currentPath;
33
34
    protected function processValue(mixed $value, bool $isRoot = false): mixed
35
    {
36
        if (!$value instanceof Definition) {
37
            return parent::processValue($value, $isRoot);
38
        }
39
        if ($isRoot) {
40
            // yes, we are specifically fetching the definition from the
41
            // container to ensure we are not operating on stale data
42
            $value = $this->container->getDefinition($this->currentId);
0 ignored issues
show
Bug introduced by
The method getDefinition() does not exist on null. ( Ignorable by Annotation )

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

42
            /** @scrutinizer ignore-call */ 
43
            $value = $this->container->getDefinition($this->currentId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like $this->currentId can also be of type null; however, parameter $id of Symfony\Component\Depend...uilder::getDefinition() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

42
            $value = $this->container->getDefinition(/** @scrutinizer ignore-type */ $this->currentId);
Loading history...
43
        }
44
        if ($value instanceof ChildDefinition) {
45
            $this->currentPath = [];
46
            $value = $this->resolveDefinition($value);
47
            if ($isRoot) {
48
                $this->container->setDefinition($this->currentId, $value);
0 ignored issues
show
Bug introduced by
It seems like $this->currentId can also be of type null; however, parameter $id of Symfony\Component\Depend...uilder::setDefinition() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
                $this->container->setDefinition(/** @scrutinizer ignore-type */ $this->currentId, $value);
Loading history...
49
            }
50
        }
51
52
        return parent::processValue($value, $isRoot);
53
    }
54
55
    /**
56
     * Resolves the definition.
57
     *
58
     * @throws RuntimeException When the definition is invalid
59
     */
60
    private function resolveDefinition(ChildDefinition $definition): Definition
61
    {
62
        try {
63
            return $this->doResolveDefinition($definition);
64
        } catch (ServiceCircularReferenceException $e) {
65
            throw $e;
66
        } catch (ExceptionInterface $e) {
67
            $r = new \ReflectionProperty($e, 'message');
68
            $r->setValue($e, \sprintf('Service "%s": %s', $this->currentId, $e->getMessage()));
69
70
            throw $e;
71
        }
72
    }
73
74
    private function doResolveDefinition(ChildDefinition $definition): Definition
75
    {
76
        if (!$this->container->has($parent = $definition->getParent())) {
77
            throw new RuntimeException(\sprintf('Parent definition "%s" does not exist.', $parent));
78
        }
79
80
        $searchKey = array_search($parent, $this->currentPath);
81
        $this->currentPath[] = $parent;
82
83
        if (false !== $searchKey) {
84
            throw new ServiceCircularReferenceException($parent, \array_slice($this->currentPath, $searchKey));
0 ignored issues
show
Bug introduced by
It seems like $searchKey can also be of type string; however, parameter $offset of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

84
            throw new ServiceCircularReferenceException($parent, \array_slice($this->currentPath, /** @scrutinizer ignore-type */ $searchKey));
Loading history...
85
        }
86
87
        $parentDef = $this->container->findDefinition($parent);
88
        if ($parentDef instanceof ChildDefinition) {
89
            $id = $this->currentId;
90
            $this->currentId = $parent;
91
            $parentDef = $this->resolveDefinition($parentDef);
92
            $this->container->setDefinition($parent, $parentDef);
93
            $this->currentId = $id;
94
        }
95
96
        $this->container->log($this, \sprintf('Resolving inheritance for "%s" (parent: %s).', $this->currentId, $parent));
97
        $def = new Definition();
98
99
        // merge in parent definition
100
        // purposely ignored attributes: abstract, shared, tags, autoconfigured
101
        $def->setClass($parentDef->getClass());
102
        $def->setArguments($parentDef->getArguments());
103
        $def->setMethodCalls($parentDef->getMethodCalls());
104
        $def->setProperties($parentDef->getProperties());
105
        if ($parentDef->isDeprecated()) {
106
            $deprecation = $parentDef->getDeprecation('%service_id%');
107
            $def->setDeprecated($deprecation['package'], $deprecation['version'], $deprecation['message']);
108
        }
109
        $def->setFactory($parentDef->getFactory());
110
        $def->setConfigurator($parentDef->getConfigurator());
111
        $def->setFile($parentDef->getFile());
112
        $def->setPublic($parentDef->isPublic());
113
        $def->setLazy($parentDef->isLazy());
114
        $def->setAutowired($parentDef->isAutowired());
115
        $def->setChanges($parentDef->getChanges());
116
117
        $def->setBindings($definition->getBindings() + $parentDef->getBindings());
118
119
        $def->setSynthetic($definition->isSynthetic());
120
121
        // overwrite with values specified in the decorator
122
        $changes = $definition->getChanges();
123
        if (isset($changes['class'])) {
124
            $def->setClass($definition->getClass());
125
        }
126
        if (isset($changes['factory'])) {
127
            $def->setFactory($definition->getFactory());
128
        }
129
        if (isset($changes['configurator'])) {
130
            $def->setConfigurator($definition->getConfigurator());
131
        }
132
        if (isset($changes['file'])) {
133
            $def->setFile($definition->getFile());
134
        }
135
        if (isset($changes['public'])) {
136
            $def->setPublic($definition->isPublic());
137
        } else {
138
            $def->setPublic($parentDef->isPublic());
139
        }
140
        if (isset($changes['lazy'])) {
141
            $def->setLazy($definition->isLazy());
142
        }
143
        if (isset($changes['deprecated']) && $definition->isDeprecated()) {
144
            $deprecation = $definition->getDeprecation('%service_id%');
145
            $def->setDeprecated($deprecation['package'], $deprecation['version'], $deprecation['message']);
146
        }
147
        if (isset($changes['autowired'])) {
148
            $def->setAutowired($definition->isAutowired());
149
        }
150
        if (isset($changes['shared'])) {
151
            $def->setShared($definition->isShared());
152
        }
153
        if (isset($changes['decorated_service'])) {
154
            $decoratedService = $definition->getDecoratedService();
155
            if (null === $decoratedService) {
156
                $def->setDecoratedService($decoratedService);
157
            } else {
158
                $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2], $decoratedService[3] ?? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
159
            }
160
        }
161
162
        // merge arguments
163
        foreach ($definition->getArguments() as $k => $v) {
164
            if (is_numeric($k)) {
165
                $def->addArgument($v);
166
            } elseif (str_starts_with($k, 'index_')) {
167
                $def->replaceArgument((int) substr($k, \strlen('index_')), $v);
168
            } else {
169
                $def->setArgument($k, $v);
170
            }
171
        }
172
173
        // merge properties
174
        foreach ($definition->getProperties() as $k => $v) {
175
            $def->setProperty($k, $v);
176
        }
177
178
        // append method calls
179
        if ($calls = $definition->getMethodCalls()) {
180
            $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
181
        }
182
183
        $def->addError($parentDef);
184
        $def->addError($definition);
185
186
        // these attributes are always taken from the child
187
        $def->setAbstract($definition->isAbstract());
188
        $def->setTags($definition->getTags());
189
        // autoconfigure is never taken from parent (on purpose)
190
        // and it's not legal on an instanceof
191
        $def->setAutoconfigured($definition->isAutoconfigured());
192
193
        if (!$def->hasTag('proxy')) {
194
            foreach ($parentDef->getTag('proxy') as $v) {
195
                $def->addTag('proxy', $v);
196
            }
197
        }
198
199
        return $def;
200
    }
201
}
202