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 DocumentRepository |
||
57 | */ |
||
58 | private $repository; |
||
59 | /** |
||
60 | * @var Visitor |
||
61 | */ |
||
62 | private $visitor; |
||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $notModifiableOriginRecords; |
||
67 | /** |
||
68 | * @var integer |
||
69 | */ |
||
70 | private $paginationDefaultLimit; |
||
71 | |||
72 | /** |
||
73 | * @var boolean |
||
74 | */ |
||
75 | protected $filterByAuthUser; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $filterByAuthField; |
||
81 | |||
82 | /** |
||
83 | * @var RqlTranslator |
||
84 | */ |
||
85 | protected $translator; |
||
86 | |||
87 | /** |
||
88 | * @var DocumentManager |
||
89 | */ |
||
90 | protected $manager; |
||
91 | |||
92 | /** |
||
93 | * @param Visitor $visitor rql query visitor |
||
94 | * @param RqlTranslator $translator Translator for query modification |
||
95 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
96 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
97 | */ |
||
98 | 4 | public function __construct( |
|
110 | |||
111 | /** |
||
112 | * get repository instance |
||
113 | * |
||
114 | * @return DocumentRepository |
||
115 | */ |
||
116 | 2 | public function getRepository() |
|
120 | |||
121 | /** |
||
122 | * create new app model |
||
123 | * |
||
124 | * @param DocumentRepository $repository Repository of countries |
||
125 | * |
||
126 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
127 | */ |
||
128 | 4 | public function setRepository(DocumentRepository $repository) |
|
129 | { |
||
130 | 4 | $this->repository = $repository; |
|
131 | 4 | $this->manager = $repository->getDocumentManager(); |
|
132 | |||
133 | 4 | return $this; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * {@inheritDoc} |
||
138 | * |
||
139 | * @param Request $request The request object |
||
140 | * @param SecurityUser $user SecurityUser Object |
||
|
|||
141 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
146 | { |
||
147 | $pageNumber = $request->query->get('page', 1); |
||
148 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
||
149 | $startAt = ($pageNumber - 1) * $numberPerPage; |
||
150 | // Only 1 search text node allowed. |
||
151 | $hasSearch = false; |
||
152 | |||
153 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
||
154 | $queryBuilder = $this->repository |
||
155 | ->createQueryBuilder(); |
||
156 | |||
157 | if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
||
158 | $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
||
159 | } |
||
160 | |||
161 | // *** do we have an RQL expression, do we need to filter data? |
||
162 | if ($request->attributes->get('hasRql', false)) { |
||
163 | $innerQuery = $request->attributes->get('rqlQuery')->getQuery(); |
||
164 | $xiagQuery = new XiagQuery(); |
||
165 | // can we perform a search in an index instead of filtering? |
||
166 | if ($innerQuery instanceof AbstractLogicOperatorNode) { |
||
167 | foreach ($innerQuery->getQueries() as $innerRql) { |
||
168 | if (!$hasSearch && $innerRql instanceof SearchNode) { |
||
169 | $searchString = implode('&', $innerRql->getSearchTerms()); |
||
170 | $queryBuilder->addAnd( |
||
171 | $queryBuilder->expr()->text($searchString) |
||
172 | ); |
||
173 | $hasSearch = true; |
||
174 | } else { |
||
175 | $xiagQuery->setQuery($innerRql); |
||
176 | } |
||
177 | } |
||
178 | } elseif ($this->hasCustomSearchIndex() && ($innerQuery instanceof SearchNode)) { |
||
179 | $searchString = implode('&', $innerQuery->getSearchTerms()); |
||
180 | $queryBuilder->addAnd( |
||
181 | $queryBuilder->expr()->text($searchString) |
||
182 | ); |
||
183 | $hasSearch = true; |
||
184 | } else { |
||
185 | if ($innerQuery instanceof AbstractScalarOperatorNode) { |
||
186 | $xiagQuery->setQuery($innerQuery); |
||
187 | } else { |
||
188 | /** @var AbstractLogicOperatorNode $innerQuery */ |
||
189 | foreach ($innerQuery->getQueries() as $innerRql) { |
||
190 | if (!$innerRql instanceof SearchNode) { |
||
191 | $xiagQuery->setQuery($innerRql); |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | $queryBuilder = $this->doRqlQuery( |
||
198 | $queryBuilder, |
||
199 | $xiagQuery |
||
200 | ); |
||
201 | } else { |
||
202 | // @todo [lapistano]: seems the offset is missing for this query. |
||
203 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
204 | $queryBuilder->find($this->repository->getDocumentName()); |
||
205 | } |
||
206 | |||
207 | // define offset and limit |
||
208 | if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
||
209 | $queryBuilder->skip($startAt); |
||
210 | } else { |
||
211 | $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
||
212 | } |
||
213 | |||
214 | if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
||
215 | $queryBuilder->limit($numberPerPage); |
||
216 | } else { |
||
217 | $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
||
218 | } |
||
219 | |||
220 | // Limit can not be negative nor null. |
||
221 | if ($numberPerPage < 1) { |
||
222 | throw new RqlSyntaxErrorException('negative or null limit in rql'); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * add a default sort on id if none was specified earlier |
||
227 | * |
||
228 | * not specifying something to sort on leads to very weird cases when fetching references |
||
229 | * If search node, sort by Score |
||
230 | * TODO Review this sorting, not 100% sure |
||
231 | */ |
||
232 | if ($hasSearch && !array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
233 | $queryBuilder->sortMeta('score', 'textScore'); |
||
234 | } elseif (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
235 | $queryBuilder->sort('_id'); |
||
236 | } |
||
237 | |||
238 | // run query |
||
239 | $query = $queryBuilder->getQuery(); |
||
240 | $records = array_values($query->execute()->toArray()); |
||
241 | |||
242 | $totalCount = $query->count(); |
||
243 | $numPages = (int) ceil($totalCount / $numberPerPage); |
||
244 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
||
245 | if ($numPages > 1) { |
||
246 | $request->attributes->set('paging', true); |
||
247 | $request->attributes->set('page', $page); |
||
248 | $request->attributes->set('numPages', $numPages); |
||
249 | $request->attributes->set('startAt', $startAt); |
||
250 | $request->attributes->set('perPage', $numberPerPage); |
||
251 | $request->attributes->set('totalCount', $totalCount); |
||
252 | } |
||
253 | |||
254 | return $records; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param string $prefix the prefix for custom text search indexes |
||
259 | * @return bool |
||
260 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
261 | */ |
||
262 | private function hasCustomSearchIndex($prefix = 'search') |
||
273 | |||
274 | /** |
||
275 | * @return string the version of the MongoDB as a string |
||
276 | */ |
||
277 | private function getMongoDBVersion() |
||
288 | |||
289 | /** |
||
290 | * @param object $entity entity to insert |
||
291 | * @param bool $returnEntity true to return entity |
||
292 | * @param bool $doFlush if we should flush or not after insert |
||
293 | * |
||
294 | * @return Object|null |
||
295 | */ |
||
296 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
308 | |||
309 | /** |
||
310 | * @param string $documentId id of entity to find |
||
311 | * |
||
312 | * @return Object |
||
313 | */ |
||
314 | 4 | public function find($documentId) |
|
318 | |||
319 | /** |
||
320 | * {@inheritDoc} |
||
321 | * |
||
322 | * @param string $documentId id of entity to update |
||
323 | * @param Object $entity new entity |
||
324 | * @param bool $returnEntity true to return entity |
||
325 | * |
||
326 | * @return Object|null |
||
327 | */ |
||
328 | 2 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
350 | |||
351 | /** |
||
352 | * {@inheritDoc} |
||
353 | * |
||
354 | * @param string|object $id id of entity to delete or entity instance |
||
355 | * |
||
356 | * @return null|Object |
||
357 | */ |
||
358 | public function deleteRecord($id) |
||
376 | |||
377 | /** |
||
378 | * Triggers a flush on the DocumentManager |
||
379 | * |
||
380 | * @param null $document optional document |
||
381 | * |
||
382 | * @return void |
||
383 | */ |
||
384 | public function flush($document = null) |
||
388 | |||
389 | /** |
||
390 | * A low level delete without any checks |
||
391 | * |
||
392 | * @param mixed $id record id |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | 2 | private function deleteById($id) |
|
405 | |||
406 | /** |
||
407 | * Checks in a performant way if a certain record id exists in the database |
||
408 | * |
||
409 | * @param mixed $id record id |
||
410 | * |
||
411 | * @return bool true if it exists, false otherwise |
||
412 | */ |
||
413 | 4 | public function recordExists($id) |
|
417 | |||
418 | /** |
||
419 | * Returns a set of fields from an existing resource in a performant manner. |
||
420 | * If you need to check certain fields on an object (and don't need everything), this |
||
421 | * is a better way to get what you need. |
||
422 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
423 | * instance, make sure to pass false there. |
||
424 | * |
||
425 | * @param mixed $id record id |
||
426 | * @param array $fields list of fields you need. |
||
427 | * @param bool $hydrate whether to hydrate object or not |
||
428 | * |
||
429 | * @return array|null|object |
||
430 | */ |
||
431 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
445 | |||
446 | /** |
||
447 | * get classname of entity |
||
448 | * |
||
449 | * @return string|null |
||
450 | */ |
||
451 | 4 | public function getEntityClass() |
|
459 | |||
460 | /** |
||
461 | * {@inheritDoc} |
||
462 | * |
||
463 | * Currently this is being used to build the route id used for redirecting |
||
464 | * to newly made documents. It might benefit from having a different name |
||
465 | * for those purposes. |
||
466 | * |
||
467 | * We might use a convention based mapping here: |
||
468 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
469 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
470 | * |
||
471 | * @todo implement this in a more convention based manner |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | public function getConnectionName() |
||
481 | |||
482 | /** |
||
483 | * Does the actual query using the RQL Bundle. |
||
484 | * |
||
485 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
486 | * @param Query $query query from parser |
||
487 | * |
||
488 | * @return array |
||
489 | */ |
||
490 | protected function doRqlQuery($queryBuilder, Query $query) |
||
496 | |||
497 | /** |
||
498 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
499 | * |
||
500 | * @param Object $record record |
||
501 | * |
||
502 | * @return void |
||
503 | */ |
||
504 | 14 | protected function checkIfOriginRecord($record) |
|
519 | |||
520 | /** |
||
521 | * Determines the configured amount fo data records to be returned in pagination context. |
||
522 | * |
||
523 | * @return int |
||
524 | */ |
||
525 | private function getDefaultLimit() |
||
533 | |||
534 | /** |
||
535 | * @param Boolean $active active |
||
536 | * @param String $field field |
||
537 | * @return void |
||
538 | */ |
||
539 | 4 | public function setFilterByAuthUser($active, $field) |
|
544 | } |
||
545 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.