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 |
||
| 35 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $description; |
||
| 41 | /** |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | protected $fieldTitles; |
||
| 45 | /** |
||
| 46 | * @var string[] |
||
| 47 | */ |
||
| 48 | protected $fieldDescriptions; |
||
| 49 | /** |
||
| 50 | * @var string[] |
||
| 51 | */ |
||
| 52 | protected $requiredFields = array(); |
||
| 53 | /** |
||
| 54 | * @var string[] |
||
| 55 | */ |
||
| 56 | protected $searchableFields = array(); |
||
| 57 | /** |
||
| 58 | * @var string[] |
||
| 59 | */ |
||
| 60 | protected $textIndexes = array(); |
||
| 61 | /** |
||
| 62 | * @var DocumentRepository |
||
| 63 | */ |
||
| 64 | private $repository; |
||
| 65 | /** |
||
| 66 | * @var Visitor |
||
| 67 | */ |
||
| 68 | private $visitor; |
||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $notModifiableOriginRecords; |
||
| 73 | /** |
||
| 74 | * @var integer |
||
| 75 | */ |
||
| 76 | private $paginationDefaultLimit; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var boolean |
||
| 80 | */ |
||
| 81 | protected $filterByAuthUser; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $filterByAuthField; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var DocumentManager |
||
| 90 | */ |
||
| 91 | protected $manager; |
||
| 92 | |||
| 93 | /** @var EventDispatcher */ |
||
| 94 | protected $eventDispatcher; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param Visitor $visitor rql query visitor |
||
| 98 | * @param EventDispatcher $eventDispatcher Kernel event dispatcher |
||
| 99 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 100 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination cnt |
||
| 101 | */ |
||
| 102 | public function __construct( |
||
| 114 | |||
| 115 | /** |
||
| 116 | * get repository instance |
||
| 117 | * |
||
| 118 | * @return DocumentRepository |
||
| 119 | */ |
||
| 120 | public function getRepository() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * create new app model |
||
| 127 | * |
||
| 128 | * @param DocumentRepository $repository Repository of countries |
||
| 129 | * |
||
| 130 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 131 | */ |
||
| 132 | public function setRepository(DocumentRepository $repository) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritDoc} |
||
| 142 | * |
||
| 143 | * @param Request $request The request object |
||
| 144 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function findAll(Request $request, SecurityUser $user = null) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param XiagQuery $xiagQuery Xiag Builder |
||
| 232 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Check if collection has search indexes in DB |
||
| 289 | * |
||
| 290 | * @param string $prefix the prefix for custom text search indexes |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Build Search text index |
||
| 313 | * |
||
| 314 | * @param MongoBuilder $queryBuilder Doctrine mongo query builder object |
||
| 315 | * @param SearchNode $searchNode Graviton Search node |
||
| 316 | * @return MongoBuilder |
||
| 317 | */ |
||
| 318 | private function buildSearchTextQuery(MongoBuilder $queryBuilder, SearchNode $searchNode) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return string the version of the MongoDB as a string |
||
| 334 | */ |
||
| 335 | private function getMongoDBVersion() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param object $entity entity to insert |
||
| 349 | * @param bool $returnEntity true to return entity |
||
| 350 | * @param bool $doFlush if we should flush or not after insert |
||
| 351 | * |
||
| 352 | * @return Object|null |
||
| 353 | */ |
||
| 354 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $documentId id of entity to find |
||
| 372 | * @param Request $request request |
||
| 373 | * |
||
| 374 | * @return Object |
||
| 375 | */ |
||
| 376 | public function find($documentId, Request $request = null) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritDoc} |
||
| 406 | * |
||
| 407 | * @param string $documentId id of entity to update |
||
| 408 | * @param Object $entity new entity |
||
| 409 | * @param bool $returnEntity true to return entity |
||
| 410 | * |
||
| 411 | * @return Object|null |
||
| 412 | */ |
||
| 413 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * {@inheritDoc} |
||
| 437 | * |
||
| 438 | * @param string|object $id id of entity to delete or entity instance |
||
| 439 | * |
||
| 440 | * @return null|Object |
||
| 441 | */ |
||
| 442 | public function deleteRecord($id) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Triggers a flush on the DocumentManager |
||
| 469 | * |
||
| 470 | * @param null $document optional document |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | public function flush($document = null) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * A low level delete without any checks |
||
| 481 | * |
||
| 482 | * @param mixed $id record id |
||
| 483 | * |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | private function deleteById($id) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Checks in a performant way if a certain record id exists in the database |
||
| 498 | * |
||
| 499 | * @param mixed $id record id |
||
| 500 | * |
||
| 501 | * @return bool true if it exists, false otherwise |
||
| 502 | */ |
||
| 503 | public function recordExists($id) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 510 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 511 | * is a better way to get what you need. |
||
| 512 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 513 | * instance, make sure to pass false there. |
||
| 514 | * |
||
| 515 | * @param mixed $id record id |
||
| 516 | * @param array $fields list of fields you need. |
||
| 517 | * @param bool $hydrate whether to hydrate object or not |
||
| 518 | * |
||
| 519 | * @return array|null|object |
||
| 520 | */ |
||
| 521 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * get classname of entity |
||
| 538 | * |
||
| 539 | * @return string|null |
||
| 540 | */ |
||
| 541 | public function getEntityClass() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * {@inheritDoc} |
||
| 552 | * |
||
| 553 | * Currently this is being used to build the route id used for redirecting |
||
| 554 | * to newly made documents. It might benefit from having a different name |
||
| 555 | * for those purposes. |
||
| 556 | * |
||
| 557 | * We might use a convention based mapping here: |
||
| 558 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 559 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 560 | * |
||
| 561 | * @todo implement this in a more convention based manner |
||
| 562 | * |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | public function getConnectionName() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Does the actual query using the RQL Bundle. |
||
| 574 | * |
||
| 575 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 576 | * @param Query $query query from parser |
||
| 577 | * |
||
| 578 | * @return array |
||
| 579 | */ |
||
| 580 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 589 | * |
||
| 590 | * @param Object $record record |
||
| 591 | * |
||
| 592 | * @return void |
||
| 593 | */ |
||
| 594 | protected function checkIfOriginRecord($record) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 612 | * |
||
| 613 | * @return int |
||
| 614 | */ |
||
| 615 | private function getDefaultLimit() |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Will fire a ModelEvent |
||
| 626 | * |
||
| 627 | * @param string $action insert or update |
||
| 628 | * @param Object $collection the changed Document |
||
| 629 | * |
||
| 630 | * @return void |
||
| 631 | */ |
||
| 632 | private function dispatchModelEvent($action, $collection) |
||
| 647 | } |
||
| 648 |
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.