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 | ||
| 41 | class ModelManager implements ModelManagerInterface, LockInterface | ||
| 42 | { | ||
| 43 | public const ID_SEPARATOR = '~'; | ||
| 44 | /** | ||
| 45 | * @var ManagerRegistry | ||
| 46 | */ | ||
| 47 | protected $registry; | ||
| 48 | |||
| 49 | /** | ||
| 50 | * @var EntityManager[] | ||
| 51 | */ | ||
| 52 | protected $cache = []; | ||
| 53 | |||
| 54 | public function __construct(ManagerRegistry $registry) | ||
| 55 |     { | ||
| 56 | $this->registry = $registry; | ||
| 57 | } | ||
| 58 | |||
| 59 | /** | ||
| 60 | * @param string $class | ||
| 61 | * | ||
| 62 | * @return ClassMetadata | ||
| 63 | */ | ||
| 64 | public function getMetadata($class) | ||
| 65 |     { | ||
| 66 | return $this->getEntityManager($class)->getMetadataFactory()->getMetadataFor($class); | ||
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Returns the model's metadata holding the fully qualified property, and the last | ||
| 71 | * property name. | ||
| 72 | * | ||
| 73 | * @param string $baseClass The base class of the model holding the fully qualified property | ||
| 74 |      * @param string $propertyFullName The name of the fully qualified property (dot ('.') separated | ||
| 75 | * property string) | ||
| 76 | * | ||
| 77 | * @return array( | ||
|  | |||
| 78 | * \Doctrine\ORM\Mapping\ClassMetadata $parentMetadata, | ||
| 79 | * string $lastPropertyName, | ||
| 80 | * array $parentAssociationMappings | ||
| 81 | * ) | ||
| 82 | */ | ||
| 83 | public function getParentMetadataForProperty($baseClass, $propertyFullName) | ||
| 84 |     { | ||
| 85 |         $nameElements = explode('.', $propertyFullName); | ||
| 86 | $lastPropertyName = array_pop($nameElements); | ||
| 87 | $class = $baseClass; | ||
| 88 | $parentAssociationMappings = []; | ||
| 89 | |||
| 90 |         foreach ($nameElements as $nameElement) { | ||
| 91 | $metadata = $this->getMetadata($class); | ||
| 92 | |||
| 93 |             if (isset($metadata->associationMappings[$nameElement])) { | ||
| 94 | $parentAssociationMappings[] = $metadata->associationMappings[$nameElement]; | ||
| 95 | $class = $metadata->getAssociationTargetClass($nameElement); | ||
| 96 | |||
| 97 | continue; | ||
| 98 | } | ||
| 99 | |||
| 100 | break; | ||
| 101 | } | ||
| 102 | |||
| 103 | $properties = \array_slice($nameElements, \count($parentAssociationMappings)); | ||
| 104 | $properties[] = $lastPropertyName; | ||
| 105 | |||
| 106 |         return [$this->getMetadata($class), implode('.', $properties), $parentAssociationMappings]; | ||
| 107 | } | ||
| 108 | |||
| 109 | /** | ||
| 110 | * @param string $class | ||
| 111 | * | ||
| 112 | * @return bool | ||
| 113 | */ | ||
| 114 | public function hasMetadata($class) | ||
| 115 |     { | ||
| 116 | return $this->getEntityManager($class)->getMetadataFactory()->hasMetadataFor($class); | ||
| 117 | } | ||
| 118 | |||
| 119 | public function getNewFieldDescriptionInstance($class, $name, array $options = []) | ||
| 120 |     { | ||
| 121 |         if (!\is_string($name)) { | ||
| 122 |             throw new \RuntimeException('The name argument must be a string'); | ||
| 123 | } | ||
| 124 | |||
| 125 |         if (!isset($options['route']['name'])) { | ||
| 126 | $options['route']['name'] = 'edit'; | ||
| 127 | } | ||
| 128 | |||
| 129 |         if (!isset($options['route']['parameters'])) { | ||
| 130 | $options['route']['parameters'] = []; | ||
| 131 | } | ||
| 132 | |||
| 133 | list($metadata, $propertyName, $parentAssociationMappings) = $this->getParentMetadataForProperty($class, $name); | ||
| 134 | |||
| 135 | $fieldDescription = new FieldDescription(); | ||
| 136 | $fieldDescription->setName($name); | ||
| 137 | $fieldDescription->setOptions($options); | ||
| 138 | $fieldDescription->setParentAssociationMappings($parentAssociationMappings); | ||
| 139 | |||
| 140 |         if (isset($metadata->associationMappings[$propertyName])) { | ||
| 141 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$propertyName]); | ||
| 142 | } | ||
| 143 | |||
| 144 |         if (isset($metadata->fieldMappings[$propertyName])) { | ||
| 145 | $fieldDescription->setFieldMapping($metadata->fieldMappings[$propertyName]); | ||
| 146 | } | ||
| 147 | |||
| 148 | return $fieldDescription; | ||
| 149 | } | ||
| 150 | |||
| 151 | public function create($object) | ||
| 152 |     { | ||
| 153 |         try { | ||
| 154 | $entityManager = $this->getEntityManager($object); | ||
| 155 | $entityManager->persist($object); | ||
| 156 | $entityManager->flush(); | ||
| 157 |         } catch (\PDOException $e) { | ||
| 158 | throw new ModelManagerException( | ||
| 159 |                 sprintf('Failed to create object: %s', ClassUtils::getClass($object)), | ||
| 160 | $e->getCode(), | ||
| 161 | $e | ||
| 162 | ); | ||
| 163 |         } catch (DBALException $e) { | ||
| 164 | throw new ModelManagerException( | ||
| 165 |                 sprintf('Failed to create object: %s', ClassUtils::getClass($object)), | ||
| 166 | $e->getCode(), | ||
| 167 | $e | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | public function update($object) | ||
| 173 |     { | ||
| 174 |         try { | ||
| 175 | $entityManager = $this->getEntityManager($object); | ||
| 176 | $entityManager->persist($object); | ||
| 177 | $entityManager->flush(); | ||
| 178 |         } catch (\PDOException $e) { | ||
| 179 | throw new ModelManagerException( | ||
| 180 |                 sprintf('Failed to update object: %s', ClassUtils::getClass($object)), | ||
| 181 | $e->getCode(), | ||
| 182 | $e | ||
| 183 | ); | ||
| 184 |         } catch (DBALException $e) { | ||
| 185 | throw new ModelManagerException( | ||
| 186 |                 sprintf('Failed to update object: %s', ClassUtils::getClass($object)), | ||
| 187 | $e->getCode(), | ||
| 188 | $e | ||
| 189 | ); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | public function delete($object) | ||
| 194 |     { | ||
| 195 |         try { | ||
| 196 | $entityManager = $this->getEntityManager($object); | ||
| 197 | $entityManager->remove($object); | ||
| 198 | $entityManager->flush(); | ||
| 199 |         } catch (\PDOException $e) { | ||
| 200 | throw new ModelManagerException( | ||
| 201 |                 sprintf('Failed to delete object: %s', ClassUtils::getClass($object)), | ||
| 202 | $e->getCode(), | ||
| 203 | $e | ||
| 204 | ); | ||
| 205 |         } catch (DBALException $e) { | ||
| 206 | throw new ModelManagerException( | ||
| 207 |                 sprintf('Failed to delete object: %s', ClassUtils::getClass($object)), | ||
| 208 | $e->getCode(), | ||
| 209 | $e | ||
| 210 | ); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | public function getLockVersion($object) | ||
| 215 |     { | ||
| 216 | $metadata = $this->getMetadata(ClassUtils::getClass($object)); | ||
| 217 | |||
| 218 |         if (!$metadata->isVersioned) { | ||
| 219 | return null; | ||
| 220 | } | ||
| 221 | |||
| 222 | return $metadata->reflFields[$metadata->versionField]->getValue($object); | ||
| 223 | } | ||
| 224 | |||
| 225 | public function lock($object, $expectedVersion) | ||
| 226 |     { | ||
| 227 | $metadata = $this->getMetadata(ClassUtils::getClass($object)); | ||
| 228 | |||
| 229 |         if (!$metadata->isVersioned) { | ||
| 230 | return; | ||
| 231 | } | ||
| 232 | |||
| 233 |         try { | ||
| 234 | $entityManager = $this->getEntityManager($object); | ||
| 235 | $entityManager->lock($object, LockMode::OPTIMISTIC, $expectedVersion); | ||
| 236 |         } catch (OptimisticLockException $e) { | ||
| 237 | throw new LockException($e->getMessage(), $e->getCode(), $e); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | public function find($class, $id) | ||
| 242 |     { | ||
| 243 |         if (!isset($id)) { | ||
| 244 | return null; | ||
| 245 | } | ||
| 246 | |||
| 247 | $values = array_combine($this->getIdentifierFieldNames($class), explode(self::ID_SEPARATOR, (string) $id)); | ||
| 248 | |||
| 249 | return $this->getEntityManager($class)->getRepository($class)->find($values); | ||
| 250 | } | ||
| 251 | |||
| 252 | public function findBy($class, array $criteria = []) | ||
| 253 |     { | ||
| 254 | return $this->getEntityManager($class)->getRepository($class)->findBy($criteria); | ||
| 255 | } | ||
| 256 | |||
| 257 | public function findOneBy($class, array $criteria = []) | ||
| 258 |     { | ||
| 259 | return $this->getEntityManager($class)->getRepository($class)->findOneBy($criteria); | ||
| 260 | } | ||
| 261 | |||
| 262 | /** | ||
| 263 | * @param string $class | ||
| 264 | * | ||
| 265 | * @return EntityManager | ||
| 266 | */ | ||
| 267 | public function getEntityManager($class) | ||
| 268 |     { | ||
| 269 |         if (\is_object($class)) { | ||
| 270 | $class = \get_class($class); | ||
| 271 | } | ||
| 272 | |||
| 273 |         if (!isset($this->cache[$class])) { | ||
| 274 | $em = $this->registry->getManagerForClass($class); | ||
| 275 | |||
| 276 |             if (!$em) { | ||
| 277 |                 throw new \RuntimeException(sprintf('No entity manager defined for class %s', $class)); | ||
| 278 | } | ||
| 279 | |||
| 280 | $this->cache[$class] = $em; | ||
| 281 | } | ||
| 282 | |||
| 283 | return $this->cache[$class]; | ||
| 284 | } | ||
| 285 | |||
| 286 | public function getParentFieldDescription($parentAssociationMapping, $class) | ||
| 287 |     { | ||
| 288 | $fieldName = $parentAssociationMapping['fieldName']; | ||
| 289 | |||
| 290 | $metadata = $this->getMetadata($class); | ||
| 291 | |||
| 292 | $associatingMapping = $metadata->associationMappings[$parentAssociationMapping]; | ||
| 293 | |||
| 294 | $fieldDescription = $this->getNewFieldDescriptionInstance($class, $fieldName); | ||
| 295 | $fieldDescription->setName($parentAssociationMapping); | ||
| 296 | $fieldDescription->setAssociationMapping($associatingMapping); | ||
| 297 | |||
| 298 | return $fieldDescription; | ||
| 299 | } | ||
| 300 | |||
| 301 | public function createQuery($class, $alias = 'o') | ||
| 302 |     { | ||
| 303 | $repository = $this->getEntityManager($class)->getRepository($class); | ||
| 304 | |||
| 305 | return new ProxyQuery($repository->createQueryBuilder($alias)); | ||
| 306 | } | ||
| 307 | |||
| 308 | public function executeQuery($query) | ||
| 309 |     { | ||
| 310 |         if ($query instanceof QueryBuilder) { | ||
| 311 | return $query->getQuery()->execute(); | ||
| 312 | } | ||
| 313 | |||
| 314 | return $query->execute(); | ||
| 315 | } | ||
| 316 | |||
| 317 | /** | ||
| 318 | * NEXT_MAJOR: Remove this function. | ||
| 319 | * | ||
| 320 | * @deprecated since sonata-project/doctrine-orm-admin-bundle 3.x. To be removed in 4.0. | ||
| 321 | */ | ||
| 322 | public function getModelIdentifier($class) | ||
| 323 |     { | ||
| 324 | return $this->getMetadata($class)->identifier; | ||
| 325 | } | ||
| 326 | |||
| 327 | public function getIdentifierValues($entity) | ||
| 328 |     { | ||
| 329 | // Fix code has an impact on performance, so disable it ... | ||
| 330 | //$entityManager = $this->getEntityManager($entity); | ||
| 331 |         //if (!$entityManager->getUnitOfWork()->isInIdentityMap($entity)) { | ||
| 332 |         //    throw new \RuntimeException('Entities passed to the choice field must be managed'); | ||
| 333 | //} | ||
| 334 | |||
| 335 | $class = ClassUtils::getClass($entity); | ||
| 336 | $metadata = $this->getMetadata($class); | ||
| 337 | $platform = $this->getEntityManager($class)->getConnection()->getDatabasePlatform(); | ||
| 338 | |||
| 339 | $identifiers = []; | ||
| 340 | |||
| 341 |         foreach ($metadata->getIdentifierValues($entity) as $name => $value) { | ||
| 342 |             if (!\is_object($value)) { | ||
| 343 | $identifiers[] = $value; | ||
| 344 | |||
| 345 | continue; | ||
| 346 | } | ||
| 347 | |||
| 348 | $fieldType = $metadata->getTypeOfField($name); | ||
| 349 | $type = $fieldType && Type::hasType($fieldType) ? Type::getType($fieldType) : null; | ||
| 350 |             if ($type) { | ||
| 351 | $identifiers[] = $this->getValueFromType($value, $type, $fieldType, $platform); | ||
| 352 | |||
| 353 | continue; | ||
| 354 | } | ||
| 355 | |||
| 356 | $identifierMetadata = $this->getMetadata(ClassUtils::getClass($value)); | ||
| 357 | |||
| 358 |             foreach ($identifierMetadata->getIdentifierValues($value) as $value) { | ||
| 359 | $identifiers[] = $value; | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | return $identifiers; | ||
| 364 | } | ||
| 365 | |||
| 366 | public function getIdentifierFieldNames($class) | ||
| 370 | |||
| 371 | public function getNormalizedIdentifier($entity) | ||
| 396 | |||
| 397 | /** | ||
| 398 |      * {@inheritdoc} | ||
| 399 | * | ||
| 400 | * The ORM implementation does nothing special but you still should use | ||
| 401 | * this method when using the id in a URL to allow for future improvements. | ||
| 402 | */ | ||
| 403 | public function getUrlSafeIdentifier($entity) | ||
| 404 |     { | ||
| 405 | return $this->getNormalizedIdentifier($entity); | ||
| 406 | } | ||
| 407 | |||
| 408 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) | ||
| 430 | |||
| 431 | public function batchDelete($class, ProxyQueryInterface $queryProxy) | ||
| 456 | |||
| 457 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) | ||
| 480 | |||
| 481 | public function getExportFields($class) | ||
| 487 | |||
| 488 | public function getModelInstance($class) | ||
| 503 | |||
| 504 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) | ||
| 505 |     { | ||
| 506 | $values = $datagrid->getValues(); | ||
| 507 | |||
| 508 |         if ($this->isFieldAlreadySorted($fieldDescription, $datagrid)) { | ||
| 509 |             if ('ASC' === $values['_sort_order']) { | ||
| 510 | $values['_sort_order'] = 'DESC'; | ||
| 511 |             } else { | ||
| 512 | $values['_sort_order'] = 'ASC'; | ||
| 513 | } | ||
| 514 |         } else { | ||
| 515 | $values['_sort_order'] = 'ASC'; | ||
| 516 | } | ||
| 517 | |||
| 518 |         $values['_sort_by'] = \is_string($fieldDescription->getOption('sortable')) ? $fieldDescription->getOption('sortable') : $fieldDescription->getName(); | ||
| 519 | |||
| 520 | return ['filter' => $values]; | ||
| 521 | } | ||
| 522 | |||
| 523 | public function getPaginationParameters(DatagridInterface $datagrid, $page) | ||
| 534 | |||
| 535 | public function getDefaultSortValues($class) | ||
| 544 | |||
| 545 | public function getDefaultPerPageOptions(string $class): array | ||
| 546 |     { | ||
| 547 | return [10, 25, 50, 100, 250]; | ||
| 548 | } | ||
| 549 | |||
| 550 | public function modelTransform($class, $instance) | ||
| 551 |     { | ||
| 552 | return $instance; | ||
| 553 | } | ||
| 554 | |||
| 555 | public function modelReverseTransform($class, array $array = []) | ||
| 556 |     { | ||
| 557 | $instance = $this->getModelInstance($class); | ||
| 558 | $metadata = $this->getMetadata($class); | ||
| 559 | |||
| 560 | $reflClass = $metadata->reflClass; | ||
| 561 |         foreach ($array as $name => $value) { | ||
| 562 | $reflection_property = false; | ||
| 563 | // property or association ? | ||
| 564 |             if (\array_key_exists($name, $metadata->fieldMappings)) { | ||
| 565 | $property = $metadata->fieldMappings[$name]['fieldName']; | ||
| 566 | $reflection_property = $metadata->reflFields[$name]; | ||
| 567 |             } elseif (\array_key_exists($name, $metadata->associationMappings)) { | ||
| 568 | $property = $metadata->associationMappings[$name]['fieldName']; | ||
| 569 |             } else { | ||
| 570 | $property = $name; | ||
| 571 | } | ||
| 572 | |||
| 573 | $setter = 'set'.$this->camelize($name); | ||
| 574 | |||
| 575 |             if ($reflClass->hasMethod($setter)) { | ||
| 576 |                 if (!$reflClass->getMethod($setter)->isPublic()) { | ||
| 577 | throw new PropertyAccessDeniedException(sprintf( | ||
| 578 | 'Method "%s()" is not public in class "%s"', | ||
| 579 | $setter, | ||
| 580 | $reflClass->getName() | ||
| 581 | )); | ||
| 582 | } | ||
| 583 | |||
| 584 | $instance->$setter($value); | ||
| 585 |             } elseif ($reflClass->hasMethod('__set')) { | ||
| 586 | // needed to support magic method __set | ||
| 587 | $instance->$property = $value; | ||
| 588 |             } elseif ($reflClass->hasProperty($property)) { | ||
| 589 |                 if (!$reflClass->getProperty($property)->isPublic()) { | ||
| 590 | throw new PropertyAccessDeniedException(sprintf( | ||
| 591 | 'Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?', | ||
| 592 | $property, | ||
| 593 | $reflClass->getName(), | ||
| 594 | ucfirst($property) | ||
| 595 | )); | ||
| 596 | } | ||
| 597 | |||
| 598 | $instance->$property = $value; | ||
| 599 |             } elseif ($reflection_property) { | ||
| 600 | $reflection_property->setValue($instance, $value); | ||
| 601 | } | ||
| 602 | } | ||
| 603 | |||
| 604 | return $instance; | ||
| 605 | } | ||
| 606 | |||
| 607 | public function getModelCollectionInstance($class) | ||
| 608 |     { | ||
| 609 | return new \Doctrine\Common\Collections\ArrayCollection(); | ||
| 610 | } | ||
| 611 | |||
| 612 | public function collectionClear(&$collection) | ||
| 613 |     { | ||
| 614 | return $collection->clear(); | ||
| 615 | } | ||
| 616 | |||
| 617 | public function collectionHasElement(&$collection, &$element) | ||
| 618 |     { | ||
| 619 | return $collection->contains($element); | ||
| 620 | } | ||
| 621 | |||
| 622 | public function collectionAddElement(&$collection, &$element) | ||
| 623 |     { | ||
| 624 | return $collection->add($element); | ||
| 625 | } | ||
| 626 | |||
| 627 | public function collectionRemoveElement(&$collection, &$element) | ||
| 628 |     { | ||
| 629 | return $collection->removeElement($element); | ||
| 630 | } | ||
| 631 | |||
| 632 | /** | ||
| 633 | * method taken from Symfony\Component\PropertyAccess\PropertyAccessor. | ||
| 634 | * | ||
| 635 | * @param string $property | ||
| 636 | * | ||
| 637 | * @return mixed | ||
| 638 | */ | ||
| 639 | protected function camelize($property) | ||
| 640 |     { | ||
| 641 |         return str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); | ||
| 642 | } | ||
| 643 | |||
| 644 | private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool | ||
| 655 | |||
| 656 | /** | ||
| 657 | * @param mixed $value | ||
| 658 | */ | ||
| 659 | private function getValueFromType($value, Type $type, string $fieldType, AbstractPlatform $platform): string | ||
| 679 | } | ||
| 680 | 
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.