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) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * {@inheritDoc} |
||
| 446 | * |
||
| 447 | * @param string|object $id id of entity to delete or entity instance |
||
| 448 | * |
||
| 449 | * @return null|Object |
||
| 450 | */ |
||
| 451 | public function deleteRecord($id) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Triggers a flush on the DocumentManager |
||
| 475 | * |
||
| 476 | * @param null $document optional document |
||
| 477 | * |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | public function flush($document = null) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * A low level delete without any checks |
||
| 487 | * |
||
| 488 | * @param mixed $id record id |
||
| 489 | * |
||
| 490 | * @return void |
||
| 491 | */ |
||
| 492 | private function deleteById($id) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Checks in a performant way if a certain record id exists in the database |
||
| 504 | * |
||
| 505 | * @param mixed $id record id |
||
| 506 | * |
||
| 507 | * @return bool true if it exists, false otherwise |
||
| 508 | */ |
||
| 509 | public function recordExists($id) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 516 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 517 | * is a better way to get what you need. |
||
| 518 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 519 | * instance, make sure to pass false there. |
||
| 520 | * |
||
| 521 | * @param mixed $id record id |
||
| 522 | * @param array $fields list of fields you need. |
||
| 523 | * @param bool $hydrate whether to hydrate object or not |
||
| 524 | * |
||
| 525 | * @return array|null|object |
||
| 526 | */ |
||
| 527 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * get classname of entity |
||
| 544 | * |
||
| 545 | * @return string|null |
||
| 546 | */ |
||
| 547 | public function getEntityClass() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * {@inheritDoc} |
||
| 558 | * |
||
| 559 | * Currently this is being used to build the route id used for redirecting |
||
| 560 | * to newly made documents. It might benefit from having a different name |
||
| 561 | * for those purposes. |
||
| 562 | * |
||
| 563 | * We might use a convention based mapping here: |
||
| 564 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 565 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 566 | * |
||
| 567 | * @todo implement this in a more convention based manner |
||
| 568 | * |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | public function getConnectionName() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Does the actual query using the RQL Bundle. |
||
| 580 | * |
||
| 581 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 582 | * @param Query $query query from parser |
||
| 583 | * |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 595 | * |
||
| 596 | * @param Object $record record |
||
| 597 | * |
||
| 598 | * @return void |
||
| 599 | */ |
||
| 600 | protected function checkIfOriginRecord($record) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 618 | * |
||
| 619 | * @return int |
||
| 620 | */ |
||
| 621 | private function getDefaultLimit() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @param Boolean $active active |
||
| 632 | * @param String $field field |
||
| 633 | * @return void |
||
| 634 | */ |
||
| 635 | public function setFilterByAuthUser($active, $field) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Security needs to be enabled to get Object. |
||
| 643 | * |
||
| 644 | * @return String|bool Username if available |
||
| 645 | */ |
||
| 646 | View Code Duplication | public function getSecurityUserUsername() |
|
| 656 | } |
||
| 657 |
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.