Complex classes like Builder 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Builder, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
14 | class Builder implements BuilderInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var ContainerInterface |
||
18 | */ |
||
19 | protected $container; |
||
20 | |||
21 | /** |
||
22 | * @var DependenciesResolver |
||
23 | */ |
||
24 | protected $dependenciesResolver; |
||
25 | |||
26 | protected $autoWire = true; |
||
27 | |||
28 | 61 | public function __construct(bool $autoWire = true, ?DependenciesResolverInterface $dependenciesResolver = null) |
|
33 | |||
34 | /** |
||
35 | * {@inheritDoc} |
||
36 | */ |
||
37 | 48 | public function setContainer(ContainerInterface $container): void |
|
41 | |||
42 | /** |
||
43 | * {@inheritDoc} |
||
44 | */ |
||
45 | 39 | public function construct(DefinitionInterface $definition): object |
|
54 | |||
55 | /** |
||
56 | * {@inheritDoc} |
||
57 | */ |
||
58 | 10 | public function configure(object $object, DefinitionInterface $definition): object |
|
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | 1 | public function invoke(callable $callable, array $arguments = []) |
|
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | */ |
||
76 | 2 | public function inflect(object $object, InflectionInterface $inflection): object |
|
82 | |||
83 | /** |
||
84 | * @param object $object |
||
85 | * @param PropertyInterface[] $properties |
||
86 | */ |
||
87 | 12 | protected function applyProperties(object $object, array $properties): void |
|
93 | |||
94 | /** |
||
95 | * @param object $object |
||
96 | * @param CallInterface[] $calls |
||
97 | * @throws NotFoundException |
||
98 | */ |
||
99 | 12 | protected function applyCalls(object $object, array $calls): void |
|
105 | |||
106 | /** |
||
107 | * @param callable $callable |
||
108 | * @param array $arguments |
||
109 | * @return mixed |
||
110 | * @throws NotFoundException |
||
111 | */ |
||
112 | 5 | protected function call(callable $callable, array $arguments = []) |
|
122 | |||
123 | /** |
||
124 | * Make object without factory |
||
125 | * |
||
126 | * @param DefinitionInterface $definition |
||
127 | * @return mixed |
||
128 | * @throws NotFoundException |
||
129 | */ |
||
130 | 27 | protected function makeObjectSelf(DefinitionInterface $definition) |
|
146 | |||
147 | /** |
||
148 | * Make object with factory |
||
149 | * |
||
150 | * @param DefinitionInterface $definition |
||
151 | * @return mixed |
||
152 | * @throws InvalidConfigurationException |
||
153 | * @throws InvalidFactoryException |
||
154 | * @throws NotFoundException |
||
155 | */ |
||
156 | 12 | protected function makeObjectWithFactory(DefinitionInterface $definition) |
|
175 | |||
176 | /** |
||
177 | * Build callable factory from non-callable |
||
178 | * |
||
179 | * @param DefinitionInterface $definition |
||
180 | * @return callable |
||
181 | * @throws InvalidFactoryException |
||
182 | */ |
||
183 | 8 | protected function buildFactoryFromNonCallable(DefinitionInterface $definition): callable |
|
201 | |||
202 | /** |
||
203 | * Create object with provided arguments and optional construct method |
||
204 | * |
||
205 | * @param string $className |
||
206 | * @param array $arguments |
||
207 | * @param string|null $constructMethod |
||
208 | * @return mixed |
||
209 | */ |
||
210 | 24 | protected function constructObject(string $className, array $arguments, ?string $constructMethod = null) |
|
219 | |||
220 | /** |
||
221 | * Build function attributes for type-value representation |
||
222 | * |
||
223 | * @param array $attributes |
||
224 | * @return Parameter[] |
||
225 | */ |
||
226 | 41 | protected function buildParameters($attributes = []): array |
|
234 | |||
235 | /** |
||
236 | * Create parameter from provided argument |
||
237 | * |
||
238 | * @param $value |
||
239 | * @return ParameterInterface |
||
240 | */ |
||
241 | 18 | protected function buildParameter($value): ParameterInterface |
|
258 | |||
259 | /** |
||
260 | * Try fetch dependency name from string value |
||
261 | * |
||
262 | * @param string $value |
||
263 | * @return string|null |
||
264 | */ |
||
265 | 7 | protected function fetchDependencyId(string $value): ?string |
|
272 | |||
273 | /** |
||
274 | * Build arguments by dependencies and parameters |
||
275 | * |
||
276 | * @param DependencyInterface[] $dependencies |
||
277 | * @param ParameterInterface[] $parameters |
||
278 | * @return array |
||
279 | * @throws NotFoundException |
||
280 | */ |
||
281 | 41 | protected function buildArguments(array $dependencies, array $parameters): array |
|
293 | |||
294 | /** |
||
295 | * @param DependencyInterface[] $dependencies |
||
296 | * @param ParameterInterface[] $parameters |
||
297 | * @return array |
||
298 | * @throws NotFoundException |
||
299 | */ |
||
300 | 33 | protected function buildArgumentsFromDependencies(array $dependencies, array $parameters): array |
|
325 | |||
326 | /** |
||
327 | * @param array $parameters |
||
328 | * @param array $usedParameters |
||
329 | * @param array $arguments |
||
330 | * @return array |
||
331 | * @throws NotFoundException |
||
332 | */ |
||
333 | 30 | protected function appendUnusedParamsToArguments( |
|
345 | |||
346 | /** |
||
347 | * @param ParameterInterface $parameter |
||
348 | * @return mixed |
||
349 | * @throws NotFoundException |
||
350 | */ |
||
351 | 18 | protected function makeArgumentByParameter(ParameterInterface $parameter) |
|
362 | |||
363 | /** |
||
364 | * @param DependencyInterface $dependency |
||
365 | * @return mixed |
||
366 | * @throws NotFoundException |
||
367 | */ |
||
368 | 25 | protected function makeArgumentByDependency(DependencyInterface $dependency) |
|
379 | |||
380 | /** |
||
381 | * @param $id |
||
382 | * @return mixed |
||
383 | * @throws NotFoundException |
||
384 | */ |
||
385 | 23 | protected function retrieveRequiredDependencyFromContainer($id) |
|
392 | |||
393 | /** |
||
394 | * @param $id |
||
395 | * @return mixed |
||
396 | */ |
||
397 | 4 | protected function retrieveOptionalDependencyFromContainer($id) |
|
404 | } |
||
405 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.