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 string[] |
||
| 57 | */ |
||
| 58 | protected $textIndexes = array(); |
||
| 59 | /** |
||
| 60 | * @var DocumentRepository |
||
| 61 | */ |
||
| 62 | private $repository; |
||
| 63 | /** |
||
| 64 | * @var Visitor |
||
| 65 | */ |
||
| 66 | private $visitor; |
||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $notModifiableOriginRecords; |
||
| 71 | /** |
||
| 72 | * @var integer |
||
| 73 | */ |
||
| 74 | private $paginationDefaultLimit; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var boolean |
||
| 78 | */ |
||
| 79 | protected $filterByAuthUser; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $filterByAuthField; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var DocumentManager |
||
| 88 | */ |
||
| 89 | protected $manager; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Visitor $visitor rql query visitor |
||
| 93 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 94 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 95 | */ |
||
| 96 | public function __construct( |
||
| 106 | |||
| 107 | /** |
||
| 108 | * get repository instance |
||
| 109 | * |
||
| 110 | * @return DocumentRepository |
||
| 111 | */ |
||
| 112 | public function getRepository() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * create new app model |
||
| 119 | * |
||
| 120 | * @param DocumentRepository $repository Repository of countries |
||
| 121 | * |
||
| 122 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 123 | */ |
||
| 124 | public function setRepository(DocumentRepository $repository) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritDoc} |
||
| 134 | * |
||
| 135 | * @param Request $request The request object |
||
| 136 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function findAll(Request $request, SecurityUser $user = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param XiagQuery $xiagQuery Xiag Builder |
||
| 224 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Check if collection has search indexes in DB |
||
| 281 | * |
||
| 282 | * @param string $prefix the prefix for custom text search indexes |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Build Search text index |
||
| 305 | * |
||
| 306 | * @param MongoBuilder $queryBuilder Doctrine mongo query builder object |
||
| 307 | * @param SearchNode $searchNode Graviton Search node |
||
| 308 | * @return MongoBuilder |
||
| 309 | */ |
||
| 310 | private function buildSearchTextQuery(MongoBuilder $queryBuilder, SearchNode $searchNode) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return string the version of the MongoDB as a string |
||
| 326 | */ |
||
| 327 | private function getMongoDBVersion() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param object $entity entity to insert |
||
| 341 | * @param bool $returnEntity true to return entity |
||
| 342 | * @param bool $doFlush if we should flush or not after insert |
||
| 343 | * |
||
| 344 | * @return Object|null |
||
| 345 | */ |
||
| 346 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $documentId id of entity to find |
||
| 360 | * @param Request $request request |
||
| 361 | * |
||
| 362 | * @return Object |
||
| 363 | */ |
||
| 364 | public function find($documentId, Request $request = null) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * {@inheritDoc} |
||
| 394 | * |
||
| 395 | * @param string $documentId id of entity to update |
||
| 396 | * @param Object $entity new entity |
||
| 397 | * @param bool $returnEntity true to return entity |
||
| 398 | * |
||
| 399 | * @return Object|null |
||
| 400 | */ |
||
| 401 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 402 | { |
||
| 403 | if (!is_null($documentId)) { |
||
| 404 | $this->deleteById($documentId); |
||
| 405 | // detach so odm knows it's gone |
||
| 406 | $this->manager->detach($entity); |
||
| 407 | $this->manager->clear(); |
||
| 408 | } |
||
| 409 | |||
| 410 | $entity = $this->manager->merge($entity); |
||
| 411 | |||
| 412 | $this->manager->persist($entity); |
||
| 413 | $this->manager->flush($entity); |
||
| 414 | |||
| 415 | if ($returnEntity) { |
||
| 416 | return $entity; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritDoc} |
||
| 422 | * |
||
| 423 | * @param string|object $id id of entity to delete or entity instance |
||
| 424 | * |
||
| 425 | * @return null|Object |
||
| 426 | */ |
||
| 427 | public function deleteRecord($id) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Triggers a flush on the DocumentManager |
||
| 448 | * |
||
| 449 | * @param null $document optional document |
||
| 450 | * |
||
| 451 | * @return void |
||
| 452 | */ |
||
| 453 | public function flush($document = null) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * A low level delete without any checks |
||
| 460 | * |
||
| 461 | * @param mixed $id record id |
||
| 462 | * |
||
| 463 | * @return void |
||
| 464 | */ |
||
| 465 | private function deleteById($id) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Checks in a performant way if a certain record id exists in the database |
||
| 477 | * |
||
| 478 | * @param mixed $id record id |
||
| 479 | * |
||
| 480 | * @return bool true if it exists, false otherwise |
||
| 481 | */ |
||
| 482 | public function recordExists($id) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 489 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 490 | * is a better way to get what you need. |
||
| 491 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 492 | * instance, make sure to pass false there. |
||
| 493 | * |
||
| 494 | * @param mixed $id record id |
||
| 495 | * @param array $fields list of fields you need. |
||
| 496 | * @param bool $hydrate whether to hydrate object or not |
||
| 497 | * |
||
| 498 | * @return array|null|object |
||
| 499 | */ |
||
| 500 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * get classname of entity |
||
| 517 | * |
||
| 518 | * @return string|null |
||
| 519 | */ |
||
| 520 | public function getEntityClass() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * {@inheritDoc} |
||
| 531 | * |
||
| 532 | * Currently this is being used to build the route id used for redirecting |
||
| 533 | * to newly made documents. It might benefit from having a different name |
||
| 534 | * for those purposes. |
||
| 535 | * |
||
| 536 | * We might use a convention based mapping here: |
||
| 537 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 538 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 539 | * |
||
| 540 | * @todo implement this in a more convention based manner |
||
| 541 | * |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | public function getConnectionName() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Does the actual query using the RQL Bundle. |
||
| 553 | * |
||
| 554 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 555 | * @param Query $query query from parser |
||
| 556 | * |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 568 | * |
||
| 569 | * @param Object $record record |
||
| 570 | * |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | protected function checkIfOriginRecord($record) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 591 | * |
||
| 592 | * @return int |
||
| 593 | */ |
||
| 594 | private function getDefaultLimit() |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param Boolean $active active |
||
| 605 | * @param String $field field |
||
| 606 | * @return void |
||
| 607 | */ |
||
| 608 | public function setFilterByAuthUser($active, $field) |
||
| 613 | } |
||
| 614 |
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.