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 |
||
35 | class ModelManager implements ModelManagerInterface, LockInterface |
||
36 | { |
||
37 | /** |
||
38 | * @var RegistryInterface |
||
39 | */ |
||
40 | protected $registry; |
||
41 | |||
42 | /** |
||
43 | * @var EntityManager[] |
||
44 | */ |
||
45 | protected $cache = array(); |
||
46 | |||
47 | const ID_SEPARATOR = '~'; |
||
48 | |||
49 | /** |
||
50 | * @param RegistryInterface $registry |
||
51 | */ |
||
52 | public function __construct(RegistryInterface $registry) |
||
56 | |||
57 | /** |
||
58 | * @param string $class |
||
59 | * |
||
60 | * @return ClassMetadata |
||
61 | */ |
||
62 | public function getMetadata($class) |
||
66 | |||
67 | /** |
||
68 | * Returns the model's metadata holding the fully qualified property, and the last |
||
69 | * property name. |
||
70 | * |
||
71 | * @param string $baseClass The base class of the model holding the fully qualified property. |
||
72 | * @param string $propertyFullName The name of the fully qualified property (dot ('.') separated |
||
73 | * property string) |
||
74 | * |
||
75 | * @return array( |
||
|
|||
76 | * \Doctrine\ORM\Mapping\ClassMetadata $parentMetadata, |
||
77 | * string $lastPropertyName, |
||
78 | * array $parentAssociationMappings |
||
79 | * ) |
||
80 | */ |
||
81 | public function getParentMetadataForProperty($baseClass, $propertyFullName) |
||
82 | { |
||
83 | $nameElements = explode('.', $propertyFullName); |
||
84 | $lastPropertyName = array_pop($nameElements); |
||
85 | $class = $baseClass; |
||
86 | $parentAssociationMappings = array(); |
||
87 | |||
88 | foreach ($nameElements as $nameElement) { |
||
89 | $metadata = $this->getMetadata($class); |
||
90 | |||
91 | if (isset($metadata->associationMappings[$nameElement])) { |
||
92 | $parentAssociationMappings[] = $metadata->associationMappings[$nameElement]; |
||
93 | $class = $metadata->getAssociationTargetClass($nameElement); |
||
94 | } elseif (isset($metadata->embeddedClasses[$nameElement])) { |
||
95 | $parentAssociationMappings = array(); |
||
96 | $lastPropertyName = $propertyFullName; |
||
97 | $class = $baseClass; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return array($this->getMetadata($class), $lastPropertyName, $parentAssociationMappings); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $class |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function hasMetadata($class) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function getNewFieldDescriptionInstance($class, $name, array $options = array()) |
||
118 | { |
||
119 | if (!is_string($name)) { |
||
120 | throw new \RunTimeException('The name argument must be a string'); |
||
121 | } |
||
122 | |||
123 | if (!isset($options['route']['name'])) { |
||
124 | $options['route']['name'] = 'edit'; |
||
125 | } |
||
126 | |||
127 | if (!isset($options['route']['parameters'])) { |
||
128 | $options['route']['parameters'] = array(); |
||
129 | } |
||
130 | |||
131 | list($metadata, $propertyName, $parentAssociationMappings) = $this->getParentMetadataForProperty($class, $name); |
||
132 | |||
133 | $fieldDescription = new FieldDescription(); |
||
134 | $fieldDescription->setName($name); |
||
135 | $fieldDescription->setOptions($options); |
||
136 | $fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
137 | |||
138 | if (isset($metadata->associationMappings[$propertyName])) { |
||
139 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$propertyName]); |
||
140 | } |
||
141 | |||
142 | if (isset($metadata->fieldMappings[$propertyName])) { |
||
143 | $fieldDescription->setFieldMapping($metadata->fieldMappings[$propertyName]); |
||
144 | } |
||
145 | |||
146 | return $fieldDescription; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function create($object) |
||
153 | { |
||
154 | try { |
||
155 | $entityManager = $this->getEntityManager($object); |
||
156 | $entityManager->persist($object); |
||
157 | $entityManager->flush(); |
||
158 | } catch (\PDOException $e) { |
||
159 | throw new ModelManagerException(sprintf('Failed to create object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
160 | } catch (DBALException $e) { |
||
161 | throw new ModelManagerException(sprintf('Failed to create object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function update($object) |
||
169 | { |
||
170 | try { |
||
171 | $entityManager = $this->getEntityManager($object); |
||
172 | $entityManager->persist($object); |
||
173 | $entityManager->flush(); |
||
174 | } catch (\PDOException $e) { |
||
175 | throw new ModelManagerException(sprintf('Failed to update object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
176 | } catch (DBALException $e) { |
||
177 | throw new ModelManagerException(sprintf('Failed to update object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function delete($object) |
||
185 | { |
||
186 | try { |
||
187 | $entityManager = $this->getEntityManager($object); |
||
188 | $entityManager->remove($object); |
||
189 | $entityManager->flush(); |
||
190 | } catch (\PDOException $e) { |
||
191 | throw new ModelManagerException(sprintf('Failed to delete object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
192 | } catch (DBALException $e) { |
||
193 | throw new ModelManagerException(sprintf('Failed to delete object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function getLockVersion($object) |
||
201 | { |
||
202 | $metadata = $this->getMetadata(ClassUtils::getClass($object)); |
||
203 | |||
204 | if (!$metadata->isVersioned) { |
||
205 | return; |
||
206 | } |
||
207 | |||
208 | return $metadata->reflFields[$metadata->versionField]->getValue($object); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function lock($object, $expectedVersion) |
||
215 | { |
||
216 | $metadata = $this->getMetadata(ClassUtils::getClass($object)); |
||
217 | |||
218 | if (!$metadata->isVersioned) { |
||
219 | return; |
||
220 | } |
||
221 | |||
222 | try { |
||
223 | $entityManager = $this->getEntityManager($object); |
||
224 | $entityManager->lock($object, LockMode::OPTIMISTIC, $expectedVersion); |
||
225 | } catch (OptimisticLockException $e) { |
||
226 | throw new LockException($e->getMessage(), $e->getCode(), $e); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | public function find($class, $id) |
||
234 | { |
||
235 | if (!isset($id)) { |
||
236 | return; |
||
237 | } |
||
238 | |||
239 | $values = array_combine($this->getIdentifierFieldNames($class), explode(self::ID_SEPARATOR, $id)); |
||
240 | |||
241 | return $this->getEntityManager($class)->getRepository($class)->find($values); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function findBy($class, array $criteria = array()) |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function findOneBy($class, array $criteria = array()) |
||
259 | |||
260 | /** |
||
261 | * @param string $class |
||
262 | * |
||
263 | * @return EntityManager |
||
264 | */ |
||
265 | public function getEntityManager($class) |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function getParentFieldDescription($parentAssociationMapping, $class) |
||
288 | { |
||
289 | $fieldName = $parentAssociationMapping['fieldName']; |
||
290 | |||
291 | $metadata = $this->getMetadata($class); |
||
292 | |||
293 | $associatingMapping = $metadata->associationMappings[$parentAssociationMapping]; |
||
294 | |||
295 | $fieldDescription = $this->getNewFieldDescriptionInstance($class, $fieldName); |
||
296 | $fieldDescription->setName($parentAssociationMapping); |
||
297 | $fieldDescription->setAssociationMapping($associatingMapping); |
||
298 | |||
299 | return $fieldDescription; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | */ |
||
305 | public function createQuery($class, $alias = 'o') |
||
306 | { |
||
307 | $repository = $this->getEntityManager($class)->getRepository($class); |
||
308 | |||
309 | return new ProxyQuery($repository->createQueryBuilder($alias)); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | public function executeQuery($query) |
||
316 | { |
||
317 | if ($query instanceof QueryBuilder) { |
||
318 | return $query->getQuery()->execute(); |
||
319 | } |
||
320 | |||
321 | return $query->execute(); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * {@inheritdoc} |
||
326 | */ |
||
327 | public function getModelIdentifier($class) |
||
331 | |||
332 | /** |
||
333 | * {@inheritdoc} |
||
334 | */ |
||
335 | public function getIdentifierValues($entity) |
||
336 | { |
||
337 | // Fix code has an impact on performance, so disable it ... |
||
338 | //$entityManager = $this->getEntityManager($entity); |
||
339 | //if (!$entityManager->getUnitOfWork()->isInIdentityMap($entity)) { |
||
340 | // throw new \RuntimeException('Entities passed to the choice field must be managed'); |
||
341 | //} |
||
342 | |||
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | public function getIdentifierFieldNames($class) |
||
379 | |||
380 | /** |
||
381 | * {@inheritdoc} |
||
382 | */ |
||
383 | public function getNormalizedIdentifier($entity) |
||
402 | |||
403 | /** |
||
404 | * {@inheritdoc} |
||
405 | * |
||
406 | * The ORM implementation does nothing special but you still should use |
||
407 | * this method when using the id in a URL to allow for future improvements. |
||
408 | */ |
||
409 | public function getUrlsafeIdentifier($entity) |
||
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | */ |
||
417 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function batchDelete($class, ProxyQueryInterface $queryProxy) |
||
468 | |||
469 | /** |
||
470 | * {@inheritdoc} |
||
471 | */ |
||
472 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
||
489 | |||
490 | /** |
||
491 | * {@inheritdoc} |
||
492 | */ |
||
493 | public function getExportFields($class) |
||
499 | |||
500 | /** |
||
501 | * {@inheritdoc} |
||
502 | */ |
||
503 | public function getModelInstance($class) |
||
512 | |||
513 | /** |
||
514 | * {@inheritdoc} |
||
515 | */ |
||
516 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
||
534 | |||
535 | /** |
||
536 | * {@inheritdoc} |
||
537 | */ |
||
538 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
547 | |||
548 | /** |
||
549 | * {@inheritdoc} |
||
550 | */ |
||
551 | public function getDefaultSortValues($class) |
||
560 | |||
561 | /** |
||
562 | * {@inheritdoc} |
||
563 | */ |
||
564 | public function modelTransform($class, $instance) |
||
568 | |||
569 | /** |
||
570 | * {@inheritdoc} |
||
571 | */ |
||
572 | public function modelReverseTransform($class, array $array = array()) |
||
616 | |||
617 | /** |
||
618 | * method taken from PropertyPath. |
||
619 | * |
||
620 | * @param string $property |
||
621 | * |
||
622 | * @return mixed |
||
623 | */ |
||
624 | protected function camelize($property) |
||
628 | |||
629 | /** |
||
630 | * {@inheritdoc} |
||
631 | */ |
||
632 | public function getModelCollectionInstance($class) |
||
636 | |||
637 | /** |
||
638 | * {@inheritdoc} |
||
639 | */ |
||
640 | public function collectionClear(&$collection) |
||
644 | |||
645 | /** |
||
646 | * {@inheritdoc} |
||
647 | */ |
||
648 | public function collectionHasElement(&$collection, &$element) |
||
652 | |||
653 | /** |
||
654 | * {@inheritdoc} |
||
655 | */ |
||
656 | public function collectionAddElement(&$collection, &$element) |
||
660 | |||
661 | /** |
||
662 | * {@inheritdoc} |
||
663 | */ |
||
664 | public function collectionRemoveElement(&$collection, &$element) |
||
668 | } |
||
669 |
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.