Complex classes like MongoDBBuilder 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 MongoDBBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class MongoDBBuilder extends AbstractManagerBuilder |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Logger callable. |
||
| 35 | * |
||
| 36 | * @var callable |
||
| 37 | */ |
||
| 38 | protected $loggerCallable; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | protected function getDefaultOptions() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | protected function wipe() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | * |
||
| 72 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 73 | * @throws \InvalidArgumentException |
||
| 74 | * @throws \RuntimeException |
||
| 75 | * @throws \UnexpectedValueException |
||
| 76 | * |
||
| 77 | * @return DocumentManager |
||
| 78 | */ |
||
| 79 | protected function buildManager() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set up general manager configurations. |
||
| 101 | * |
||
| 102 | * @param Configuration $config |
||
| 103 | */ |
||
| 104 | protected function setUpGeneralConfigurations(Configuration $config) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set up manager specific configurations. |
||
| 126 | * |
||
| 127 | * @param Configuration $config |
||
| 128 | */ |
||
| 129 | protected function setUpSpecificConfigurations(Configuration $config) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Create MongoDB Connection. |
||
| 154 | * |
||
| 155 | * @param Configuration $config |
||
| 156 | * |
||
| 157 | * @throws \InvalidArgumentException |
||
| 158 | * @throws \RuntimeException |
||
| 159 | * |
||
| 160 | * @return Connection |
||
| 161 | */ |
||
| 162 | protected function getConnection(Configuration $config) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | protected function getAnnotationMappingDriver(array $paths) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | protected function getXmlMappingDriver(array $paths, $extension = null) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | protected function getYamlMappingDriver(array $paths, $extension = null) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | * |
||
| 222 | * @throws \InvalidArgumentException |
||
| 223 | * |
||
| 224 | * @return RepositoryFactory|null |
||
| 225 | */ |
||
| 226 | protected function getRepositoryFactory() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Retrieve hydrators path. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | protected function getHydratorsPath() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Retrieve hydrators namespace. |
||
| 256 | * |
||
| 257 | * @return null|string |
||
| 258 | */ |
||
| 259 | protected function getHydratorsNamespace() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Retrieve hydrators generation strategy. |
||
| 268 | * |
||
| 269 | * @return int |
||
| 270 | */ |
||
| 271 | protected function getHydratorsAutoGeneration() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Retrieve persistent collections path. |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | protected function getPersistentCollectionPath() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Retrieve persistent collections namespace. |
||
| 288 | * |
||
| 289 | * @return null|string |
||
| 290 | */ |
||
| 291 | protected function getPersistentCollectionNamespace() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Retrieve persistent collections generation strategy. |
||
| 300 | * |
||
| 301 | * @return int |
||
| 302 | */ |
||
| 303 | protected function getAutoGeneratePersistentCollection() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get default database. |
||
| 310 | * |
||
| 311 | * @return string|null |
||
| 312 | */ |
||
| 313 | protected function getDefaultDatabase() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Retrieve logger callable. |
||
| 320 | * |
||
| 321 | * @return callable|null |
||
| 322 | */ |
||
| 323 | protected function getLoggerCallable() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get custom registered filters. |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | protected function getCustomFilters() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * {@inheritdoc} |
||
| 356 | * |
||
| 357 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 358 | * @throws \InvalidArgumentException |
||
| 359 | * @throws \RuntimeException |
||
| 360 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
| 361 | * @throws \Symfony\Component\Console\Exception\LogicException |
||
| 362 | * @throws \UnexpectedValueException |
||
| 363 | * |
||
| 364 | * @return Command[] |
||
| 365 | */ |
||
| 366 | public function getConsoleCommands() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | * |
||
| 407 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 408 | * @throws \InvalidArgumentException |
||
| 409 | * @throws \RuntimeException |
||
| 410 | * @throws \UnexpectedValueException |
||
| 411 | */ |
||
| 412 | public function getConsoleHelperSet() |
||
| 418 | } |
||
| 419 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.