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 | use OptionsTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Manager builder's common default options. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $defaultOptions = [ |
||
| 40 | 'proxies_auto_generation' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
||
| 41 | ]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Builder name. |
||
| 45 | * |
||
| 46 | * @var |
||
| 47 | */ |
||
| 48 | protected $name; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Metadata mapping driver. |
||
| 52 | * |
||
| 53 | * @var MappingDriverChain |
||
| 54 | */ |
||
| 55 | protected $mappingDriver; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * General cache driver. |
||
| 59 | * |
||
| 60 | * @var CacheProvider |
||
| 61 | */ |
||
| 62 | protected $cacheDriver; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Metadata cache driver. |
||
| 66 | * |
||
| 67 | * @var CacheProvider |
||
| 68 | */ |
||
| 69 | protected $metadataCacheDriver; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Event manager. |
||
| 73 | * |
||
| 74 | * @var EventManager |
||
| 75 | */ |
||
| 76 | protected $eventManager; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * ManagerBuilder constructor. |
||
| 80 | * |
||
| 81 | * @param array $options |
||
| 82 | * @param string|null $name |
||
| 83 | */ |
||
| 84 | public function __construct(array $options = [], $name = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get manager builder's default options. |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | abstract protected function getDefaultOptions(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Unset created objects for rebuild. |
||
| 99 | */ |
||
| 100 | protected function wipe() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | * |
||
| 111 | * @return string|null |
||
| 112 | */ |
||
| 113 | public function getName() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | public function setName($name = null) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set up annotation metadata. |
||
| 132 | * |
||
| 133 | * @throws \InvalidArgumentException |
||
| 134 | * @throws \RuntimeException |
||
| 135 | */ |
||
| 136 | protected function setupAnnotationMetadata() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Retrieve annotation namespaces. |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | protected function getAnnotationNamespaces() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | * |
||
| 182 | * @throws \RuntimeException |
||
| 183 | * @throws \UnexpectedValueException |
||
| 184 | */ |
||
| 185 | public function getMetadataMappingDriver() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Retrieve mapping driver. |
||
| 222 | * |
||
| 223 | * @param array $metadataMapping |
||
| 224 | * |
||
| 225 | * @throws \UnexpectedValueException |
||
| 226 | * |
||
| 227 | * @return MappingDriver |
||
| 228 | */ |
||
| 229 | protected function getMappingDriver(array $metadataMapping) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get annotation metadata driver. |
||
| 275 | * |
||
| 276 | * @param array $paths |
||
| 277 | * |
||
| 278 | * @return MappingDriver |
||
| 279 | */ |
||
| 280 | abstract protected function getAnnotationMetadataDriver(array $paths); |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get XML metadata driver. |
||
| 284 | * |
||
| 285 | * @param array $paths |
||
| 286 | * @param string|null $extension |
||
| 287 | * |
||
| 288 | * @return MappingDriver |
||
| 289 | */ |
||
| 290 | abstract protected function getXmlMetadataDriver(array $paths, $extension = null); |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get YAML metadata driver. |
||
| 294 | * |
||
| 295 | * @param array $paths |
||
| 296 | * @param string|null $extension |
||
| 297 | * |
||
| 298 | * @return MappingDriver |
||
| 299 | */ |
||
| 300 | abstract protected function getYamlMetadataDriver(array $paths, $extension = null); |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | * |
||
| 305 | * @return $this |
||
| 306 | */ |
||
| 307 | public function setMetadataMappingDriver(MappingDriverChain $mappingDriver) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get PHP metadata driver. |
||
| 316 | * |
||
| 317 | * @param array $paths |
||
| 318 | * |
||
| 319 | * @return PHPDriver |
||
| 320 | */ |
||
| 321 | protected function getPhpMetadataDriver(array $paths) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get custom repository factory. |
||
| 328 | */ |
||
| 329 | abstract protected function getRepositoryFactory(); |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get default repository class name. |
||
| 333 | * |
||
| 334 | * @return string|null |
||
| 335 | */ |
||
| 336 | protected function getDefaultRepositoryClass() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Retrieve proxies path. |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | protected function getProxiesPath() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Retrieve proxies namespace. |
||
| 355 | * |
||
| 356 | * @return null|string |
||
| 357 | */ |
||
| 358 | protected function getProxiesNamespace() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Retrieve proxy generation strategy. |
||
| 367 | * |
||
| 368 | * @return int |
||
| 369 | */ |
||
| 370 | protected function getProxiesAutoGeneration() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * {@inheritdoc} |
||
| 377 | * |
||
| 378 | * @throws \InvalidArgumentException |
||
| 379 | */ |
||
| 380 | public function getMetadataCacheDriver() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Retrieve a newly created cache driver. |
||
| 401 | * |
||
| 402 | * @return ApcuCache|ArrayCache|MemcacheCache|RedisCache|XcacheCache |
||
| 403 | */ |
||
| 404 | private function createNewCacheDriver() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * {@inheritdoc} |
||
| 442 | * |
||
| 443 | * @return $this |
||
| 444 | */ |
||
| 445 | public function setMetadataCacheDriver(CacheProvider $metadataCacheDriver) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * {@inheritdoc} |
||
| 454 | */ |
||
| 455 | public function getEventManager() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * {@inheritdoc} |
||
| 472 | * |
||
| 473 | * @return $this |
||
| 474 | */ |
||
| 475 | public function setEventManager(EventManager $eventManager) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get event subscribers. |
||
| 484 | * |
||
| 485 | * @return array|null |
||
| 486 | */ |
||
| 487 | protected function getEventSubscribers() |
||
| 503 | } |
||
| 504 |