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 | /** |
||
27 | * @var ArgumentsBuilder |
||
28 | */ |
||
29 | protected $argumentsBuilder; |
||
30 | |||
31 | protected $analyzeArguments = true; |
||
32 | |||
33 | 61 | public function __construct( |
|
41 | |||
42 | /** |
||
43 | * {@inheritDoc} |
||
44 | */ |
||
45 | 48 | public function setContainer(ContainerInterface $container): void |
|
50 | |||
51 | /** |
||
52 | * {@inheritDoc} |
||
53 | */ |
||
54 | 39 | public function construct(DefinitionInterface $definition): object |
|
63 | |||
64 | /** |
||
65 | * {@inheritDoc} |
||
66 | */ |
||
67 | 10 | public function configure(object $object, DefinitionInterface $definition): object |
|
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | 1 | public function invoke(callable $callable, array $arguments = []) |
|
81 | |||
82 | /** |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | 2 | public function inflect(object $object, InflectionInterface $inflection): object |
|
91 | |||
92 | /** |
||
93 | * @param object $object |
||
94 | * @param PropertyInterface[] $properties |
||
95 | */ |
||
96 | 12 | protected function applyProperties(object $object, array $properties): void |
|
102 | |||
103 | /** |
||
104 | * @param object $object |
||
105 | * @param CallInterface[] $calls |
||
106 | * @throws NotFoundException |
||
107 | */ |
||
108 | 12 | protected function applyCalls(object $object, array $calls): void |
|
114 | |||
115 | /** |
||
116 | * @param callable $callable |
||
117 | * @param array $arguments |
||
118 | * @return mixed |
||
119 | * @throws NotFoundException |
||
120 | */ |
||
121 | 5 | protected function call(callable $callable, array $arguments = []) |
|
131 | |||
132 | /** |
||
133 | * Make object without factory |
||
134 | * |
||
135 | * @param DefinitionInterface $definition |
||
136 | * @return mixed |
||
137 | * @throws NotFoundException |
||
138 | */ |
||
139 | 27 | protected function makeObjectSelf(DefinitionInterface $definition) |
|
155 | |||
156 | /** |
||
157 | * Make object with factory |
||
158 | * |
||
159 | * @param DefinitionInterface $definition |
||
160 | * @return mixed |
||
161 | * @throws InvalidConfigurationException |
||
162 | * @throws InvalidFactoryException |
||
163 | * @throws NotFoundException |
||
164 | */ |
||
165 | 12 | protected function makeObjectWithFactory(DefinitionInterface $definition) |
|
184 | |||
185 | /** |
||
186 | * Build callable factory from non-callable |
||
187 | * |
||
188 | * @param DefinitionInterface $definition |
||
189 | * @return callable |
||
190 | * @throws InvalidFactoryException |
||
191 | */ |
||
192 | 8 | protected function buildFactoryFromNonCallable(DefinitionInterface $definition): callable |
|
203 | |||
204 | /** |
||
205 | * @param DefinitionInterface $definition |
||
206 | * @param string $factory |
||
207 | * @return callable |
||
208 | * @throws InvalidFactoryException |
||
209 | */ |
||
210 | 5 | protected function resolveStringFactory(DefinitionInterface $definition, string $factory): callable |
|
216 | |||
217 | /** |
||
218 | * @param DefinitionInterface $definition |
||
219 | * @param array $factory |
||
220 | * @return callable |
||
221 | * @throws InvalidFactoryException |
||
222 | */ |
||
223 | 2 | protected function resolveArrayFactory(DefinitionInterface $definition, array $factory): callable |
|
235 | |||
236 | /** |
||
237 | * Fetch factory from container |
||
238 | * |
||
239 | * @param $id |
||
240 | * @param $method |
||
241 | * @return callable |
||
242 | * @throws InvalidFactoryException |
||
243 | */ |
||
244 | 7 | protected function resolveFactoryFromContainer($id, $method): callable |
|
252 | |||
253 | /** |
||
254 | * Create object with provided arguments and optional construct method |
||
255 | * |
||
256 | * @param string $className |
||
257 | * @param array $arguments |
||
258 | * @param string|null $constructMethod |
||
259 | * @return mixed |
||
260 | */ |
||
261 | 24 | protected function constructObject(string $className, array $arguments, ?string $constructMethod = null) |
|
270 | |||
271 | /** |
||
272 | * Build function attributes for type-value representation |
||
273 | * |
||
274 | * @param array $attributes |
||
275 | * @return Parameter[] |
||
276 | */ |
||
277 | 41 | protected function buildParameters($attributes = []): array |
|
285 | |||
286 | /** |
||
287 | * Create parameter from provided argument |
||
288 | * |
||
289 | * @param $value |
||
290 | * @return ParameterInterface |
||
291 | */ |
||
292 | 18 | protected function buildParameter($value): ParameterInterface |
|
309 | |||
310 | /** |
||
311 | * Try fetch dependency name from string value |
||
312 | * |
||
313 | * @param string $value |
||
314 | * @return string|null |
||
315 | */ |
||
316 | 7 | protected function fetchDependencyId(string $value): ?string |
|
323 | } |
||
324 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.