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 |
||
30 | abstract class AbstractManagerBuilder implements ManagerBuilder |
||
31 | { |
||
32 | /** |
||
33 | * Manager builder's common default options. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $defaultOptions = [ |
||
38 | 'proxies_auto_generation' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Builder options. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $options = []; |
||
47 | |||
48 | /** |
||
49 | * Builder name. |
||
50 | * |
||
51 | * @var |
||
52 | */ |
||
53 | protected $name; |
||
54 | |||
55 | /** |
||
56 | * Metadata mapping driver. |
||
57 | * |
||
58 | * @var MappingDriverChain |
||
59 | */ |
||
60 | protected $mappingDriver; |
||
61 | |||
62 | /** |
||
63 | * General cache driver. |
||
64 | * |
||
65 | * @var CacheProvider |
||
66 | */ |
||
67 | protected $cacheDriver; |
||
68 | |||
69 | /** |
||
70 | * Metadata cache driver. |
||
71 | * |
||
72 | * @var CacheProvider |
||
73 | */ |
||
74 | protected $metadataCacheDriver; |
||
75 | |||
76 | /** |
||
77 | * Event manager. |
||
78 | * |
||
79 | * @var EventManager |
||
80 | */ |
||
81 | protected $eventManager; |
||
82 | |||
83 | /** |
||
84 | * ManagerBuilder constructor. |
||
85 | * |
||
86 | * @param array $options |
||
87 | * @param string|null $name |
||
88 | */ |
||
89 | public function __construct(array $options = [], $name = null) |
||
94 | |||
95 | /** |
||
96 | * Get manager builder's default options. |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | abstract protected function getDefaultOptions(); |
||
101 | |||
102 | /** |
||
103 | * Unset created objects for rebuild. |
||
104 | */ |
||
105 | protected function wipe() |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | * |
||
116 | * @return string|null |
||
117 | */ |
||
118 | public function getName() |
||
122 | |||
123 | /** |
||
124 | * Set builder's name. |
||
125 | * |
||
126 | * @param string|null $name |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function setName($name = null) |
||
136 | |||
137 | /** |
||
138 | * Retrieve builder options. |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getOptions() |
||
146 | |||
147 | /** |
||
148 | * Retrieve builder option. |
||
149 | * |
||
150 | * @param string $option |
||
151 | * @param mixed|null $default |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | public function getOption($option, $default = null) |
||
159 | |||
160 | /** |
||
161 | * Verifies option existence. |
||
162 | * |
||
163 | * @param string $option |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function hasOption($option) |
||
171 | |||
172 | /** |
||
173 | * Set builder options. |
||
174 | * |
||
175 | * @param array $options |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function setOptions(array $options) |
||
187 | |||
188 | /** |
||
189 | * Set builder option. |
||
190 | * |
||
191 | * @param string $option |
||
192 | * @param mixed $value |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function setOption($option, $value) |
||
202 | |||
203 | /** |
||
204 | * Set up annotation metadata. |
||
205 | * |
||
206 | * @throws \InvalidArgumentException |
||
207 | * @throws \RuntimeException |
||
208 | */ |
||
209 | protected function setupAnnotationMetadata() |
||
233 | |||
234 | /** |
||
235 | * Retrieve annotation namespaces. |
||
236 | * |
||
237 | * @return array |
||
238 | */ |
||
239 | protected function getAnnotationNamespaces() |
||
251 | |||
252 | /** |
||
253 | * Create metadata mapping driver. |
||
254 | * |
||
255 | * @throws \RuntimeException |
||
256 | * @throws \UnexpectedValueException |
||
257 | * |
||
258 | * @return MappingDriver |
||
259 | */ |
||
260 | public function getMetadataMappingDriver() |
||
294 | |||
295 | /** |
||
296 | * Retrieve mapping driver. |
||
297 | * |
||
298 | * @param array $metadataMapping |
||
299 | * |
||
300 | * @throws \UnexpectedValueException |
||
301 | * |
||
302 | * @return MappingDriver |
||
303 | */ |
||
304 | protected function getMappingDriver(array $metadataMapping) |
||
347 | |||
348 | /** |
||
349 | * Get annotation metadata driver. |
||
350 | * |
||
351 | * @param array $paths |
||
352 | * |
||
353 | * @return MappingDriver |
||
354 | */ |
||
355 | abstract protected function getAnnotationMetadataDriver(array $paths); |
||
356 | |||
357 | /** |
||
358 | * Get XML metadata driver. |
||
359 | * |
||
360 | * @param array $paths |
||
361 | * @param string|null $extension |
||
362 | * |
||
363 | * @return MappingDriver |
||
364 | */ |
||
365 | abstract protected function getXmlMetadataDriver(array $paths, $extension = null); |
||
366 | |||
367 | /** |
||
368 | * Get YAML metadata driver. |
||
369 | * |
||
370 | * @param array $paths |
||
371 | * @param string|null $extension |
||
372 | * |
||
373 | * @return MappingDriver |
||
374 | */ |
||
375 | abstract protected function getYamlMetadataDriver(array $paths, $extension = null); |
||
376 | |||
377 | /** |
||
378 | * Get PHP metadata driver. |
||
379 | * |
||
380 | * @param array $paths |
||
381 | * |
||
382 | * @return PHPDriver |
||
383 | */ |
||
384 | protected function getPhpMetadataDriver(array $paths) |
||
388 | |||
389 | /** |
||
390 | * Get custom repository factory. |
||
391 | */ |
||
392 | abstract protected function getRepositoryFactory(); |
||
393 | |||
394 | /** |
||
395 | * Get default repository class name. |
||
396 | * |
||
397 | * @return string|null |
||
398 | */ |
||
399 | protected function getDefaultRepositoryClass() |
||
405 | |||
406 | /** |
||
407 | * Retrieve proxies path. |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | protected function getProxiesPath() |
||
415 | |||
416 | /** |
||
417 | * Retrieve proxies namespace. |
||
418 | * |
||
419 | * @return null|string |
||
420 | */ |
||
421 | protected function getProxiesNamespace() |
||
427 | |||
428 | /** |
||
429 | * Retrieve proxy generation strategy. |
||
430 | * |
||
431 | * @return int |
||
432 | */ |
||
433 | protected function getProxiesAutoGeneration() |
||
437 | |||
438 | /** |
||
439 | * Retrieve metadata cache driver. |
||
440 | * |
||
441 | * @throws \InvalidArgumentException |
||
442 | * |
||
443 | * @return CacheProvider |
||
444 | */ |
||
445 | public function getMetadataCacheDriver() |
||
463 | |||
464 | /** |
||
465 | * Retrieve a newly created cache driver. |
||
466 | * |
||
467 | * @return ApcuCache|ArrayCache|MemcacheCache|RedisCache|XcacheCache |
||
468 | */ |
||
469 | private function createNewCacheDriver() |
||
504 | |||
505 | /** |
||
506 | * Set metadata cache driver. |
||
507 | * |
||
508 | * @param CacheProvider $metadataCacheDriver |
||
509 | */ |
||
510 | public function setMetadataCacheDriver(CacheProvider $metadataCacheDriver) |
||
514 | |||
515 | /** |
||
516 | * Retrieve event manager. |
||
517 | * |
||
518 | * @return EventManager |
||
519 | */ |
||
520 | public function getEventManager() |
||
534 | |||
535 | /** |
||
536 | * Set event manager. |
||
537 | * |
||
538 | * @param EventManager $eventManager |
||
539 | */ |
||
540 | public function setEventManager(EventManager $eventManager) |
||
544 | } |
||
545 |