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) |
||
238 | |||
239 | /** |
||
240 | * @param string $prefix the prefix for custom text search indexes |
||
241 | * @return bool |
||
242 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
243 | */ |
||
244 | private function hasCustomSearchIndex($prefix = 'search') |
||
255 | |||
256 | /** |
||
257 | * @return string the version of the MongoDB as a string |
||
258 | */ |
||
259 | private function getMongoDBVersion() |
||
270 | |||
271 | /** |
||
272 | * @param object $entity entity to insert |
||
273 | * @param bool $returnEntity true to return entity |
||
274 | * @param bool $doFlush if we should flush or not after insert |
||
275 | * |
||
276 | * @return Object|null |
||
277 | */ |
||
278 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
290 | |||
291 | /** |
||
292 | * @param string $documentId id of entity to find |
||
293 | * |
||
294 | * @return Object |
||
295 | */ |
||
296 | 4 | public function find($documentId) |
|
300 | |||
301 | /** |
||
302 | * {@inheritDoc} |
||
303 | * |
||
304 | * @param string $documentId id of entity to update |
||
305 | * @param Object $entity new entity |
||
306 | * @param bool $returnEntity true to return entity |
||
307 | * |
||
308 | * @return Object|null |
||
309 | */ |
||
310 | 3 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
332 | |||
333 | /** |
||
334 | * {@inheritDoc} |
||
335 | * |
||
336 | * @param string|object $id id of entity to delete or entity instance |
||
337 | * |
||
338 | * @return null|Object |
||
339 | */ |
||
340 | public function deleteRecord($id) |
||
358 | |||
359 | /** |
||
360 | * Triggers a flush on the DocumentManager |
||
361 | * |
||
362 | * @param null $document optional document |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | public function flush($document = null) |
||
370 | |||
371 | /** |
||
372 | * A low level delete without any checks |
||
373 | * |
||
374 | * @param mixed $id record id |
||
375 | * |
||
376 | * @return void |
||
377 | */ |
||
378 | 2 | private function deleteById($id) |
|
387 | |||
388 | /** |
||
389 | * Checks in a performant way if a certain record id exists in the database |
||
390 | * |
||
391 | * @param mixed $id record id |
||
392 | * |
||
393 | * @return bool true if it exists, false otherwise |
||
394 | */ |
||
395 | 4 | public function recordExists($id) |
|
399 | |||
400 | /** |
||
401 | * Returns a set of fields from an existing resource in a performant manner. |
||
402 | * If you need to check certain fields on an object (and don't need everything), this |
||
403 | * is a better way to get what you need. |
||
404 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
405 | * instance, make sure to pass false there. |
||
406 | * |
||
407 | * @param mixed $id record id |
||
408 | * @param array $fields list of fields you need. |
||
409 | * @param bool $hydrate whether to hydrate object or not |
||
410 | * |
||
411 | * @return array|null|object |
||
412 | */ |
||
413 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
427 | |||
428 | /** |
||
429 | * get classname of entity |
||
430 | * |
||
431 | * @return string|null |
||
432 | */ |
||
433 | 4 | public function getEntityClass() |
|
441 | |||
442 | /** |
||
443 | * {@inheritDoc} |
||
444 | * |
||
445 | * Currently this is being used to build the route id used for redirecting |
||
446 | * to newly made documents. It might benefit from having a different name |
||
447 | * for those purposes. |
||
448 | * |
||
449 | * We might use a convention based mapping here: |
||
450 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
451 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
452 | * |
||
453 | * @todo implement this in a more convention based manner |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | public function getConnectionName() |
||
463 | |||
464 | /** |
||
465 | * Does the actual query using the RQL Bundle. |
||
466 | * |
||
467 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
468 | * @param Query $query query from parser |
||
469 | * |
||
470 | * @return array |
||
471 | */ |
||
472 | protected function doRqlQuery($queryBuilder, Query $query) |
||
478 | |||
479 | /** |
||
480 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
481 | * |
||
482 | * @param Object $record record |
||
483 | * |
||
484 | * @return void |
||
485 | */ |
||
486 | 14 | protected function checkIfOriginRecord($record) |
|
501 | |||
502 | /** |
||
503 | * Determines the configured amount fo data records to be returned in pagination context. |
||
504 | * |
||
505 | * @return int |
||
506 | */ |
||
507 | private function getDefaultLimit() |
||
515 | |||
516 | /** |
||
517 | * @param Boolean $active active |
||
518 | * @param String $field field |
||
519 | * @return void |
||
520 | */ |
||
521 | 4 | public function setFilterByAuthUser($active, $field) |
|
526 | } |
||
527 |
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.