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 DocumentRepository |
||
| 57 | */ |
||
| 58 | private $repository; |
||
| 59 | /** |
||
| 60 | * @var Visitor |
||
| 61 | */ |
||
| 62 | private $visitor; |
||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $notModifiableOriginRecords; |
||
| 67 | /** |
||
| 68 | * @var integer |
||
| 69 | */ |
||
| 70 | private $paginationDefaultLimit; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var boolean |
||
| 74 | */ |
||
| 75 | protected $filterByAuthUser; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $filterByAuthField; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var DocumentManager |
||
| 84 | */ |
||
| 85 | protected $manager; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param Visitor $visitor rql query visitor |
||
| 89 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 90 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 91 | */ |
||
| 92 | 4 | public function __construct( |
|
| 102 | |||
| 103 | /** |
||
| 104 | * get repository instance |
||
| 105 | * |
||
| 106 | * @return DocumentRepository |
||
| 107 | */ |
||
| 108 | 2 | public function getRepository() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * create new app model |
||
| 115 | * |
||
| 116 | * @param DocumentRepository $repository Repository of countries |
||
| 117 | * |
||
| 118 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 119 | */ |
||
| 120 | 4 | public function setRepository(DocumentRepository $repository) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritDoc} |
||
| 130 | * |
||
| 131 | * @param Request $request The request object |
||
| 132 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 133 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param XiagQuery $xiagQuery Xiag Builder |
||
| 222 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $prefix the prefix for custom text search indexes |
||
| 278 | * @return bool |
||
| 279 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 280 | */ |
||
| 281 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return string the version of the MongoDB as a string |
||
| 295 | */ |
||
| 296 | private function getMongoDBVersion() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param object $entity entity to insert |
||
| 310 | * @param bool $returnEntity true to return entity |
||
| 311 | * @param bool $doFlush if we should flush or not after insert |
||
| 312 | * |
||
| 313 | * @return Object|null |
||
| 314 | */ |
||
| 315 | 1 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $documentId id of entity to find |
||
| 330 | * |
||
| 331 | * @return Object |
||
| 332 | */ |
||
| 333 | 4 | public function find($documentId) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritDoc} |
||
| 340 | * |
||
| 341 | * @param string $documentId id of entity to update |
||
| 342 | * @param Object $entity new entity |
||
| 343 | * @param bool $returnEntity true to return entity |
||
| 344 | * |
||
| 345 | * @return Object|null |
||
| 346 | */ |
||
| 347 | 2 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * {@inheritDoc} |
||
| 372 | * |
||
| 373 | * @param string|object $id id of entity to delete or entity instance |
||
| 374 | * |
||
| 375 | * @return null|Object |
||
| 376 | */ |
||
| 377 | public function deleteRecord($id) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Triggers a flush on the DocumentManager |
||
| 398 | * |
||
| 399 | * @param null $document optional document |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function flush($document = null) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * A low level delete without any checks |
||
| 410 | * |
||
| 411 | * @param mixed $id record id |
||
| 412 | * |
||
| 413 | * @return void |
||
| 414 | */ |
||
| 415 | 2 | private function deleteById($id) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Checks in a performant way if a certain record id exists in the database |
||
| 427 | * |
||
| 428 | * @param mixed $id record id |
||
| 429 | * |
||
| 430 | * @return bool true if it exists, false otherwise |
||
| 431 | */ |
||
| 432 | 4 | public function recordExists($id) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 439 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 440 | * is a better way to get what you need. |
||
| 441 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 442 | * instance, make sure to pass false there. |
||
| 443 | * |
||
| 444 | * @param mixed $id record id |
||
| 445 | * @param array $fields list of fields you need. |
||
| 446 | * @param bool $hydrate whether to hydrate object or not |
||
| 447 | * |
||
| 448 | * @return array|null|object |
||
| 449 | */ |
||
| 450 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * get classname of entity |
||
| 467 | * |
||
| 468 | * @return string|null |
||
| 469 | */ |
||
| 470 | 4 | public function getEntityClass() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * {@inheritDoc} |
||
| 481 | * |
||
| 482 | * Currently this is being used to build the route id used for redirecting |
||
| 483 | * to newly made documents. It might benefit from having a different name |
||
| 484 | * for those purposes. |
||
| 485 | * |
||
| 486 | * We might use a convention based mapping here: |
||
| 487 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 488 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 489 | * |
||
| 490 | * @todo implement this in a more convention based manner |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public function getConnectionName() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Does the actual query using the RQL Bundle. |
||
| 503 | * |
||
| 504 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 505 | * @param Query $query query from parser |
||
| 506 | * |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 518 | * |
||
| 519 | * @param Object $record record |
||
| 520 | * |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | 14 | protected function checkIfOriginRecord($record) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 541 | * |
||
| 542 | * @return int |
||
| 543 | */ |
||
| 544 | private function getDefaultLimit() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param Boolean $active active |
||
| 555 | * @param String $field field |
||
| 556 | * @return void |
||
| 557 | */ |
||
| 558 | 4 | public function setFilterByAuthUser($active, $field) |
|
| 563 | } |
||
| 564 |
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.