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 | /** @var EventDispatcher */ |
||
| 92 | protected $eventDispatcher; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param Visitor $visitor rql query visitor |
||
| 96 | * @param EventDispatcher $eventDispatcher Kernel event dispatcher |
||
| 97 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 98 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination cnt |
||
| 99 | */ |
||
| 100 | public function __construct( |
||
| 112 | |||
| 113 | /** |
||
| 114 | * get repository instance |
||
| 115 | * |
||
| 116 | * @return DocumentRepository |
||
| 117 | */ |
||
| 118 | public function getRepository() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * create new app model |
||
| 125 | * |
||
| 126 | * @param DocumentRepository $repository Repository of countries |
||
| 127 | * |
||
| 128 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 129 | */ |
||
| 130 | public function setRepository(DocumentRepository $repository) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritDoc} |
||
| 140 | * |
||
| 141 | * @param Request $request The request object |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public function findAll(Request $request) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param XiagQuery $xiagQuery Xiag Builder |
||
| 229 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Check if collection has search indexes in DB |
||
| 286 | * |
||
| 287 | * @param string $prefix the prefix for custom text search indexes |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Build Search text index |
||
| 310 | * |
||
| 311 | * @param MongoBuilder $queryBuilder Doctrine mongo query builder object |
||
| 312 | * @param SearchNode $searchNode Graviton Search node |
||
| 313 | * @return MongoBuilder |
||
| 314 | */ |
||
| 315 | private function buildSearchTextQuery(MongoBuilder $queryBuilder, SearchNode $searchNode) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return string the version of the MongoDB as a string |
||
| 331 | */ |
||
| 332 | private function getMongoDBVersion() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param object $entity entity to insert |
||
| 346 | * @param bool $returnEntity true to return entity |
||
| 347 | * @param bool $doFlush if we should flush or not after insert |
||
| 348 | * |
||
| 349 | * @return Object|null |
||
| 350 | */ |
||
| 351 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $documentId id of entity to find |
||
| 369 | * @param Request $request request |
||
| 370 | * |
||
| 371 | * @return Object |
||
| 372 | */ |
||
| 373 | public function find($documentId, Request $request = null) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * {@inheritDoc} |
||
| 403 | * |
||
| 404 | * @param string $documentId id of entity to update |
||
| 405 | * @param Object $entity new entity |
||
| 406 | * @param bool $returnEntity true to return entity |
||
| 407 | * |
||
| 408 | * @return Object|null |
||
| 409 | */ |
||
| 410 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * {@inheritDoc} |
||
| 434 | * |
||
| 435 | * @param string|object $id id of entity to delete or entity instance |
||
| 436 | * |
||
| 437 | * @return null|Object |
||
| 438 | */ |
||
| 439 | public function deleteRecord($id) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Triggers a flush on the DocumentManager |
||
| 465 | * |
||
| 466 | * @param null $document optional document |
||
| 467 | * |
||
| 468 | * @return void |
||
| 469 | */ |
||
| 470 | public function flush($document = null) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * A low level delete without any checks |
||
| 477 | * |
||
| 478 | * @param mixed $id record id |
||
| 479 | * |
||
| 480 | * @return void |
||
| 481 | */ |
||
| 482 | private function deleteById($id) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Checks in a performant way if a certain record id exists in the database |
||
| 494 | * |
||
| 495 | * @param mixed $id record id |
||
| 496 | * |
||
| 497 | * @return bool true if it exists, false otherwise |
||
| 498 | */ |
||
| 499 | public function recordExists($id) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 506 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 507 | * is a better way to get what you need. |
||
| 508 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 509 | * instance, make sure to pass false there. |
||
| 510 | * |
||
| 511 | * @param mixed $id record id |
||
| 512 | * @param array $fields list of fields you need. |
||
| 513 | * @param bool $hydrate whether to hydrate object or not |
||
| 514 | * |
||
| 515 | * @return array|null|object |
||
| 516 | */ |
||
| 517 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * get classname of entity |
||
| 534 | * |
||
| 535 | * @return string|null |
||
| 536 | */ |
||
| 537 | public function getEntityClass() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * {@inheritDoc} |
||
| 548 | * |
||
| 549 | * Currently this is being used to build the route id used for redirecting |
||
| 550 | * to newly made documents. It might benefit from having a different name |
||
| 551 | * for those purposes. |
||
| 552 | * |
||
| 553 | * We might use a convention based mapping here: |
||
| 554 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 555 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 556 | * |
||
| 557 | * @todo implement this in a more convention based manner |
||
| 558 | * |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function getConnectionName() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Does the actual query using the RQL Bundle. |
||
| 570 | * |
||
| 571 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 572 | * @param Query $query query from parser |
||
| 573 | * |
||
| 574 | * @return array |
||
| 575 | */ |
||
| 576 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 585 | * |
||
| 586 | * @param Object $record record |
||
| 587 | * |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | protected function checkIfOriginRecord($record) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 608 | * |
||
| 609 | * @return int |
||
| 610 | */ |
||
| 611 | private function getDefaultLimit() |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Will fire a ModelEvent |
||
| 622 | * |
||
| 623 | * @param string $action insert or update |
||
| 624 | * @param Object $collection the changed Document |
||
| 625 | * |
||
| 626 | * @return void |
||
| 627 | */ |
||
| 628 | private function dispatchModelEvent($action, $collection) |
||
| 646 | } |
||
| 647 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: