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 |
||
| 27 | class ModelManager implements ModelManagerInterface |
||
| 28 | { |
||
| 29 | public const ID_SEPARATOR = '-'; |
||
| 30 | protected $registry; |
||
| 31 | |||
| 32 | public function __construct(ManagerRegistry $registry) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | public function getMetadata($class) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns the model's metadata holding the fully qualified property, and the last |
||
| 47 | * property name. |
||
| 48 | * |
||
| 49 | * @param string $baseClass The base class of the model holding the fully qualified property |
||
| 50 | * @param string $propertyFullName The name of the fully qualified property (dot ('.') separated |
||
| 51 | * property string) |
||
| 52 | * |
||
| 53 | * @return array( |
||
|
|
|||
| 54 | * \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentMetadata, |
||
| 55 | * string $lastPropertyName, |
||
| 56 | * array $parentAssociationMappings |
||
| 57 | * ) |
||
| 58 | */ |
||
| 59 | public function getParentMetadataForProperty($baseClass, $propertyFullName) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public function hasMetadata($class) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function getNewFieldDescriptionInstance($class, $name, array $options = []) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function create($object) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | public function update($object) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function delete($object) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function find($class, $id) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | public function findBy($class, array $criteria = []) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | */ |
||
| 182 | public function findOneBy($class, array $criteria = []) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param object|string $class |
||
| 189 | * |
||
| 190 | * @throw \RuntimeException |
||
| 191 | * |
||
| 192 | * @return \Doctrine\ODM\MongoDB\DocumentManager |
||
| 193 | */ |
||
| 194 | public function getDocumentManager($class) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function getParentFieldDescription($parentAssociationMapping, $class) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | */ |
||
| 230 | public function createQuery($class, $alias = 'o') |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function executeQuery($query) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function getModelIdentifier($class) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function getIdentifierValues($document) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function getIdentifierFieldNames($class) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function getNormalizedIdentifier($document) |
||
| 277 | { |
||
| 278 | if (null === $document) { |
||
| 279 | return null; |
||
| 280 | } |
||
| 281 | |||
| 282 | if (!\is_object($document)) { |
||
| 283 | throw new \RunTimeException('Invalid argument, object or null required'); |
||
| 284 | } |
||
| 285 | |||
| 286 | // the document is not managed |
||
| 287 | if (!$this->getDocumentManager($document)->getUnitOfWork()->isInIdentityMap($document)) { |
||
| 288 | return null; |
||
| 289 | } |
||
| 290 | |||
| 291 | $values = $this->getIdentifierValues($document); |
||
| 292 | |||
| 293 | return implode(self::ID_SEPARATOR, $values); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | public function getUrlSafeIdentifier($document) |
||
| 300 | { |
||
| 301 | return $this->getNormalizedIdentifier($document); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | public function batchDelete($class, ProxyQueryInterface $queryProxy) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * {@inheritdoc} |
||
| 353 | */ |
||
| 354 | public function getExportFields($class) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | public function getModelInstance($class) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * {@inheritdoc} |
||
| 371 | */ |
||
| 372 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | */ |
||
| 394 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * {@inheritdoc} |
||
| 408 | */ |
||
| 409 | public function getDefaultSortValues($class) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritdoc} |
||
| 421 | */ |
||
| 422 | public function modelTransform($class, $instance) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * {@inheritdoc} |
||
| 429 | */ |
||
| 430 | public function modelReverseTransform($class, array $array = []) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * {@inheritdoc} |
||
| 475 | */ |
||
| 476 | public function getModelCollectionInstance($class) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | public function collectionClear(&$collection) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * {@inheritdoc} |
||
| 491 | */ |
||
| 492 | public function collectionHasElement(&$collection, &$element) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritdoc} |
||
| 499 | */ |
||
| 500 | public function collectionAddElement(&$collection, &$element) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * {@inheritdoc} |
||
| 507 | */ |
||
| 508 | public function collectionRemoveElement(&$collection, &$element) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * method taken from PropertyPath. |
||
| 515 | * |
||
| 516 | * @param string $property |
||
| 517 | * |
||
| 518 | * @return mixed |
||
| 519 | */ |
||
| 520 | protected function camelize($property) |
||
| 524 | |||
| 525 | private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool |
||
| 536 | } |
||
| 537 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.