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 |
||
| 32 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $description; |
||
| 38 | /** |
||
| 39 | * @var string[] |
||
| 40 | */ |
||
| 41 | protected $fieldTitles; |
||
| 42 | /** |
||
| 43 | * @var string[] |
||
| 44 | */ |
||
| 45 | protected $fieldDescriptions; |
||
| 46 | /** |
||
| 47 | * @var string[] |
||
| 48 | */ |
||
| 49 | protected $requiredFields = array(); |
||
| 50 | /** |
||
| 51 | * @var string[] |
||
| 52 | */ |
||
| 53 | protected $searchableFields = array(); |
||
| 54 | /** |
||
| 55 | * @var DocumentRepository |
||
| 56 | */ |
||
| 57 | private $repository; |
||
| 58 | /** |
||
| 59 | * @var Visitor |
||
| 60 | */ |
||
| 61 | private $visitor; |
||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $notModifiableOriginRecords; |
||
| 66 | /** |
||
| 67 | * @var integer |
||
| 68 | */ |
||
| 69 | private $paginationDefaultLimit; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var boolean |
||
| 73 | */ |
||
| 74 | protected $filterByAuthUser; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $filterByAuthField; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var RqlTranslator |
||
| 83 | */ |
||
| 84 | protected $translator; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var DocumentManager |
||
| 88 | */ |
||
| 89 | protected $manager; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Visitor $visitor rql query visitor |
||
| 93 | * @param RqlTranslator $translator Translator for query modification |
||
| 94 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 95 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 96 | */ |
||
| 97 | 4 | public function __construct( |
|
| 98 | Visitor $visitor, |
||
| 99 | RqlTranslator $translator, |
||
| 100 | $notModifiableOriginRecords, |
||
| 101 | $paginationDefaultLimit |
||
| 102 | ) { |
||
| 103 | 4 | parent::__construct(); |
|
| 104 | 4 | $this->visitor = $visitor; |
|
| 105 | 4 | $this->translator = $translator; |
|
| 106 | 4 | $this->notModifiableOriginRecords = $notModifiableOriginRecords; |
|
| 107 | 4 | $this->paginationDefaultLimit = (int) $paginationDefaultLimit; |
|
| 108 | 4 | } |
|
| 109 | |||
| 110 | /** |
||
| 111 | * get repository instance |
||
| 112 | * |
||
| 113 | * @return DocumentRepository |
||
| 114 | */ |
||
| 115 | 2 | public function getRepository() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * create new app model |
||
| 122 | * |
||
| 123 | * @param DocumentRepository $repository Repository of countries |
||
| 124 | * |
||
| 125 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 126 | */ |
||
| 127 | 4 | public function setRepository(DocumentRepository $repository) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritDoc} |
||
| 137 | * |
||
| 138 | * @param Request $request The request object |
||
| 139 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 140 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $prefix the prefix for custom text search indexes |
||
| 244 | * @return bool |
||
| 245 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 246 | */ |
||
| 247 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string the version of the MongoDB as a string |
||
| 261 | */ |
||
| 262 | private function getMongoDBVersion() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param object $entity entity to insert |
||
| 276 | * @param bool $returnEntity true to return entity |
||
| 277 | * @param bool $doFlush if we should flush or not after insert |
||
| 278 | * |
||
| 279 | * @return Object|null |
||
| 280 | */ |
||
| 281 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $documentId id of entity to find |
||
| 296 | * |
||
| 297 | * @return Object |
||
| 298 | */ |
||
| 299 | 4 | public function find($documentId) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritDoc} |
||
| 306 | * |
||
| 307 | * @param string $documentId id of entity to update |
||
| 308 | * @param Object $entity new entity |
||
| 309 | * @param bool $returnEntity true to return entity |
||
| 310 | * |
||
| 311 | * @return Object|null |
||
| 312 | */ |
||
| 313 | 3 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * {@inheritDoc} |
||
| 338 | * |
||
| 339 | * @param string|object $id id of entity to delete or entity instance |
||
| 340 | * |
||
| 341 | * @return null|Object |
||
| 342 | */ |
||
| 343 | public function deleteRecord($id) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Triggers a flush on the DocumentManager |
||
| 364 | * |
||
| 365 | * @param null $document optional document |
||
| 366 | * |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | public function flush($document = null) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * A low level delete without any checks |
||
| 376 | * |
||
| 377 | * @param mixed $id record id |
||
| 378 | * |
||
| 379 | * @return void |
||
| 380 | */ |
||
| 381 | 2 | private function deleteById($id) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Checks in a performant way if a certain record id exists in the database |
||
| 393 | * |
||
| 394 | * @param mixed $id record id |
||
| 395 | * |
||
| 396 | * @return bool true if it exists, false otherwise |
||
| 397 | */ |
||
| 398 | 4 | public function recordExists($id) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 405 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 406 | * is a better way to get what you need. |
||
| 407 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 408 | * instance, make sure to pass false there. |
||
| 409 | * |
||
| 410 | * @param mixed $id record id |
||
| 411 | * @param array $fields list of fields you need. |
||
| 412 | * @param bool $hydrate whether to hydrate object or not |
||
| 413 | * |
||
| 414 | * @return array|null|object |
||
| 415 | */ |
||
| 416 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * get classname of entity |
||
| 433 | * |
||
| 434 | * @return string|null |
||
| 435 | */ |
||
| 436 | 4 | public function getEntityClass() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * {@inheritDoc} |
||
| 447 | * |
||
| 448 | * Currently this is being used to build the route id used for redirecting |
||
| 449 | * to newly made documents. It might benefit from having a different name |
||
| 450 | * for those purposes. |
||
| 451 | * |
||
| 452 | * We might use a convention based mapping here: |
||
| 453 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 454 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 455 | * |
||
| 456 | * @todo implement this in a more convention based manner |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function getConnectionName() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Does the actual query using the RQL Bundle. |
||
| 469 | * |
||
| 470 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 471 | * @param Query $query query from parser |
||
| 472 | * |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 484 | * |
||
| 485 | * @param Object $record record |
||
| 486 | * |
||
| 487 | * @return void |
||
| 488 | */ |
||
| 489 | 14 | protected function checkIfOriginRecord($record) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 507 | * |
||
| 508 | * @return int |
||
| 509 | */ |
||
| 510 | private function getDefaultLimit() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param Boolean $active active |
||
| 521 | * @param String $field field |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | 4 | public function setFilterByAuthUser($active, $field) |
|
| 529 | } |
||
| 530 |
This check looks for
@paramannotations 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.