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) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $prefix the prefix for custom text search indexes |
||
| 248 | * @return bool |
||
| 249 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 250 | */ |
||
| 251 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return string the version of the MongoDB as a string |
||
| 265 | */ |
||
| 266 | private function getMongoDBVersion() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param object $entity entity to insert |
||
| 280 | * @param bool $returnEntity true to return entity |
||
| 281 | * @param bool $doFlush if we should flush or not after insert |
||
| 282 | * |
||
| 283 | * @return Object|null |
||
| 284 | */ |
||
| 285 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $documentId id of entity to find |
||
| 300 | * |
||
| 301 | * @return Object |
||
| 302 | */ |
||
| 303 | 4 | public function find($documentId) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritDoc} |
||
| 310 | * |
||
| 311 | * @param string $documentId id of entity to update |
||
| 312 | * @param Object $entity new entity |
||
| 313 | * @param bool $returnEntity true to return entity |
||
| 314 | * |
||
| 315 | * @return Object|null |
||
| 316 | */ |
||
| 317 | 3 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritDoc} |
||
| 342 | * |
||
| 343 | * @param string|object $id id of entity to delete or entity instance |
||
| 344 | * |
||
| 345 | * @return null|Object |
||
| 346 | */ |
||
| 347 | public function deleteRecord($id) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Triggers a flush on the DocumentManager |
||
| 368 | * |
||
| 369 | * @param null $document optional document |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function flush($document = null) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * A low level delete without any checks |
||
| 380 | * |
||
| 381 | * @param mixed $id record id |
||
| 382 | * |
||
| 383 | * @return void |
||
| 384 | */ |
||
| 385 | 2 | private function deleteById($id) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Checks in a performant way if a certain record id exists in the database |
||
| 397 | * |
||
| 398 | * @param mixed $id record id |
||
| 399 | * |
||
| 400 | * @return bool true if it exists, false otherwise |
||
| 401 | */ |
||
| 402 | 4 | public function recordExists($id) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 409 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 410 | * is a better way to get what you need. |
||
| 411 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 412 | * instance, make sure to pass false there. |
||
| 413 | * |
||
| 414 | * @param mixed $id record id |
||
| 415 | * @param array $fields list of fields you need. |
||
| 416 | * @param bool $hydrate whether to hydrate object or not |
||
| 417 | * |
||
| 418 | * @return array|null|object |
||
| 419 | */ |
||
| 420 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * get classname of entity |
||
| 437 | * |
||
| 438 | * @return string|null |
||
| 439 | */ |
||
| 440 | 4 | public function getEntityClass() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * {@inheritDoc} |
||
| 451 | * |
||
| 452 | * Currently this is being used to build the route id used for redirecting |
||
| 453 | * to newly made documents. It might benefit from having a different name |
||
| 454 | * for those purposes. |
||
| 455 | * |
||
| 456 | * We might use a convention based mapping here: |
||
| 457 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 458 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 459 | * |
||
| 460 | * @todo implement this in a more convention based manner |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public function getConnectionName() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Does the actual query using the RQL Bundle. |
||
| 473 | * |
||
| 474 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 475 | * @param Query $query query from parser |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 488 | * |
||
| 489 | * @param Object $record record |
||
| 490 | * |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | 14 | protected function checkIfOriginRecord($record) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 511 | * |
||
| 512 | * @return int |
||
| 513 | */ |
||
| 514 | private function getDefaultLimit() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param Boolean $active active |
||
| 525 | * @param String $field field |
||
| 526 | * @return void |
||
| 527 | */ |
||
| 528 | 4 | public function setFilterByAuthUser($active, $field) |
|
| 533 | } |
||
| 534 |
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.