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 |
||
35 | class DocumentModel extends SchemaModel implements ModelInterface |
||
36 | { |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $description; |
||
41 | /** |
||
42 | * @var string[] |
||
43 | */ |
||
44 | protected $fieldTitles; |
||
45 | /** |
||
46 | * @var string[] |
||
47 | */ |
||
48 | protected $fieldDescriptions; |
||
49 | /** |
||
50 | * @var string[] |
||
51 | */ |
||
52 | protected $requiredFields = array(); |
||
53 | /** |
||
54 | * @var string[] |
||
55 | */ |
||
56 | protected $searchableFields = array(); |
||
57 | /** |
||
58 | * @var string[] |
||
59 | */ |
||
60 | protected $textIndexes = array(); |
||
61 | /** |
||
62 | * @var DocumentRepository |
||
63 | */ |
||
64 | private $repository; |
||
65 | /** |
||
66 | * @var Visitor |
||
67 | */ |
||
68 | private $visitor; |
||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $notModifiableOriginRecords; |
||
73 | /** |
||
74 | * @var integer |
||
75 | */ |
||
76 | private $paginationDefaultLimit; |
||
77 | |||
78 | /** |
||
79 | * @var boolean |
||
80 | */ |
||
81 | protected $filterByAuthUser; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $filterByAuthField; |
||
87 | |||
88 | /** |
||
89 | * @var DocumentManager |
||
90 | */ |
||
91 | protected $manager; |
||
92 | |||
93 | /** @var EventDispatcher */ |
||
94 | protected $eventDispatcher; |
||
95 | |||
96 | /** @var $collectionCache */ |
||
97 | protected $cache; |
||
98 | |||
99 | /** |
||
100 | * @var RestUtils |
||
101 | */ |
||
102 | private $restUtils; |
||
103 | |||
104 | /** |
||
105 | * @param Visitor $visitor rql query visitor |
||
106 | * @param RestUtils $restUtils Rest utils |
||
107 | * @param EventDispatcher $eventDispatcher Kernel event dispatcher |
||
108 | * @param CollectionCache $collectionCache Cache Service |
||
109 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
110 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination cnt |
||
111 | */ |
||
112 | public function __construct( |
||
128 | |||
129 | /** |
||
130 | * get repository instance |
||
131 | * |
||
132 | * @return DocumentRepository |
||
133 | */ |
||
134 | public function getRepository() |
||
138 | |||
139 | /** |
||
140 | * create new app model |
||
141 | * |
||
142 | * @param DocumentRepository $repository Repository of countries |
||
143 | * |
||
144 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
145 | */ |
||
146 | public function setRepository(DocumentRepository $repository) |
||
153 | |||
154 | /** |
||
155 | * {@inheritDoc} |
||
156 | * |
||
157 | * @param Request $request The request object |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | public function findAll(Request $request) |
||
242 | |||
243 | /** |
||
244 | * @param XiagQuery $xiagQuery Xiag Builder |
||
245 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
246 | * @return array |
||
247 | */ |
||
248 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
299 | |||
300 | /** |
||
301 | * Check if collection has search indexes in DB |
||
302 | * |
||
303 | * @param string $prefix the prefix for custom text search indexes |
||
304 | * @return bool |
||
305 | */ |
||
306 | private function hasCustomSearchIndex($prefix = 'search') |
||
323 | |||
324 | /** |
||
325 | * Build Search text index |
||
326 | * |
||
327 | * @param MongoBuilder $queryBuilder Doctrine mongo query builder object |
||
328 | * @param SearchNode $searchNode Graviton Search node |
||
329 | * @return MongoBuilder |
||
330 | */ |
||
331 | private function buildSearchTextQuery(MongoBuilder $queryBuilder, SearchNode $searchNode) |
||
344 | |||
345 | /** |
||
346 | * @return string the version of the MongoDB as a string |
||
347 | */ |
||
348 | private function getMongoDBVersion() |
||
359 | |||
360 | /** |
||
361 | * @param object $entity entity to insert |
||
362 | * @param bool $returnEntity true to return entity |
||
363 | * @param bool $doFlush if we should flush or not after insert |
||
364 | * |
||
365 | * @return Object|null |
||
366 | */ |
||
367 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
383 | |||
384 | /** |
||
385 | * @param string $documentId id of entity to find |
||
386 | * |
||
387 | * @throws NotFoundException |
||
388 | * @return Object |
||
389 | */ |
||
390 | public function find($documentId) |
||
400 | |||
401 | /** |
||
402 | * Will attempt to find Document by ID. |
||
403 | * If config cache is enabled for document it will save it. |
||
404 | * |
||
405 | * @param string $documentId id of entity to find |
||
406 | * @param Request $request request |
||
407 | * |
||
408 | * @throws NotFoundException |
||
409 | * @return string Serialised object |
||
410 | */ |
||
411 | public function getSerialised($documentId, Request $request = null) |
||
435 | |||
436 | /** |
||
437 | * {@inheritDoc} |
||
438 | * |
||
439 | * @param string $documentId id of entity to update |
||
440 | * @param Object $entity new entity |
||
441 | * @param bool $returnEntity true to return entity |
||
442 | * |
||
443 | * @return Object|null |
||
444 | */ |
||
445 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
467 | |||
468 | /** |
||
469 | * {@inheritDoc} |
||
470 | * |
||
471 | * @param string|object $id id of entity to delete or entity instance |
||
472 | * |
||
473 | * @return null|Object |
||
474 | */ |
||
475 | public function deleteRecord($id) |
||
504 | |||
505 | /** |
||
506 | * Triggers a flush on the DocumentManager |
||
507 | * |
||
508 | * @param null $document optional document |
||
509 | * |
||
510 | * @return void |
||
511 | */ |
||
512 | public function flush($document = null) |
||
516 | |||
517 | /** |
||
518 | * A low level delete without any checks |
||
519 | * |
||
520 | * @param mixed $id record id |
||
521 | * |
||
522 | * @return void |
||
523 | */ |
||
524 | private function deleteById($id) |
||
533 | |||
534 | /** |
||
535 | * Checks in a performant way if a certain record id exists in the database |
||
536 | * |
||
537 | * @param mixed $id record id |
||
538 | * |
||
539 | * @return bool true if it exists, false otherwise |
||
540 | */ |
||
541 | public function recordExists($id) |
||
545 | |||
546 | /** |
||
547 | * Returns a set of fields from an existing resource in a performant manner. |
||
548 | * If you need to check certain fields on an object (and don't need everything), this |
||
549 | * is a better way to get what you need. |
||
550 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
551 | * instance, make sure to pass false there. |
||
552 | * |
||
553 | * @param mixed $id record id |
||
554 | * @param array $fields list of fields you need. |
||
555 | * @param bool $hydrate whether to hydrate object or not |
||
556 | * |
||
557 | * @return array|null|object |
||
558 | */ |
||
559 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
573 | |||
574 | /** |
||
575 | * get classname of entity |
||
576 | * |
||
577 | * @return string|null |
||
578 | */ |
||
579 | public function getEntityClass() |
||
587 | |||
588 | /** |
||
589 | * {@inheritDoc} |
||
590 | * |
||
591 | * Currently this is being used to build the route id used for redirecting |
||
592 | * to newly made documents. It might benefit from having a different name |
||
593 | * for those purposes. |
||
594 | * |
||
595 | * We might use a convention based mapping here: |
||
596 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
597 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
598 | * |
||
599 | * @todo implement this in a more convention based manner |
||
600 | * |
||
601 | * @return string |
||
602 | */ |
||
603 | public function getConnectionName() |
||
609 | |||
610 | /** |
||
611 | * Does the actual query using the RQL Bundle. |
||
612 | * |
||
613 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
614 | * @param Query $query query from parser |
||
615 | * |
||
616 | * @return array |
||
617 | */ |
||
618 | protected function doRqlQuery($queryBuilder, Query $query) |
||
624 | |||
625 | /** |
||
626 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
627 | * |
||
628 | * @param Object $record record |
||
629 | * |
||
630 | * @return void |
||
631 | */ |
||
632 | protected function checkIfOriginRecord($record) |
||
647 | |||
648 | /** |
||
649 | * Determines the configured amount fo data records to be returned in pagination context. |
||
650 | * |
||
651 | * @return int |
||
652 | */ |
||
653 | private function getDefaultLimit() |
||
661 | |||
662 | /** |
||
663 | * Will fire a ModelEvent |
||
664 | * |
||
665 | * @param string $action insert or update |
||
666 | * @param Object $collection the changed Document |
||
667 | * |
||
668 | * @return void |
||
669 | */ |
||
670 | private function dispatchModelEvent($action, $collection) |
||
688 | } |
||
689 |
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: