Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 37 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $description; |
||
| 43 | /** |
||
| 44 | * @var string[] |
||
| 45 | */ |
||
| 46 | protected $fieldTitles; |
||
| 47 | /** |
||
| 48 | * @var string[] |
||
| 49 | */ |
||
| 50 | protected $fieldDescriptions; |
||
| 51 | /** |
||
| 52 | * @var string[] |
||
| 53 | */ |
||
| 54 | protected $requiredFields = array(); |
||
| 55 | /** |
||
| 56 | * @var string[] |
||
| 57 | */ |
||
| 58 | protected $searchableFields = array(); |
||
| 59 | /** |
||
| 60 | * @var string[] |
||
| 61 | */ |
||
| 62 | protected $textIndexes = array(); |
||
| 63 | /** |
||
| 64 | * @var DocumentRepository |
||
| 65 | */ |
||
| 66 | private $repository; |
||
| 67 | /** |
||
| 68 | * @var Visitor |
||
| 69 | */ |
||
| 70 | private $visitor; |
||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $notModifiableOriginRecords; |
||
| 75 | /** |
||
| 76 | * @var integer |
||
| 77 | */ |
||
| 78 | private $paginationDefaultLimit; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var boolean |
||
| 82 | */ |
||
| 83 | protected $filterByAuthUser; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $filterByAuthField; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var DocumentManager |
||
| 92 | */ |
||
| 93 | protected $manager; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var TokenStorage |
||
| 97 | */ |
||
| 98 | protected $tokenStorage; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param Visitor $visitor rql query visitor |
||
| 102 | * @param TokenStorage $tokenStorage Sf Security Token Storage |
||
| 103 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 104 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 105 | */ |
||
| 106 | public function __construct( |
||
| 118 | |||
| 119 | /** |
||
| 120 | * get repository instance |
||
| 121 | * |
||
| 122 | * @return DocumentRepository |
||
| 123 | */ |
||
| 124 | public function getRepository() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * create new app model |
||
| 131 | * |
||
| 132 | * @param DocumentRepository $repository Repository of countries |
||
| 133 | * |
||
| 134 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 135 | */ |
||
| 136 | public function setRepository(DocumentRepository $repository) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritDoc} |
||
| 146 | * |
||
| 147 | * @param Request $request The request object |
||
| 148 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function findAll(Request $request, SecurityUser $user = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param XiagQuery $xiagQuery Xiag Builder |
||
| 236 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Check if collection has search indexes in DB |
||
| 293 | * |
||
| 294 | * @param string $prefix the prefix for custom text search indexes |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Build Search text index |
||
| 317 | * |
||
| 318 | * @param MongoBuilder $queryBuilder Doctrine mongo query builder object |
||
| 319 | * @param SearchNode $searchNode Graviton Search node |
||
| 320 | * @return MongoBuilder |
||
| 321 | */ |
||
| 322 | private function buildSearchTextQuery(MongoBuilder $queryBuilder, SearchNode $searchNode) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return string the version of the MongoDB as a string |
||
| 338 | */ |
||
| 339 | private function getMongoDBVersion() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param object $entity entity to insert |
||
| 353 | * @param bool $returnEntity true to return entity |
||
| 354 | * @param bool $doFlush if we should flush or not after insert |
||
| 355 | * |
||
| 356 | * @return Object|null |
||
| 357 | */ |
||
| 358 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $documentId id of entity to find |
||
| 378 | * @param Request $request request |
||
| 379 | * |
||
| 380 | * @return Object |
||
| 381 | */ |
||
| 382 | public function find($documentId, Request $request = null) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * {@inheritDoc} |
||
| 412 | * |
||
| 413 | * @param string $documentId id of entity to update |
||
| 414 | * @param Object $entity new entity |
||
| 415 | * @param bool $returnEntity true to return entity |
||
| 416 | * |
||
| 417 | * @return Object|null |
||
| 418 | */ |
||
| 419 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * {@inheritDoc} |
||
| 461 | * |
||
| 462 | * @param string|object $id id of entity to delete or entity instance |
||
| 463 | * |
||
| 464 | * @return null|Object |
||
| 465 | */ |
||
| 466 | public function deleteRecord($id) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Triggers a flush on the DocumentManager |
||
| 490 | * |
||
| 491 | * @param null $document optional document |
||
| 492 | * |
||
| 493 | * @return void |
||
| 494 | */ |
||
| 495 | public function flush($document = null) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * A low level delete without any checks |
||
| 502 | * |
||
| 503 | * @param mixed $id record id |
||
| 504 | * |
||
| 505 | * @return void |
||
| 506 | */ |
||
| 507 | private function deleteById($id) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Checks in a performant way if a certain record id exists in the database |
||
| 519 | * |
||
| 520 | * @param mixed $id record id |
||
| 521 | * |
||
| 522 | * @return bool true if it exists, false otherwise |
||
| 523 | */ |
||
| 524 | public function recordExists($id) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 531 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 532 | * is a better way to get what you need. |
||
| 533 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 534 | * instance, make sure to pass false there. |
||
| 535 | * |
||
| 536 | * @param mixed $id record id |
||
| 537 | * @param array $fields list of fields you need. |
||
| 538 | * @param bool $hydrate whether to hydrate object or not |
||
| 539 | * |
||
| 540 | * @return array|null|object |
||
| 541 | */ |
||
| 542 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * get classname of entity |
||
| 559 | * |
||
| 560 | * @return string|null |
||
| 561 | */ |
||
| 562 | public function getEntityClass() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * {@inheritDoc} |
||
| 573 | * |
||
| 574 | * Currently this is being used to build the route id used for redirecting |
||
| 575 | * to newly made documents. It might benefit from having a different name |
||
| 576 | * for those purposes. |
||
| 577 | * |
||
| 578 | * We might use a convention based mapping here: |
||
| 579 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 580 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 581 | * |
||
| 582 | * @todo implement this in a more convention based manner |
||
| 583 | * |
||
| 584 | * @return string |
||
| 585 | */ |
||
| 586 | public function getConnectionName() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Does the actual query using the RQL Bundle. |
||
| 595 | * |
||
| 596 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 597 | * @param Query $query query from parser |
||
| 598 | * |
||
| 599 | * @return array |
||
| 600 | */ |
||
| 601 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 610 | * |
||
| 611 | * @param Object $record record |
||
| 612 | * |
||
| 613 | * @return void |
||
| 614 | */ |
||
| 615 | protected function checkIfOriginRecord($record) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 633 | * |
||
| 634 | * @return int |
||
| 635 | */ |
||
| 636 | private function getDefaultLimit() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @param Boolean $active active |
||
| 647 | * @param String $field field |
||
| 648 | * @return void |
||
| 649 | */ |
||
| 650 | public function setFilterByAuthUser($active, $field) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Security needs to be enabled to get Object. |
||
| 658 | * |
||
| 659 | * @return String|bool Username if available |
||
| 660 | */ |
||
| 661 | View Code Duplication | public function getSecurityUserUsername() |
|
| 671 | } |
||
| 672 |
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.