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 | /** |
||
30 | * @var DocumentManager |
||
31 | */ |
||
32 | protected $dm; |
||
33 | |||
34 | /** |
||
35 | * @param DocumentManager $dm |
||
36 | */ |
||
37 | public function __construct(DocumentManager $dm) |
||
41 | |||
42 | /** |
||
43 | * Returns the related model's metadata. |
||
44 | * |
||
45 | * @param string $class |
||
46 | * |
||
47 | * @return ClassMetadata |
||
48 | */ |
||
49 | public function getMetadata($class) |
||
53 | |||
54 | /** |
||
55 | * Returns true is the model has some metadata. |
||
56 | * |
||
57 | * @param string $class |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | public function hasMetadata($class) |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | * |
||
69 | * @throws ModelManagerException if the document manager throws any exception |
||
70 | */ |
||
71 | public function create($object) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | * |
||
84 | * @throws ModelManagerException if the document manager throws any exception |
||
85 | */ |
||
86 | public function update($object) |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | * |
||
99 | * @throws ModelManagerException if the document manager throws any exception |
||
100 | */ |
||
101 | public function delete($object) |
||
110 | |||
111 | /** |
||
112 | * Find one object from the given class repository. |
||
113 | * |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function find($class, $id) |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | * |
||
132 | * @return FieldDescription |
||
133 | * |
||
134 | * @throws \RunTimeException if $name is not a string |
||
135 | */ |
||
136 | public function getNewFieldDescriptionInstance($class, $name, array $options = array()) |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function findBy($class, array $criteria = array()) |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function findOneBy($class, array $criteria = array()) |
||
174 | |||
175 | /** |
||
176 | * @return DocumentManager The PHPCR-ODM document manager responsible for |
||
177 | * this model |
||
178 | */ |
||
179 | public function getDocumentManager() |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | * |
||
187 | * @return FieldDescriptionInterface |
||
188 | */ |
||
189 | public function getParentFieldDescription($parentAssociationMapping, $class) |
||
203 | |||
204 | /** |
||
205 | * @param string $class the fully qualified class name to search for |
||
206 | * @param string $alias alias to use for this class when accessing fields, |
||
207 | * defaults to 'a' |
||
208 | * |
||
209 | * @return ProxyQueryInterface |
||
210 | * |
||
211 | * @throws \InvalidArgumentException if alias is not a string or an empty string |
||
212 | */ |
||
213 | public function createQuery($class, $alias = 'a') |
||
220 | |||
221 | /** |
||
222 | * @param ProxyQuery $query |
||
223 | * |
||
224 | * @return mixed |
||
225 | */ |
||
226 | public function executeQuery($query) |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function getModelIdentifier($classname) |
||
238 | |||
239 | /** |
||
240 | * Transforms the document into the PHPCR path. |
||
241 | * |
||
242 | * Note: This is returning an array because Doctrine ORM for example can |
||
243 | * have multiple identifiers, e.g. if the primary key is composed of |
||
244 | * several columns. We only ever have one, but return that wrapped into an |
||
245 | * array to adhere to the interface. |
||
246 | * |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function getIdentifierValues($document) |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function getIdentifierFieldNames($class) |
||
264 | |||
265 | /** |
||
266 | * This is just taking the id out of the array again. |
||
267 | * |
||
268 | * {@inheritdoc} |
||
269 | * |
||
270 | * @throws \InvalidArgumentException if $document is not an object or null |
||
271 | */ |
||
272 | public function getNormalizedIdentifier($document) |
||
287 | |||
288 | /** |
||
289 | * Currently only the leading slash is removed. |
||
290 | * |
||
291 | * @param object $document |
||
292 | * |
||
293 | * @return null|string |
||
294 | */ |
||
295 | public function getUrlsafeIdentifier($document) |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) |
||
320 | |||
321 | /** |
||
322 | * Add leading slash to construct valid phpcr document id. |
||
323 | * |
||
324 | * The phpcr-odm QueryBuilder uses absolute paths and expects id´s to start with a forward slash |
||
325 | * because SonataAdmin uses object id´s for constructing URL´s it has to use id´s without the |
||
326 | * leading slash. |
||
327 | * |
||
328 | * @param string $id |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getBackendId($id) |
||
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | * |
||
340 | * @throws ModelManagerException if anything goes wrong during query execution |
||
341 | */ |
||
342 | public function batchDelete($class, ProxyQueryInterface $queryProxy) |
||
362 | |||
363 | /** |
||
364 | * {@inheritdoc} |
||
365 | * |
||
366 | * @return object |
||
367 | */ |
||
368 | public function getModelInstance($class) |
||
372 | |||
373 | /** |
||
374 | * {@inheritdoc} |
||
375 | */ |
||
376 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
||
395 | |||
396 | /** |
||
397 | * {@inheritdoc} |
||
398 | */ |
||
399 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
408 | |||
409 | /** |
||
410 | * {@inheritdoc} |
||
411 | */ |
||
412 | public function getDefaultSortValues($class) |
||
420 | |||
421 | /** |
||
422 | * {@inheritdoc} |
||
423 | * |
||
424 | * @return object |
||
425 | */ |
||
426 | public function modelTransform($class, $instance) |
||
430 | |||
431 | /** |
||
432 | * {@inheritdoc} |
||
433 | * |
||
434 | * @return object |
||
435 | * |
||
436 | * @throws NoSuchPropertyException if the class has no magic setter and |
||
437 | * public property for a field in array |
||
438 | */ |
||
439 | public function modelReverseTransform($class, array $array = array()) |
||
482 | |||
483 | /** |
||
484 | * {@inheritdoc} |
||
485 | */ |
||
486 | public function getModelCollectionInstance($class) |
||
490 | |||
491 | /** |
||
492 | * {@inheritdoc} |
||
493 | */ |
||
494 | public function collectionClear(&$collection) |
||
498 | |||
499 | /** |
||
500 | * {@inheritdoc} |
||
501 | */ |
||
502 | public function collectionHasElement(&$collection, &$element) |
||
506 | |||
507 | /** |
||
508 | * {@inheritdoc} |
||
509 | */ |
||
510 | public function collectionAddElement(&$collection, &$element) |
||
514 | |||
515 | /** |
||
516 | * {@inheritdoc} |
||
517 | */ |
||
518 | public function collectionRemoveElement(&$collection, &$element) |
||
522 | |||
523 | /** |
||
524 | * {@inheritdoc} |
||
525 | */ |
||
526 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
||
530 | |||
531 | /** |
||
532 | * {@inheritdoc} |
||
533 | * |
||
534 | * Not really implemented. |
||
535 | */ |
||
536 | public function getExportFields($class) |
||
540 | |||
541 | /** |
||
542 | * Method taken from PropertyPath. |
||
543 | * |
||
544 | * NEXT_MAJOR: remove when doing https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle/issues/187 |
||
545 | * |
||
546 | * @param string $property |
||
547 | * |
||
548 | * @return string |
||
549 | * |
||
550 | * @deprecated |
||
551 | */ |
||
552 | protected function camelize($property) |
||
556 | } |
||
557 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.