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 | /** @var $collectionCache */ |
||
| 97 | protected $cache; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var RestUtils |
||
| 101 | */ |
||
| 102 | private $restUtils; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param Visitor $visitor rql query visitor |
||
| 106 | * @param RestUtils $restUtils Rest utils |
||
| 107 | * @param EventDispatcher $eventDispatcher Kernel event dispatcher |
||
| 108 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 109 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination cnt |
||
| 110 | */ |
||
| 111 | public function __construct( |
||
| 125 | |||
| 126 | /** |
||
| 127 | * get repository instance |
||
| 128 | * |
||
| 129 | * @return DocumentRepository |
||
| 130 | */ |
||
| 131 | public function getRepository() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * create new app model |
||
| 138 | * |
||
| 139 | * @param DocumentRepository $repository Repository of countries |
||
| 140 | * |
||
| 141 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 142 | */ |
||
| 143 | public function setRepository(DocumentRepository $repository) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritDoc} |
||
| 153 | * |
||
| 154 | * @param Request $request The request object |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public function findAll(Request $request) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Check if collection has search indexes in DB |
||
| 249 | * |
||
| 250 | * @param string $prefix the prefix for custom text search indexes |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | private function hasCustomSearchIndex($prefix = 'search_') |
||
| 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) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $documentId id of entity to find |
||
| 300 | * |
||
| 301 | * @throws NotFoundException |
||
| 302 | * @return Object |
||
| 303 | */ |
||
| 304 | public function find($documentId) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Will attempt to find Document by ID. |
||
| 317 | * If config cache is enabled for document it will save it. |
||
| 318 | * |
||
| 319 | * @param string $documentId id of entity to find |
||
| 320 | * @param Request $request request |
||
| 321 | * @param bool $skipLock if true, we don't check for the lock |
||
| 322 | * |
||
| 323 | * @throws NotFoundException |
||
| 324 | * @return string Serialised object |
||
| 325 | */ |
||
| 326 | public function getSerialised($documentId, Request $request = null, $skipLock = false) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * {@inheritDoc} |
||
| 349 | * |
||
| 350 | * @param string $documentId id of entity to update |
||
| 351 | * @param Object $entity new entity |
||
| 352 | * @param bool $returnEntity true to return entity |
||
| 353 | * |
||
| 354 | * @return Object|null |
||
| 355 | */ |
||
| 356 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritDoc} |
||
| 381 | * |
||
| 382 | * @param string|object $id id of entity to delete or entity instance |
||
| 383 | * |
||
| 384 | * @return null|Object |
||
| 385 | */ |
||
| 386 | public function deleteRecord($id) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Triggers a flush on the DocumentManager |
||
| 412 | * |
||
| 413 | * @param null $document optional document |
||
| 414 | * |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | public function flush($document = null) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * A low level delete without any checks |
||
| 424 | * |
||
| 425 | * @param mixed $id record id |
||
| 426 | * |
||
| 427 | * @return void |
||
| 428 | */ |
||
| 429 | private function deleteById($id) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Checks in a performant way if a certain record id exists in the database |
||
| 441 | * |
||
| 442 | * @param mixed $id record id |
||
| 443 | * |
||
| 444 | * @return bool true if it exists, false otherwise |
||
| 445 | */ |
||
| 446 | public function recordExists($id) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 453 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 454 | * is a better way to get what you need. |
||
| 455 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 456 | * instance, make sure to pass false there. |
||
| 457 | * |
||
| 458 | * @param mixed $id record id |
||
| 459 | * @param array $fields list of fields you need. |
||
| 460 | * @param bool $hydrate whether to hydrate object or not |
||
| 461 | * |
||
| 462 | * @return array|null|object |
||
| 463 | */ |
||
| 464 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * get classname of entity |
||
| 481 | * |
||
| 482 | * @return string|null |
||
| 483 | */ |
||
| 484 | public function getEntityClass() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * {@inheritDoc} |
||
| 495 | * |
||
| 496 | * Currently this is being used to build the route id used for redirecting |
||
| 497 | * to newly made documents. It might benefit from having a different name |
||
| 498 | * for those purposes. |
||
| 499 | * |
||
| 500 | * We might use a convention based mapping here: |
||
| 501 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 502 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 503 | * |
||
| 504 | * @todo implement this in a more convention based manner |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public function getConnectionName() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Does the actual query using the RQL Bundle. |
||
| 517 | * |
||
| 518 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 519 | * @param Query $query query from parser |
||
| 520 | * |
||
| 521 | * @return Builder|Expr |
||
| 522 | */ |
||
| 523 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 532 | * |
||
| 533 | * @param Object $record record |
||
| 534 | * |
||
| 535 | * @return void |
||
| 536 | */ |
||
| 537 | protected function checkIfOriginRecord($record) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 555 | * |
||
| 556 | * @return int |
||
| 557 | */ |
||
| 558 | private function getDefaultLimit() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Will fire a ModelEvent |
||
| 569 | * |
||
| 570 | * @param string $action insert or update |
||
| 571 | * @param Object $collection the changed Document |
||
| 572 | * |
||
| 573 | * @return void |
||
| 574 | */ |
||
| 575 | private function dispatchModelEvent($action, $collection) |
||
| 593 | } |
||
| 594 |
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: