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 | * Manager builder's common default options. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $defaultOptions = [ |
||
36 | //'connection' => [], |
||
37 | //'annotation_files' => [], |
||
38 | //'annotation_namespaces' => [], |
||
39 | //'annotation_autoloaders' => [], |
||
40 | //'metadata_mapping' => [], |
||
41 | //'proxies_path' => null, |
||
42 | //'proxies_namespace' => '', |
||
43 | 'proxies_auto_generation' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
||
44 | //'metadata_cache_driver' => null, |
||
45 | //'metadata_cache_namespace' => '', |
||
46 | //'event_manager' => null, |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Builder options. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $options = []; |
||
55 | |||
56 | /** |
||
57 | * Builder name. |
||
58 | * |
||
59 | * @var |
||
60 | */ |
||
61 | protected $name; |
||
62 | |||
63 | /** |
||
64 | * Metadata mapping driver. |
||
65 | * |
||
66 | * @var MappingDriverChain |
||
67 | */ |
||
68 | protected $mappingDriver; |
||
69 | |||
70 | /** |
||
71 | * General cache driver. |
||
72 | * |
||
73 | * @var CacheProvider |
||
74 | */ |
||
75 | protected $cacheDriver; |
||
76 | |||
77 | /** |
||
78 | * Metadata cache driver. |
||
79 | * |
||
80 | * @var CacheProvider |
||
81 | */ |
||
82 | protected $metadataCacheDriver; |
||
83 | |||
84 | /** |
||
85 | * Event manager. |
||
86 | * |
||
87 | * @var EventManager |
||
88 | */ |
||
89 | protected $eventManager; |
||
90 | |||
91 | /** |
||
92 | * ManagerBuilder constructor. |
||
93 | * |
||
94 | * @param array $options |
||
95 | * @param string|null $name |
||
96 | */ |
||
97 | public function __construct(array $options = [], $name = null) |
||
102 | |||
103 | /** |
||
104 | * Get manager builder's default options. |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | abstract protected function getDefaultOptions(); |
||
109 | |||
110 | /** |
||
111 | * Unset created objects for rebuild. |
||
112 | */ |
||
113 | protected function wipe() |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | * |
||
124 | * @return string|null |
||
125 | */ |
||
126 | public function getName() |
||
130 | |||
131 | /** |
||
132 | * Set builder's name. |
||
133 | * |
||
134 | * @param string|null $name |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function setName($name = null) |
||
144 | |||
145 | /** |
||
146 | * Retrieve builder options. |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public function getOptions() |
||
154 | |||
155 | /** |
||
156 | * Retrieve builder option. |
||
157 | * |
||
158 | * @param string $option |
||
159 | * @param mixed|null $default |
||
160 | * |
||
161 | * @return mixed |
||
162 | */ |
||
163 | public function getOption($option, $default = null) |
||
167 | |||
168 | /** |
||
169 | * Verifies option existence. |
||
170 | * |
||
171 | * @param string $option |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function hasOption($option) |
||
179 | |||
180 | /** |
||
181 | * Set builder options. |
||
182 | * |
||
183 | * @param array $options |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function setOptions(array $options) |
||
195 | |||
196 | /** |
||
197 | * Set builder option. |
||
198 | * |
||
199 | * @param string $option |
||
200 | * @param mixed $value |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function setOption($option, $value) |
||
210 | |||
211 | /** |
||
212 | * Set up annotation metadata. |
||
213 | * |
||
214 | * @param bool $registerDefault |
||
215 | * |
||
216 | * @throws \InvalidArgumentException |
||
217 | * @throws \RuntimeException |
||
218 | */ |
||
219 | protected function setupAnnotationMetadata($registerDefault = false) |
||
247 | |||
248 | /** |
||
249 | * Retrieve annotation namespaces. |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | protected function getAnnotationNamespaces() |
||
265 | |||
266 | /** |
||
267 | * Create metadata mapping driver. |
||
268 | * |
||
269 | * @throws \RuntimeException |
||
270 | * @throws \UnexpectedValueException |
||
271 | * |
||
272 | * @return MappingDriverChain |
||
273 | */ |
||
274 | public function getMetadataMappingDriver() |
||
308 | |||
309 | /** |
||
310 | * Retrieve mapping driver. |
||
311 | * |
||
312 | * @param array $metadataMapping |
||
313 | * |
||
314 | * @throws \UnexpectedValueException |
||
315 | * |
||
316 | * @return MappingDriver |
||
317 | */ |
||
318 | protected function getMappingDriver(array $metadataMapping) |
||
361 | |||
362 | /** |
||
363 | * Get annotation metadata driver. |
||
364 | * |
||
365 | * @param array $paths |
||
366 | * |
||
367 | * @return MappingDriver |
||
368 | */ |
||
369 | abstract protected function getAnnotationMetadataDriver(array $paths); |
||
370 | |||
371 | /** |
||
372 | * Get XML metadata driver. |
||
373 | * |
||
374 | * @param array $paths |
||
375 | * @param string|null $extension |
||
376 | * |
||
377 | * @return MappingDriver |
||
378 | */ |
||
379 | abstract protected function getXmlMetadataDriver(array $paths, $extension = null); |
||
380 | |||
381 | /** |
||
382 | * Get YAML metadata driver. |
||
383 | * |
||
384 | * @param array $paths |
||
385 | * @param string|null $extension |
||
386 | * |
||
387 | * @return MappingDriver |
||
388 | */ |
||
389 | abstract protected function getYamlMetadataDriver(array $paths, $extension = null); |
||
390 | |||
391 | /** |
||
392 | * Get PHP metadata driver. |
||
393 | * |
||
394 | * @param array $paths |
||
395 | * |
||
396 | * @return PHPDriver |
||
397 | */ |
||
398 | protected function getPhpMetadataDriver(array $paths) |
||
402 | |||
403 | /** |
||
404 | * Retrieve proxies path. |
||
405 | * |
||
406 | * @return string |
||
407 | */ |
||
408 | protected function getProxiesPath() |
||
412 | |||
413 | /** |
||
414 | * Retrieve proxies namespace. |
||
415 | * |
||
416 | * @return null|string |
||
417 | */ |
||
418 | protected function getProxiesNamespace() |
||
424 | |||
425 | /** |
||
426 | * Retrieve proxy generation strategy. |
||
427 | * |
||
428 | * @return int |
||
429 | */ |
||
430 | protected function getProxiesAutoGeneration() |
||
434 | |||
435 | /** |
||
436 | * Retrieve metadata cache driver. |
||
437 | * |
||
438 | * @throws \InvalidArgumentException |
||
439 | * |
||
440 | * @return CacheProvider |
||
441 | */ |
||
442 | public function getMetadataCacheDriver() |
||
460 | |||
461 | /** |
||
462 | * Retrieve a newly created cache driver. |
||
463 | * |
||
464 | * @return ApcuCache|ArrayCache|MemcacheCache|RedisCache|XcacheCache |
||
465 | */ |
||
466 | private function createNewCacheDriver() |
||
501 | |||
502 | /** |
||
503 | * Set metadata cache driver. |
||
504 | * |
||
505 | * @param CacheProvider $metadataCacheDriver |
||
506 | */ |
||
507 | public function setMetadataCacheDriver(CacheProvider $metadataCacheDriver) |
||
511 | |||
512 | /** |
||
513 | * Retrieve event manager. |
||
514 | * |
||
515 | * @return EventManager |
||
516 | */ |
||
517 | public function getEventManager() |
||
531 | |||
532 | /** |
||
533 | * Set event manager. |
||
534 | * |
||
535 | * @param EventManager $eventManager |
||
536 | */ |
||
537 | public function setEventManager(EventManager $eventManager) |
||
541 | } |
||
542 |