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() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Parse metadata mapping configuration. |
||
| 204 | * |
||
| 205 | * @param MappingDriverChain $metadataDriverChain |
||
| 206 | * |
||
| 207 | * @throws \RuntimeException |
||
| 208 | */ |
||
| 209 | protected function parseMetadataMapping(MappingDriverChain $metadataDriverChain) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Retrieve mapping driver. |
||
| 234 | * |
||
| 235 | * @param array $metadataMapping |
||
| 236 | * |
||
| 237 | * @throws \UnexpectedValueException |
||
| 238 | * |
||
| 239 | * @return MappingDriver |
||
| 240 | */ |
||
| 241 | protected function getMappingDriver(array $metadataMapping) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get metadata mapping driver implementation. |
||
| 272 | * |
||
| 273 | * @param string $type |
||
| 274 | * @param string $paths |
||
| 275 | * @param string $extension |
||
| 276 | * |
||
| 277 | * @throws \UnexpectedValueException |
||
| 278 | * |
||
| 279 | * @return MappingDriver|PHPDriver |
||
| 280 | */ |
||
| 281 | protected function getMappingDriverImplementation($type, $paths, $extension) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get annotation metadata driver. |
||
| 304 | * |
||
| 305 | * @param array $paths |
||
| 306 | * |
||
| 307 | * @return MappingDriver |
||
| 308 | */ |
||
| 309 | abstract protected function getAnnotationMappingDriver(array $paths); |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get XML metadata driver. |
||
| 313 | * |
||
| 314 | * @param array $paths |
||
| 315 | * @param string|null $extension |
||
| 316 | * |
||
| 317 | * @return MappingDriver |
||
| 318 | */ |
||
| 319 | abstract protected function getXmlMappingDriver(array $paths, $extension = null); |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get YAML metadata driver. |
||
| 323 | * |
||
| 324 | * @param array $paths |
||
| 325 | * @param string|null $extension |
||
| 326 | * |
||
| 327 | * @return MappingDriver |
||
| 328 | */ |
||
| 329 | abstract protected function getYamlMappingDriver(array $paths, $extension = null); |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function setMetadataMappingDriver(MappingDriverChain $mappingDriver) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get PHP metadata driver. |
||
| 345 | * |
||
| 346 | * @param array $paths |
||
| 347 | * |
||
| 348 | * @return PHPDriver |
||
| 349 | */ |
||
| 350 | protected function getPhpMappingDriver(array $paths) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get custom repository factory. |
||
| 357 | */ |
||
| 358 | abstract protected function getRepositoryFactory(); |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get default repository class name. |
||
| 362 | * |
||
| 363 | * @return string|null |
||
| 364 | */ |
||
| 365 | protected function getDefaultRepositoryClass() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Retrieve proxies path. |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | protected function getProxiesPath() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Retrieve proxies namespace. |
||
| 384 | * |
||
| 385 | * @return null|string |
||
| 386 | */ |
||
| 387 | protected function getProxiesNamespace() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Retrieve proxy generation strategy. |
||
| 396 | * |
||
| 397 | * @return int |
||
| 398 | */ |
||
| 399 | protected function getProxiesAutoGeneration() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | * |
||
| 407 | * @throws \InvalidArgumentException |
||
| 408 | */ |
||
| 409 | public function getMetadataCacheDriver() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Retrieve a newly created cache driver. |
||
| 430 | * |
||
| 431 | * @return ApcuCache|ArrayCache|MemcacheCache|RedisCache|XcacheCache |
||
| 432 | */ |
||
| 433 | private function createNewCacheDriver() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | * |
||
| 472 | * @return $this |
||
| 473 | */ |
||
| 474 | public function setMetadataCacheDriver(CacheProvider $metadataCacheDriver) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | public function getEventManager() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc} |
||
| 501 | * |
||
| 502 | * @return $this |
||
| 503 | */ |
||
| 504 | public function setEventManager(EventManager $eventManager) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Get event subscribers. |
||
| 513 | * |
||
| 514 | * @return array|null |
||
| 515 | */ |
||
| 516 | protected function getEventSubscribers() |
||
| 532 | } |
||
| 533 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: