Complex classes like ModelManager 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 ModelManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ModelManager implements ModelManagerInterface |
||
| 32 | { |
||
| 33 | public const ID_SEPARATOR = '-'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ManagerRegistry |
||
| 37 | */ |
||
| 38 | protected $registry; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var PropertyAccessorInterface |
||
| 42 | */ |
||
| 43 | private $propertyAccessor; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * NEXT_MAJOR: Make $propertyAccessor mandatory. |
||
| 47 | */ |
||
| 48 | public function __construct(ManagerRegistry $registry, ?PropertyAccessorInterface $propertyAccessor = null) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | public function getMetadata($class) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns the model's metadata holding the fully qualified property, and the last |
||
| 77 | * property name. |
||
| 78 | * |
||
| 79 | * @param string $baseClass The base class of the model holding the fully qualified property |
||
| 80 | * @param string $propertyFullName The name of the fully qualified property (dot ('.') separated |
||
| 81 | * property string) |
||
| 82 | * |
||
| 83 | * @return array( |
||
| 84 | * \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentMetadata, |
||
| 85 | * string $lastPropertyName, |
||
| 86 | * array $parentAssociationMappings |
||
| 87 | * ) |
||
| 88 | */ |
||
| 89 | public function getParentMetadataForProperty($baseClass, $propertyFullName) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | public function hasMetadata($class) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function getNewFieldDescriptionInstance($class, $name, array $options = []) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function create($object) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | public function update($object) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function delete($object) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | */ |
||
| 182 | public function find($class, $id) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public function findBy($class, array $criteria = []) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function findOneBy($class, array $criteria = []) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param object|string $class |
||
| 219 | * |
||
| 220 | * @throw \RuntimeException |
||
| 221 | * |
||
| 222 | * @return \Doctrine\ODM\MongoDB\DocumentManager |
||
| 223 | */ |
||
| 224 | public function getDocumentManager($class) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function getParentFieldDescription($parentAssociationMapping, $class) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function createQuery($class, $alias = 'o') |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function executeQuery($query) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public function getModelIdentifier($class) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | public function getIdentifierValues($document) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * {@inheritdoc} |
||
| 297 | */ |
||
| 298 | public function getIdentifierFieldNames($class) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * {@inheritdoc} |
||
| 305 | */ |
||
| 306 | public function getNormalizedIdentifier($document) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * {@inheritdoc} |
||
| 328 | */ |
||
| 329 | public function getUrlSafeIdentifier($document) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritdoc} |
||
| 336 | */ |
||
| 337 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * {@inheritdoc} |
||
| 345 | */ |
||
| 346 | public function batchDelete($class, ProxyQueryInterface $queryProxy) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * {@inheritdoc} |
||
| 369 | */ |
||
| 370 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function getExportFields($class) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | */ |
||
| 394 | public function getModelInstance($class) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * {@inheritdoc} |
||
| 416 | */ |
||
| 417 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * {@inheritdoc} |
||
| 438 | */ |
||
| 439 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * {@inheritdoc} |
||
| 453 | */ |
||
| 454 | public function getDefaultSortValues($class) |
||
| 463 | |||
| 464 | public function getDefaultPerPageOptions(string $class): array |
||
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | public function modelTransform($class, $instance) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritdoc} |
||
| 479 | */ |
||
| 480 | public function modelReverseTransform($class, array $array = []) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * {@inheritdoc} |
||
| 496 | */ |
||
| 497 | public function getModelCollectionInstance($class) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * {@inheritdoc} |
||
| 504 | */ |
||
| 505 | public function collectionClear(&$collection) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * {@inheritdoc} |
||
| 512 | */ |
||
| 513 | public function collectionHasElement(&$collection, &$element) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * {@inheritdoc} |
||
| 520 | */ |
||
| 521 | public function collectionAddElement(&$collection, &$element) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * {@inheritdoc} |
||
| 528 | */ |
||
| 529 | public function collectionRemoveElement(&$collection, &$element) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * NEXT_MAJOR: Remove this method. |
||
| 536 | * |
||
| 537 | * @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
||
| 538 | * |
||
| 539 | * @param string $property |
||
| 540 | * |
||
| 541 | * @return mixed |
||
| 542 | */ |
||
| 543 | protected function camelize($property) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * NEXT_MAJOR: Remove CommonClassMetadata when dropping doctrine/mongodb-odm 1.3.x. |
||
| 555 | * |
||
| 556 | * @param ClassMetadata|CommonClassMetadata $metadata |
||
| 557 | */ |
||
| 558 | private function getFieldName($metadata, string $name): string |
||
| 570 | |||
| 571 | private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool |
||
| 582 | } |
||
| 583 |
If you suppress an error, we recommend checking for the error condition explicitly: