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 |
||
34 | class DocumentModel extends SchemaModel implements ModelInterface |
||
35 | { |
||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $description; |
||
40 | /** |
||
41 | * @var string[] |
||
42 | */ |
||
43 | protected $fieldTitles; |
||
44 | /** |
||
45 | * @var string[] |
||
46 | */ |
||
47 | protected $fieldDescriptions; |
||
48 | /** |
||
49 | * @var string[] |
||
50 | */ |
||
51 | protected $requiredFields = array(); |
||
52 | /** |
||
53 | * @var string[] |
||
54 | */ |
||
55 | protected $searchableFields = array(); |
||
56 | /** |
||
57 | * @var DocumentRepository |
||
58 | */ |
||
59 | private $repository; |
||
60 | /** |
||
61 | * @var Visitor |
||
62 | */ |
||
63 | private $visitor; |
||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $notModifiableOriginRecords; |
||
68 | /** |
||
69 | * @var integer |
||
70 | */ |
||
71 | private $paginationDefaultLimit; |
||
72 | |||
73 | /** |
||
74 | * @var boolean |
||
75 | */ |
||
76 | protected $filterByAuthUser; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $filterByAuthField; |
||
82 | |||
83 | /** |
||
84 | * @var RqlTranslator |
||
85 | */ |
||
86 | protected $translator; |
||
87 | |||
88 | /** |
||
89 | * @var DocumentManager |
||
90 | */ |
||
91 | protected $manager; |
||
92 | |||
93 | /** |
||
94 | * @param Visitor $visitor rql query visitor |
||
95 | * @param RqlTranslator $translator Translator for query modification |
||
96 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
97 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
98 | */ |
||
99 | 4 | public function __construct( |
|
111 | |||
112 | /** |
||
113 | * get repository instance |
||
114 | * |
||
115 | * @return DocumentRepository |
||
116 | */ |
||
117 | 2 | public function getRepository() |
|
121 | |||
122 | /** |
||
123 | * create new app model |
||
124 | * |
||
125 | * @param DocumentRepository $repository Repository of countries |
||
126 | * |
||
127 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
128 | */ |
||
129 | 4 | public function setRepository(DocumentRepository $repository) |
|
136 | |||
137 | /** |
||
138 | * {@inheritDoc} |
||
139 | * |
||
140 | * @param Request $request The request object |
||
141 | * @param SecurityUser $user SecurityUser Object |
||
|
|||
142 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
264 | |||
265 | /** |
||
266 | * @param string $prefix the prefix for custom text search indexes |
||
267 | * @return bool |
||
268 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
269 | */ |
||
270 | private function hasCustomSearchIndex($prefix = 'search') |
||
281 | |||
282 | /** |
||
283 | * @return string the version of the MongoDB as a string |
||
284 | */ |
||
285 | private function getMongoDBVersion() |
||
296 | |||
297 | /** |
||
298 | * @param object $entity entity to insert |
||
299 | * @param bool $returnEntity true to return entity |
||
300 | * @param bool $doFlush if we should flush or not after insert |
||
301 | * |
||
302 | * @return Object|null |
||
303 | */ |
||
304 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
316 | |||
317 | /** |
||
318 | * @param string $documentId id of entity to find |
||
319 | * |
||
320 | * @return Object |
||
321 | */ |
||
322 | 4 | public function find($documentId) |
|
326 | |||
327 | /** |
||
328 | * {@inheritDoc} |
||
329 | * |
||
330 | * @param string $documentId id of entity to update |
||
331 | * @param Object $entity new entity |
||
332 | * @param bool $returnEntity true to return entity |
||
333 | * |
||
334 | * @return Object|null |
||
335 | */ |
||
336 | 2 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
358 | |||
359 | /** |
||
360 | * {@inheritDoc} |
||
361 | * |
||
362 | * @param string|object $id id of entity to delete or entity instance |
||
363 | * |
||
364 | * @return null|Object |
||
365 | */ |
||
366 | public function deleteRecord($id) |
||
384 | |||
385 | /** |
||
386 | * Triggers a flush on the DocumentManager |
||
387 | * |
||
388 | * @param null $document optional document |
||
389 | * |
||
390 | * @return void |
||
391 | */ |
||
392 | public function flush($document = null) |
||
396 | |||
397 | /** |
||
398 | * A low level delete without any checks |
||
399 | * |
||
400 | * @param mixed $id record id |
||
401 | * |
||
402 | * @return void |
||
403 | */ |
||
404 | 2 | private function deleteById($id) |
|
413 | |||
414 | /** |
||
415 | * Checks in a performant way if a certain record id exists in the database |
||
416 | * |
||
417 | * @param mixed $id record id |
||
418 | * |
||
419 | * @return bool true if it exists, false otherwise |
||
420 | */ |
||
421 | 4 | public function recordExists($id) |
|
425 | |||
426 | /** |
||
427 | * Returns a set of fields from an existing resource in a performant manner. |
||
428 | * If you need to check certain fields on an object (and don't need everything), this |
||
429 | * is a better way to get what you need. |
||
430 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
431 | * instance, make sure to pass false there. |
||
432 | * |
||
433 | * @param mixed $id record id |
||
434 | * @param array $fields list of fields you need. |
||
435 | * @param bool $hydrate whether to hydrate object or not |
||
436 | * |
||
437 | * @return array|null|object |
||
438 | */ |
||
439 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
453 | |||
454 | /** |
||
455 | * get classname of entity |
||
456 | * |
||
457 | * @return string|null |
||
458 | */ |
||
459 | 4 | public function getEntityClass() |
|
467 | |||
468 | /** |
||
469 | * {@inheritDoc} |
||
470 | * |
||
471 | * Currently this is being used to build the route id used for redirecting |
||
472 | * to newly made documents. It might benefit from having a different name |
||
473 | * for those purposes. |
||
474 | * |
||
475 | * We might use a convention based mapping here: |
||
476 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
477 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
478 | * |
||
479 | * @todo implement this in a more convention based manner |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | public function getConnectionName() |
||
489 | |||
490 | /** |
||
491 | * Does the actual query using the RQL Bundle. |
||
492 | * |
||
493 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
494 | * @param Query $query query from parser |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | protected function doRqlQuery($queryBuilder, Query $query) |
||
504 | |||
505 | /** |
||
506 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
507 | * |
||
508 | * @param Object $record record |
||
509 | * |
||
510 | * @return void |
||
511 | */ |
||
512 | 14 | protected function checkIfOriginRecord($record) |
|
527 | |||
528 | /** |
||
529 | * Determines the configured amount fo data records to be returned in pagination context. |
||
530 | * |
||
531 | * @return int |
||
532 | */ |
||
533 | private function getDefaultLimit() |
||
541 | |||
542 | /** |
||
543 | * @param Boolean $active active |
||
544 | * @param String $field field |
||
545 | * @return void |
||
546 | */ |
||
547 | 4 | public function setFilterByAuthUser($active, $field) |
|
552 | } |
||
553 |
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.