Total Complexity | 68 |
Total Lines | 236 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ResolveBindingsPass 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 ResolveBindingsPass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ResolveBindingsPass extends AbstractRecursivePass |
||
32 | { |
||
33 | protected bool $skipScalars = true; |
||
34 | |||
35 | private array $usedBindings = []; |
||
36 | private array $unusedBindings = []; |
||
37 | private array $errorMessages = []; |
||
38 | |||
39 | public function process(ContainerBuilder $container): void |
||
91 | } |
||
92 | } |
||
93 | |||
94 | protected function processValue(mixed $value, bool $isRoot = false): mixed |
||
95 | { |
||
96 | if ($value instanceof TypedReference && $value->getType() === (string) $value) { |
||
97 | // Already checked |
||
98 | $bindings = $this->container->getDefinition($this->currentId)->getBindings(); |
||
99 | $name = $value->getName(); |
||
100 | |||
101 | if (isset($name, $bindings[$name = $value.' $'.$name])) { |
||
102 | return $this->getBindingValue($bindings[$name]); |
||
103 | } |
||
104 | |||
105 | if (isset($bindings[$value->getType()])) { |
||
106 | return $this->getBindingValue($bindings[$value->getType()]); |
||
107 | } |
||
108 | |||
109 | return parent::processValue($value, $isRoot); |
||
110 | } |
||
111 | |||
112 | if (!$value instanceof Definition || !$bindings = $value->getBindings()) { |
||
113 | return parent::processValue($value, $isRoot); |
||
114 | } |
||
115 | |||
116 | $bindingNames = []; |
||
117 | |||
118 | foreach ($bindings as $key => $binding) { |
||
119 | [$bindingValue, $bindingId, $used, $bindingType, $file] = $binding->getValues(); |
||
120 | if ($used) { |
||
121 | $this->usedBindings[$bindingId] = true; |
||
122 | unset($this->unusedBindings[$bindingId]); |
||
123 | } elseif (!isset($this->usedBindings[$bindingId])) { |
||
124 | $this->unusedBindings[$bindingId] = [$key, $this->currentId, $bindingType, $file]; |
||
125 | } |
||
126 | |||
127 | if (preg_match('/^(?:(?:array|bool|float|int|string|iterable|([^ $]++)) )\$/', $key, $m)) { |
||
128 | $bindingNames[substr($key, \strlen($m[0]))] = $binding; |
||
129 | } |
||
130 | |||
131 | if (!isset($m[1])) { |
||
132 | continue; |
||
133 | } |
||
134 | |||
135 | if (is_subclass_of($m[1], \UnitEnum::class)) { |
||
136 | $bindingNames[substr($key, \strlen($m[0]))] = $binding; |
||
137 | continue; |
||
138 | } |
||
139 | |||
140 | if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) { |
||
141 | throw new InvalidArgumentException(\sprintf('Invalid value for binding key "%s" for service "%s": expected "%s", "%s", "%s", "%s" or null, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, ServiceLocatorArgument::class, get_debug_type($bindingValue))); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | if ($value->isAbstract()) { |
||
146 | return parent::processValue($value, $isRoot); |
||
147 | } |
||
148 | |||
149 | $calls = $value->getMethodCalls(); |
||
150 | |||
151 | try { |
||
152 | if ($constructor = $this->getConstructor($value, false)) { |
||
153 | $calls[] = [$constructor, $value->getArguments()]; |
||
154 | } |
||
155 | } catch (RuntimeException $e) { |
||
156 | $this->errorMessages[] = $e->getMessage(); |
||
157 | $this->container->getDefinition($this->currentId)->addError($e->getMessage()); |
||
158 | |||
159 | return parent::processValue($value, $isRoot); |
||
160 | } |
||
161 | |||
162 | foreach ($calls as $i => $call) { |
||
163 | [$method, $arguments] = $call; |
||
164 | |||
165 | if ($method instanceof \ReflectionFunctionAbstract) { |
||
166 | $reflectionMethod = $method; |
||
167 | } else { |
||
168 | try { |
||
169 | $reflectionMethod = $this->getReflectionMethod($value, $method); |
||
170 | } catch (RuntimeException $e) { |
||
171 | if ($value->getFactory()) { |
||
172 | continue; |
||
173 | } |
||
174 | throw $e; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | $names = []; |
||
179 | |||
180 | foreach ($reflectionMethod->getParameters() as $key => $parameter) { |
||
181 | $names[$key] = $parameter->name; |
||
182 | |||
183 | if (\array_key_exists($key, $arguments) && '' !== $arguments[$key] && !$arguments[$key] instanceof AbstractArgument) { |
||
184 | continue; |
||
185 | } |
||
186 | if (\array_key_exists($parameter->name, $arguments) && '' !== $arguments[$parameter->name] && !$arguments[$parameter->name] instanceof AbstractArgument) { |
||
187 | continue; |
||
188 | } |
||
189 | if ( |
||
190 | $value->isAutowired() |
||
191 | && !$value->hasTag('container.ignore_attributes') |
||
192 | && $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF) |
||
193 | ) { |
||
194 | continue; |
||
195 | } |
||
196 | |||
197 | $typeHint = ltrim(ProxyHelper::exportType($parameter) ?? '', '?'); |
||
198 | |||
199 | $name = Target::parseName($parameter, parsedName: $parsedName); |
||
200 | |||
201 | if ($typeHint && ( |
||
202 | \array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$name, $bindings) |
||
203 | || \array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$parsedName, $bindings) |
||
204 | )) { |
||
205 | $arguments[$key] = $this->getBindingValue($bindings[$k]); |
||
206 | |||
207 | continue; |
||
208 | } |
||
209 | |||
210 | if (\array_key_exists($k = '$'.$name, $bindings) || \array_key_exists($k = '$'.$parsedName, $bindings)) { |
||
211 | $arguments[$key] = $this->getBindingValue($bindings[$k]); |
||
212 | |||
213 | continue; |
||
214 | } |
||
215 | |||
216 | if ($typeHint && '\\' === $typeHint[0] && isset($bindings[$typeHint = substr($typeHint, 1)])) { |
||
217 | $arguments[$key] = $this->getBindingValue($bindings[$typeHint]); |
||
218 | |||
219 | continue; |
||
220 | } |
||
221 | |||
222 | if (isset($bindingNames[$name]) || isset($bindingNames[$parsedName]) || isset($bindingNames[$parameter->name])) { |
||
223 | $bindingKey = array_search($binding, $bindings, true); |
||
224 | $argumentType = substr($bindingKey, 0, strpos($bindingKey, ' ')); |
||
225 | $this->errorMessages[] = \sprintf('Did you forget to add the type "%s" to argument "$%s" of method "%s::%s()"?', $argumentType, $parameter->name, $reflectionMethod->class, $reflectionMethod->name); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | foreach ($names as $key => $name) { |
||
230 | if (\array_key_exists($name, $arguments) && (0 === $key || \array_key_exists($key - 1, $arguments))) { |
||
231 | if (!array_key_exists($key, $arguments)) { |
||
232 | $arguments[$key] = $arguments[$name]; |
||
233 | } |
||
234 | unset($arguments[$name]); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | if ($arguments !== $call[1]) { |
||
239 | ksort($arguments, \SORT_NATURAL); |
||
240 | $calls[$i][1] = $arguments; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | if ($constructor) { |
||
245 | [, $arguments] = array_pop($calls); |
||
246 | |||
247 | if ($arguments !== $value->getArguments()) { |
||
248 | $value->setArguments($arguments); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | if ($calls !== $value->getMethodCalls()) { |
||
253 | $value->setMethodCalls($calls); |
||
254 | } |
||
255 | |||
256 | return parent::processValue($value, $isRoot); |
||
257 | } |
||
258 | |||
259 | private function getBindingValue(BoundArgument $binding): mixed |
||
267 | } |
||
268 | } |
||
269 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths