Complex classes like AbstractManagerBuilder 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 AbstractManagerBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class AbstractManagerBuilder implements ManagerBuilder |
||
29 | { |
||
30 | /** |
||
31 | * Builder name. |
||
32 | * |
||
33 | * @var |
||
34 | */ |
||
35 | protected $name; |
||
36 | |||
37 | /** |
||
38 | * Builder options. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $options = []; |
||
43 | |||
44 | /** |
||
45 | * Metadata mapping driver. |
||
46 | * |
||
47 | * @var MappingDriverChain |
||
48 | */ |
||
49 | protected $mappingDriver; |
||
50 | |||
51 | /** |
||
52 | * General cache driver. |
||
53 | * |
||
54 | * @var Cache |
||
55 | */ |
||
56 | protected $cacheDriver; |
||
57 | |||
58 | /** |
||
59 | * Metadata cache driver. |
||
60 | * |
||
61 | * @var Cache |
||
62 | */ |
||
63 | protected $metadataCacheDriver; |
||
64 | |||
65 | /** |
||
66 | * Mapping driver chain. |
||
67 | * |
||
68 | * @var MappingDriverChain |
||
69 | */ |
||
70 | protected $mappingDriverChain; |
||
71 | |||
72 | /** |
||
73 | * Event manager. |
||
74 | * |
||
75 | * @var EventManager |
||
76 | */ |
||
77 | protected $eventManager; |
||
78 | |||
79 | /** |
||
80 | * ManagerBuilder constructor. |
||
81 | * |
||
82 | * @param array $options |
||
83 | * @param string|null $name |
||
84 | */ |
||
85 | public function __construct(array $options = [], $name = null) |
||
90 | |||
91 | /** |
||
92 | * Unset created objects for rebuild. |
||
93 | */ |
||
94 | protected function wipe() |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | * |
||
106 | * @return string|null |
||
107 | */ |
||
108 | public function getName() |
||
112 | |||
113 | /** |
||
114 | * Set builder's name. |
||
115 | * |
||
116 | * @param string|null $name |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function setName($name = null) |
||
126 | |||
127 | /** |
||
128 | * Get default options. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | abstract protected function getDefaultOptions(); |
||
133 | |||
134 | /** |
||
135 | * Retrieve builder options. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public function getOptions() |
||
143 | |||
144 | /** |
||
145 | * Retrieve builder option. |
||
146 | * |
||
147 | * @param string $option |
||
148 | * @param mixed|null $default |
||
149 | * |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function getOption($option, $default = null) |
||
156 | |||
157 | /** |
||
158 | * Verifies option existence. |
||
159 | * |
||
160 | * @param string $option |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function hasOption($option) |
||
168 | |||
169 | /** |
||
170 | * Set builder options. |
||
171 | * |
||
172 | * @param array $options |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function setOptions(array $options) |
||
184 | |||
185 | /** |
||
186 | * Set builder option. |
||
187 | * |
||
188 | * @param string $option |
||
189 | * @param mixed $value |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function setOption($option, $value) |
||
199 | |||
200 | /** |
||
201 | * Set up annotation metadata. |
||
202 | * |
||
203 | * @param bool $registerDefault |
||
204 | * |
||
205 | * @throws \InvalidArgumentException |
||
206 | * @throws \RuntimeException |
||
207 | */ |
||
208 | protected function setupAnnotationMetadata($registerDefault = false) |
||
236 | |||
237 | /** |
||
238 | * Retrieve annotation namespaces. |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | protected function getAnnotationNamespaces() |
||
254 | |||
255 | /** |
||
256 | * Create metadata mapping driver. |
||
257 | * |
||
258 | * @throws \RuntimeException |
||
259 | * @throws \UnexpectedValueException |
||
260 | * |
||
261 | * @return MappingDriverChain |
||
262 | */ |
||
263 | public function getMetadataMappingDriver() |
||
297 | |||
298 | /** |
||
299 | * Retrieve mapping driver. |
||
300 | * |
||
301 | * @param array $metadataMapping |
||
302 | * |
||
303 | * @throws \UnexpectedValueException |
||
304 | * |
||
305 | * @return MappingDriver |
||
306 | */ |
||
307 | protected function getMappingDriver(array $metadataMapping) |
||
345 | |||
346 | /** |
||
347 | * Get annotation metadata driver. |
||
348 | * |
||
349 | * @param array $paths |
||
350 | * |
||
351 | * @return MappingDriver |
||
352 | */ |
||
353 | abstract protected function getAnnotationMetadataDriver(array $paths); |
||
354 | |||
355 | /** |
||
356 | * Get XML metadata driver. |
||
357 | * |
||
358 | * @param array $paths |
||
359 | * @param string|null $extension |
||
360 | * |
||
361 | * @return MappingDriver |
||
362 | */ |
||
363 | abstract protected function getXmlMetadataDriver(array $paths, $extension = null); |
||
364 | |||
365 | /** |
||
366 | * Get YAML metadata driver. |
||
367 | * |
||
368 | * @param array $paths |
||
369 | * @param string|null $extension |
||
370 | * |
||
371 | * @return MappingDriver |
||
372 | */ |
||
373 | abstract protected function getYamlMetadataDriver(array $paths, $extension = null); |
||
374 | |||
375 | /** |
||
376 | * Retrieve proxies path. |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function getProxiesPath() |
||
384 | |||
385 | /** |
||
386 | * Retrieve proxies namespace. |
||
387 | * |
||
388 | * @return null|string |
||
389 | */ |
||
390 | protected function getProxiesNamespace() |
||
396 | |||
397 | /** |
||
398 | * Retrieve proxy generation strategy. |
||
399 | * |
||
400 | * @return int |
||
401 | */ |
||
402 | protected function getProxiesAutoGeneration() |
||
406 | |||
407 | /** |
||
408 | * Retrieve metadata cache driver. |
||
409 | * |
||
410 | * @throws \InvalidArgumentException |
||
411 | * |
||
412 | * @return Cache |
||
413 | */ |
||
414 | public function getMetadataCacheDriver() |
||
433 | |||
434 | /** |
||
435 | * Set metadata cache driver. |
||
436 | * |
||
437 | * @param Cache $metadataCacheDriver |
||
438 | */ |
||
439 | public function setMetadataCacheDriver(Cache $metadataCacheDriver) |
||
443 | |||
444 | /** |
||
445 | * Retrieve metadata cache namespace. |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | protected function getMetadataCacheNamespace() |
||
453 | |||
454 | /** |
||
455 | * Retrieve general cache driver. |
||
456 | * |
||
457 | * @throws \InvalidArgumentException |
||
458 | * |
||
459 | * @return CacheProvider |
||
460 | */ |
||
461 | public function getCacheDriver() |
||
484 | |||
485 | /** |
||
486 | * Retrieve a newly created cache driver. |
||
487 | * |
||
488 | * @param string $namespace |
||
489 | * |
||
490 | * @return ApcuCache|ArrayCache|MemcacheCache|RedisCache|XcacheCache |
||
491 | */ |
||
492 | private function createNewCacheDriver($namespace) |
||
529 | |||
530 | /** |
||
531 | * Set general cache driver. |
||
532 | * |
||
533 | * @param Cache $cacheDriver |
||
534 | */ |
||
535 | public function setCacheDriver(Cache $cacheDriver) |
||
539 | |||
540 | /** |
||
541 | * Retrieve general cache driver namespace. |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | protected function getCacheDriverNamespace() |
||
549 | |||
550 | /** |
||
551 | * Retrieve event manager. |
||
552 | * |
||
553 | * @return EventManager |
||
554 | */ |
||
555 | public function getEventManager() |
||
569 | |||
570 | /** |
||
571 | * Set event manager. |
||
572 | * |
||
573 | * @param EventManager $eventManager |
||
574 | */ |
||
575 | public function setEventManager(EventManager $eventManager) |
||
579 | } |
||
580 |