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 |
||
| 34 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $description; |
||
| 40 | /** |
||
| 41 | * @var string[] |
||
| 42 | */ |
||
| 43 | protected $fieldTitles; |
||
| 44 | /** |
||
| 45 | * @var string[] |
||
| 46 | */ |
||
| 47 | protected $fieldDescriptions; |
||
| 48 | /** |
||
| 49 | * @var string[] |
||
| 50 | */ |
||
| 51 | protected $requiredFields = array(); |
||
| 52 | /** |
||
| 53 | * @var string[] |
||
| 54 | */ |
||
| 55 | protected $searchableFields = array(); |
||
| 56 | /** |
||
| 57 | * @var DocumentRepository |
||
| 58 | */ |
||
| 59 | private $repository; |
||
| 60 | /** |
||
| 61 | * @var Visitor |
||
| 62 | */ |
||
| 63 | private $visitor; |
||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $notModifiableOriginRecords; |
||
| 68 | /** |
||
| 69 | * @var integer |
||
| 70 | */ |
||
| 71 | private $paginationDefaultLimit; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var boolean |
||
| 75 | */ |
||
| 76 | protected $filterByAuthUser; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $filterByAuthField; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var RqlTranslator |
||
| 85 | */ |
||
| 86 | protected $translator; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var DocumentManager |
||
| 90 | */ |
||
| 91 | protected $manager; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param Visitor $visitor rql query visitor |
||
| 95 | * @param RqlTranslator $translator Translator for query modification |
||
| 96 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 97 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 98 | */ |
||
| 99 | 4 | public function __construct( |
|
| 111 | |||
| 112 | /** |
||
| 113 | * get repository instance |
||
| 114 | * |
||
| 115 | * @return DocumentRepository |
||
| 116 | */ |
||
| 117 | 2 | public function getRepository() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * create new app model |
||
| 124 | * |
||
| 125 | * @param DocumentRepository $repository Repository of countries |
||
| 126 | * |
||
| 127 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 128 | */ |
||
| 129 | 4 | public function setRepository(DocumentRepository $repository) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritDoc} |
||
| 139 | * |
||
| 140 | * @param Request $request The request object |
||
| 141 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 142 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $prefix the prefix for custom text search indexes |
||
| 249 | * @return bool |
||
| 250 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 251 | */ |
||
| 252 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string the version of the MongoDB as a string |
||
| 266 | */ |
||
| 267 | private function getMongoDBVersion() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param object $entity entity to insert |
||
| 281 | * @param bool $returnEntity true to return entity |
||
| 282 | * @param bool $doFlush if we should flush or not after insert |
||
| 283 | * |
||
| 284 | * @return Object|null |
||
| 285 | */ |
||
| 286 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $documentId id of entity to find |
||
| 301 | * |
||
| 302 | * @return Object |
||
| 303 | */ |
||
| 304 | 4 | public function find($documentId) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritDoc} |
||
| 311 | * |
||
| 312 | * @param string $documentId id of entity to update |
||
| 313 | * @param Object $entity new entity |
||
| 314 | * @param bool $returnEntity true to return entity |
||
| 315 | * |
||
| 316 | * @return Object|null |
||
| 317 | */ |
||
| 318 | 2 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * {@inheritDoc} |
||
| 343 | * |
||
| 344 | * @param string|object $id id of entity to delete or entity instance |
||
| 345 | * |
||
| 346 | * @return null|Object |
||
| 347 | */ |
||
| 348 | public function deleteRecord($id) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Triggers a flush on the DocumentManager |
||
| 369 | * |
||
| 370 | * @param null $document optional document |
||
| 371 | * |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | public function flush($document = null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * A low level delete without any checks |
||
| 381 | * |
||
| 382 | * @param mixed $id record id |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | 2 | private function deleteById($id) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Checks in a performant way if a certain record id exists in the database |
||
| 398 | * |
||
| 399 | * @param mixed $id record id |
||
| 400 | * |
||
| 401 | * @return bool true if it exists, false otherwise |
||
| 402 | */ |
||
| 403 | 4 | public function recordExists($id) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 410 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 411 | * is a better way to get what you need. |
||
| 412 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 413 | * instance, make sure to pass false there. |
||
| 414 | * |
||
| 415 | * @param mixed $id record id |
||
| 416 | * @param array $fields list of fields you need. |
||
| 417 | * @param bool $hydrate whether to hydrate object or not |
||
| 418 | * |
||
| 419 | * @return array|null|object |
||
| 420 | */ |
||
| 421 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * get classname of entity |
||
| 438 | * |
||
| 439 | * @return string|null |
||
| 440 | */ |
||
| 441 | 4 | public function getEntityClass() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * {@inheritDoc} |
||
| 452 | * |
||
| 453 | * Currently this is being used to build the route id used for redirecting |
||
| 454 | * to newly made documents. It might benefit from having a different name |
||
| 455 | * for those purposes. |
||
| 456 | * |
||
| 457 | * We might use a convention based mapping here: |
||
| 458 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 459 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 460 | * |
||
| 461 | * @todo implement this in a more convention based manner |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function getConnectionName() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Does the actual query using the RQL Bundle. |
||
| 474 | * |
||
| 475 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 476 | * @param Query $query query from parser |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 489 | * |
||
| 490 | * @param Object $record record |
||
| 491 | * |
||
| 492 | * @return void |
||
| 493 | */ |
||
| 494 | 14 | protected function checkIfOriginRecord($record) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 512 | * |
||
| 513 | * @return int |
||
| 514 | */ |
||
| 515 | private function getDefaultLimit() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @param Boolean $active active |
||
| 526 | * @param String $field field |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | 4 | public function setFilterByAuthUser($active, $field) |
|
| 534 | } |
||
| 535 |
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.