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) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function getMetadata($class) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns the model's metadata holding the fully qualified property, and the last |
||
| 78 | * property name. |
||
| 79 | * |
||
| 80 | * @param string $baseClass The base class of the model holding the fully qualified property |
||
| 81 | * @param string $propertyFullName The name of the fully qualified property (dot ('.') separated |
||
| 82 | * property string) |
||
| 83 | * |
||
| 84 | * @return array( |
||
| 85 | * \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentMetadata, |
||
| 86 | * string $lastPropertyName, |
||
| 87 | * array $parentAssociationMappings |
||
| 88 | * ) |
||
| 89 | */ |
||
| 90 | public function getParentMetadataForProperty($baseClass, $propertyFullName) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function hasMetadata($class) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function getNewFieldDescriptionInstance($class, $name, array $options = []) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function create($object) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function update($object) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | public function delete($object) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function find($class, $id) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public function findBy($class, array $criteria = []) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | public function findOneBy($class, array $criteria = []) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param object|string $class |
||
| 220 | * |
||
| 221 | * @throw \RuntimeException |
||
| 222 | * |
||
| 223 | * @return \Doctrine\ODM\MongoDB\DocumentManager |
||
| 224 | */ |
||
| 225 | public function getDocumentManager($class) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | public function getParentFieldDescription($parentAssociationMapping, $class) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function createQuery($class, $alias = 'o') |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function executeQuery($query) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function getModelIdentifier($class) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * {@inheritdoc} |
||
| 290 | */ |
||
| 291 | public function getIdentifierValues($document) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | public function getIdentifierFieldNames($class) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function getNormalizedIdentifier($document) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | */ |
||
| 330 | public function getUrlSafeIdentifier($document) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritdoc} |
||
| 337 | */ |
||
| 338 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritdoc} |
||
| 346 | */ |
||
| 347 | public function batchDelete($class, ProxyQueryInterface $queryProxy) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * {@inheritdoc} |
||
| 384 | */ |
||
| 385 | public function getExportFields($class) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * {@inheritdoc} |
||
| 394 | */ |
||
| 395 | public function getModelInstance($class) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * {@inheritdoc} |
||
| 417 | */ |
||
| 418 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * {@inheritdoc} |
||
| 439 | */ |
||
| 440 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * {@inheritdoc} |
||
| 454 | */ |
||
| 455 | public function getDefaultSortValues($class) |
||
| 464 | |||
| 465 | public function getDefaultPerPageOptions(string $class): array |
||
| 469 | |||
| 470 | /** |
||
| 471 | * {@inheritdoc} |
||
| 472 | */ |
||
| 473 | public function modelTransform($class, $instance) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * {@inheritdoc} |
||
| 480 | */ |
||
| 481 | public function modelReverseTransform($class, array $array = []) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * {@inheritdoc} |
||
| 497 | */ |
||
| 498 | public function getModelCollectionInstance($class) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * {@inheritdoc} |
||
| 505 | */ |
||
| 506 | public function collectionClear(&$collection) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * {@inheritdoc} |
||
| 513 | */ |
||
| 514 | public function collectionHasElement(&$collection, &$element) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * {@inheritdoc} |
||
| 521 | */ |
||
| 522 | public function collectionAddElement(&$collection, &$element) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * {@inheritdoc} |
||
| 529 | */ |
||
| 530 | public function collectionRemoveElement(&$collection, &$element) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * NEXT_MAJOR: Remove this method. |
||
| 537 | * |
||
| 538 | * @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
||
| 539 | * |
||
| 540 | * @param string $property |
||
| 541 | * |
||
| 542 | * @return mixed |
||
| 543 | */ |
||
| 544 | protected function camelize($property) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * NEXT_MAJOR: Remove CommonClassMetadata and add ClassMetadata as type hint when dropping doctrine/mongodb-odm 1.3.x. |
||
| 556 | * |
||
| 557 | * @param ClassMetadata|CommonClassMetadata $metadata |
||
| 558 | */ |
||
| 559 | private function getFieldName($metadata, string $name): string |
||
| 571 | |||
| 572 | private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool |
||
| 583 | } |
||
| 584 |
If you suppress an error, we recommend checking for the error condition explicitly: