Complex classes like DocumentModel 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 DocumentModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class DocumentModel extends SchemaModel implements ModelInterface |
||
34 | { |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $description; |
||
39 | /** |
||
40 | * @var string[] |
||
41 | */ |
||
42 | protected $fieldTitles; |
||
43 | /** |
||
44 | * @var string[] |
||
45 | */ |
||
46 | protected $fieldDescriptions; |
||
47 | /** |
||
48 | * @var string[] |
||
49 | */ |
||
50 | protected $requiredFields = array(); |
||
51 | /** |
||
52 | * @var string[] |
||
53 | */ |
||
54 | protected $searchableFields = array(); |
||
55 | /** |
||
56 | * @var string[] |
||
57 | */ |
||
58 | protected $textIndexes = array(); |
||
59 | /** |
||
60 | * @var DocumentRepository |
||
61 | */ |
||
62 | private $repository; |
||
63 | /** |
||
64 | * @var Visitor |
||
65 | */ |
||
66 | private $visitor; |
||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $notModifiableOriginRecords; |
||
71 | /** |
||
72 | * @var integer |
||
73 | */ |
||
74 | private $paginationDefaultLimit; |
||
75 | |||
76 | /** |
||
77 | * @var boolean |
||
78 | */ |
||
79 | protected $filterByAuthUser; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $filterByAuthField; |
||
85 | |||
86 | /** |
||
87 | * @var DocumentManager |
||
88 | */ |
||
89 | protected $manager; |
||
90 | |||
91 | /** @var EventDispatcher */ |
||
92 | protected $eventDispatcher; |
||
93 | |||
94 | /** |
||
95 | * @param Visitor $visitor rql query visitor |
||
96 | * @param EventDispatcher $eventDispatcher Kernel event dispatcher |
||
97 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
98 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination cnt |
||
99 | */ |
||
100 | public function __construct( |
||
112 | |||
113 | /** |
||
114 | * get repository instance |
||
115 | * |
||
116 | * @return DocumentRepository |
||
117 | */ |
||
118 | public function getRepository() |
||
122 | |||
123 | /** |
||
124 | * create new app model |
||
125 | * |
||
126 | * @param DocumentRepository $repository Repository of countries |
||
127 | * |
||
128 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
129 | */ |
||
130 | public function setRepository(DocumentRepository $repository) |
||
137 | |||
138 | /** |
||
139 | * {@inheritDoc} |
||
140 | * |
||
141 | * @param Request $request The request object |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function findAll(Request $request) |
||
226 | |||
227 | /** |
||
228 | * @param XiagQuery $xiagQuery Xiag Builder |
||
229 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
230 | * @return array |
||
231 | */ |
||
232 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
283 | |||
284 | /** |
||
285 | * Check if collection has search indexes in DB |
||
286 | * |
||
287 | * @param string $prefix the prefix for custom text search indexes |
||
288 | * @return bool |
||
289 | */ |
||
290 | private function hasCustomSearchIndex($prefix = 'search') |
||
307 | |||
308 | /** |
||
309 | * Build Search text index |
||
310 | * |
||
311 | * @param MongoBuilder $queryBuilder Doctrine mongo query builder object |
||
312 | * @param SearchNode $searchNode Graviton Search node |
||
313 | * @return MongoBuilder |
||
314 | */ |
||
315 | private function buildSearchTextQuery(MongoBuilder $queryBuilder, SearchNode $searchNode) |
||
328 | |||
329 | /** |
||
330 | * @return string the version of the MongoDB as a string |
||
331 | */ |
||
332 | private function getMongoDBVersion() |
||
343 | |||
344 | /** |
||
345 | * @param object $entity entity to insert |
||
346 | * @param bool $returnEntity true to return entity |
||
347 | * @param bool $doFlush if we should flush or not after insert |
||
348 | * |
||
349 | * @return Object|null |
||
350 | */ |
||
351 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
366 | |||
367 | /** |
||
368 | * @param string $documentId id of entity to find |
||
369 | * @param Request $request request |
||
370 | * |
||
371 | * @return Object |
||
372 | */ |
||
373 | public function find($documentId, Request $request = null) |
||
400 | |||
401 | /** |
||
402 | * {@inheritDoc} |
||
403 | * |
||
404 | * @param string $documentId id of entity to update |
||
405 | * @param Object $entity new entity |
||
406 | * @param bool $returnEntity true to return entity |
||
407 | * |
||
408 | * @return Object|null |
||
409 | */ |
||
410 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
431 | |||
432 | /** |
||
433 | * {@inheritDoc} |
||
434 | * |
||
435 | * @param string|object $id id of entity to delete or entity instance |
||
436 | * |
||
437 | * @return null|Object |
||
438 | */ |
||
439 | public function deleteRecord($id) |
||
463 | |||
464 | /** |
||
465 | * Triggers a flush on the DocumentManager |
||
466 | * |
||
467 | * @param null $document optional document |
||
468 | * |
||
469 | * @return void |
||
470 | */ |
||
471 | public function flush($document = null) |
||
475 | |||
476 | /** |
||
477 | * A low level delete without any checks |
||
478 | * |
||
479 | * @param mixed $id record id |
||
480 | * |
||
481 | * @return void |
||
482 | */ |
||
483 | private function deleteById($id) |
||
492 | |||
493 | /** |
||
494 | * Checks in a performant way if a certain record id exists in the database |
||
495 | * |
||
496 | * @param mixed $id record id |
||
497 | * |
||
498 | * @return bool true if it exists, false otherwise |
||
499 | */ |
||
500 | public function recordExists($id) |
||
504 | |||
505 | /** |
||
506 | * Returns a set of fields from an existing resource in a performant manner. |
||
507 | * If you need to check certain fields on an object (and don't need everything), this |
||
508 | * is a better way to get what you need. |
||
509 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
510 | * instance, make sure to pass false there. |
||
511 | * |
||
512 | * @param mixed $id record id |
||
513 | * @param array $fields list of fields you need. |
||
514 | * @param bool $hydrate whether to hydrate object or not |
||
515 | * |
||
516 | * @return array|null|object |
||
517 | */ |
||
518 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
532 | |||
533 | /** |
||
534 | * get classname of entity |
||
535 | * |
||
536 | * @return string|null |
||
537 | */ |
||
538 | public function getEntityClass() |
||
546 | |||
547 | /** |
||
548 | * {@inheritDoc} |
||
549 | * |
||
550 | * Currently this is being used to build the route id used for redirecting |
||
551 | * to newly made documents. It might benefit from having a different name |
||
552 | * for those purposes. |
||
553 | * |
||
554 | * We might use a convention based mapping here: |
||
555 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
556 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
557 | * |
||
558 | * @todo implement this in a more convention based manner |
||
559 | * |
||
560 | * @return string |
||
561 | */ |
||
562 | public function getConnectionName() |
||
568 | |||
569 | /** |
||
570 | * Does the actual query using the RQL Bundle. |
||
571 | * |
||
572 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
573 | * @param Query $query query from parser |
||
574 | * |
||
575 | * @return array |
||
576 | */ |
||
577 | protected function doRqlQuery($queryBuilder, Query $query) |
||
583 | |||
584 | /** |
||
585 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
586 | * |
||
587 | * @param Object $record record |
||
588 | * |
||
589 | * @return void |
||
590 | */ |
||
591 | protected function checkIfOriginRecord($record) |
||
606 | |||
607 | /** |
||
608 | * Determines the configured amount fo data records to be returned in pagination context. |
||
609 | * |
||
610 | * @return int |
||
611 | */ |
||
612 | private function getDefaultLimit() |
||
620 | |||
621 | /** |
||
622 | * Will fire a ModelEvent |
||
623 | * |
||
624 | * @param string $action insert or update |
||
625 | * @param Object $collection the changed Document |
||
626 | * |
||
627 | * @return void |
||
628 | */ |
||
629 | private function dispatchModelEvent($action, $collection) |
||
647 | } |
||
648 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: