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): void |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function update($object): void |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function delete($object): void |
||
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): void |
||
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | public function batchDelete($class, ProxyQueryInterface $queryProxy): void |
||
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) |
||
373 | { |
||
374 | $values = $datagrid->getValues(); |
||
375 | |||
376 | if ($this->isFieldAlreadySorted($fieldDescription, $datagrid)) { |
||
377 | if ('ASC' === $values['_sort_order']) { |
||
378 | $values['_sort_order'] = 'DESC'; |
||
379 | } else { |
||
380 | $values['_sort_order'] = 'ASC'; |
||
381 | } |
||
382 | } else { |
||
383 | $values['_sort_order'] = 'ASC'; |
||
384 | } |
||
385 | |||
386 | $values['_sort_by'] = \is_string($fieldDescription->getOption('sortable')) ? $fieldDescription->getOption('sortable') : $fieldDescription->getName(); |
||
387 | |||
388 | return ['filter' => $values]; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * {@inheritdoc} |
||
393 | */ |
||
394 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
395 | { |
||
396 | $values = $datagrid->getValues(); |
||
397 | |||
398 | if (isset($values['_sort_by']) && $values['_sort_by'] instanceof FieldDescriptionInterface) { |
||
399 | $values['_sort_by'] = $values['_sort_by']->getName(); |
||
400 | } |
||
401 | $values['_page'] = $page; |
||
402 | |||
403 | return ['filter' => $values]; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | public function getDefaultSortValues($class) |
||
410 | { |
||
411 | return [ |
||
412 | '_page' => 1, |
||
413 | '_per_page' => 25, |
||
414 | ]; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | public function modelTransform($class, $instance) |
||
424 | |||
425 | /** |
||
426 | * {@inheritdoc} |
||
427 | */ |
||
428 | public function modelReverseTransform($class, array $array = []) |
||
470 | |||
471 | /** |
||
472 | * {@inheritdoc} |
||
473 | */ |
||
474 | public function getModelCollectionInstance($class) |
||
478 | |||
479 | /** |
||
480 | * {@inheritdoc} |
||
481 | */ |
||
482 | public function collectionClear(&$collection) |
||
486 | |||
487 | /** |
||
488 | * {@inheritdoc} |
||
489 | */ |
||
490 | public function collectionHasElement(&$collection, &$element) |
||
494 | |||
495 | /** |
||
496 | * {@inheritdoc} |
||
497 | */ |
||
498 | public function collectionAddElement(&$collection, &$element) |
||
502 | |||
503 | /** |
||
504 | * {@inheritdoc} |
||
505 | */ |
||
506 | public function collectionRemoveElement(&$collection, &$element) |
||
510 | |||
511 | /** |
||
512 | * method taken from PropertyPath. |
||
513 | * |
||
514 | * @param string $property |
||
515 | * |
||
516 | * @return mixed |
||
517 | */ |
||
518 | protected function camelize($property) |
||
522 | |||
523 | private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool |
||
524 | { |
||
525 | $values = $datagrid->getValues(); |
||
526 | |||
527 | if (!isset($values['_sort_by']) || !$values['_sort_by'] instanceof FieldDescriptionInterface) { |
||
534 | } |
||
535 |
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.