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) |
|
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) |
||
255 | |||
256 | /** |
||
257 | * @param string $prefix the prefix for custom text search indexes |
||
258 | * @return bool |
||
259 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
260 | */ |
||
261 | private function hasCustomSearchIndex($prefix = 'search') |
||
272 | |||
273 | /** |
||
274 | * @return string the version of the MongoDB as a string |
||
275 | */ |
||
276 | private function getMongoDBVersion() |
||
287 | |||
288 | /** |
||
289 | * @param object $entity entity to insert |
||
290 | * @param bool $returnEntity true to return entity |
||
291 | * @param bool $doFlush if we should flush or not after insert |
||
292 | * |
||
293 | * @return Object|null |
||
294 | */ |
||
295 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
307 | |||
308 | /** |
||
309 | * @param string $documentId id of entity to find |
||
310 | * |
||
311 | * @return Object |
||
312 | */ |
||
313 | 4 | public function find($documentId) |
|
317 | |||
318 | /** |
||
319 | * {@inheritDoc} |
||
320 | * |
||
321 | * @param string $documentId id of entity to update |
||
322 | * @param Object $entity new entity |
||
323 | * @param bool $returnEntity true to return entity |
||
324 | * |
||
325 | * @return Object|null |
||
326 | */ |
||
327 | 2 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
349 | |||
350 | /** |
||
351 | * {@inheritDoc} |
||
352 | * |
||
353 | * @param string|object $id id of entity to delete or entity instance |
||
354 | * |
||
355 | * @return null|Object |
||
356 | */ |
||
357 | public function deleteRecord($id) |
||
375 | |||
376 | /** |
||
377 | * Triggers a flush on the DocumentManager |
||
378 | * |
||
379 | * @param null $document optional document |
||
380 | * |
||
381 | * @return void |
||
382 | */ |
||
383 | public function flush($document = null) |
||
387 | |||
388 | /** |
||
389 | * A low level delete without any checks |
||
390 | * |
||
391 | * @param mixed $id record id |
||
392 | * |
||
393 | * @return void |
||
394 | */ |
||
395 | 2 | private function deleteById($id) |
|
404 | |||
405 | /** |
||
406 | * Checks in a performant way if a certain record id exists in the database |
||
407 | * |
||
408 | * @param mixed $id record id |
||
409 | * |
||
410 | * @return bool true if it exists, false otherwise |
||
411 | */ |
||
412 | 4 | public function recordExists($id) |
|
416 | |||
417 | /** |
||
418 | * Returns a set of fields from an existing resource in a performant manner. |
||
419 | * If you need to check certain fields on an object (and don't need everything), this |
||
420 | * is a better way to get what you need. |
||
421 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
422 | * instance, make sure to pass false there. |
||
423 | * |
||
424 | * @param mixed $id record id |
||
425 | * @param array $fields list of fields you need. |
||
426 | * @param bool $hydrate whether to hydrate object or not |
||
427 | * |
||
428 | * @return array|null|object |
||
429 | */ |
||
430 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
444 | |||
445 | /** |
||
446 | * get classname of entity |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | 4 | public function getEntityClass() |
|
454 | |||
455 | /** |
||
456 | * {@inheritDoc} |
||
457 | * |
||
458 | * Currently this is being used to build the route id used for redirecting |
||
459 | * to newly made documents. It might benefit from having a different name |
||
460 | * for those purposes. |
||
461 | * |
||
462 | * We might use a convention based mapping here: |
||
463 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
464 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
465 | * |
||
466 | * @todo implement this in a more convention based manner |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | public function getConnectionName() |
||
476 | |||
477 | /** |
||
478 | * Does the actual query using the RQL Bundle. |
||
479 | * |
||
480 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
481 | * @param Query $query query from parser |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | protected function doRqlQuery($queryBuilder, Query $query) |
||
491 | |||
492 | /** |
||
493 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
494 | * |
||
495 | * @param Object $record record |
||
496 | * |
||
497 | * @return void |
||
498 | */ |
||
499 | 14 | protected function checkIfOriginRecord($record) |
|
514 | |||
515 | /** |
||
516 | * Determines the configured amount fo data records to be returned in pagination context. |
||
517 | * |
||
518 | * @return int |
||
519 | */ |
||
520 | private function getDefaultLimit() |
||
528 | |||
529 | /** |
||
530 | * @param Boolean $active active |
||
531 | * @param String $field field |
||
532 | * @return void |
||
533 | */ |
||
534 | 4 | public function setFilterByAuthUser($active, $field) |
|
539 | } |
||
540 |
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.