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  | 
            ||
| 28 | class DocumentModel extends SchemaModel implements ModelInterface  | 
            ||
| 29 | { | 
            ||
| 30 | /**  | 
            ||
| 31 | * @var string  | 
            ||
| 32 | */  | 
            ||
| 33 | protected $description;  | 
            ||
| 34 | /**  | 
            ||
| 35 | * @var string[]  | 
            ||
| 36 | */  | 
            ||
| 37 | protected $fieldTitles;  | 
            ||
| 38 | /**  | 
            ||
| 39 | * @var string[]  | 
            ||
| 40 | */  | 
            ||
| 41 | protected $fieldDescriptions;  | 
            ||
| 42 | /**  | 
            ||
| 43 | * @var string[]  | 
            ||
| 44 | */  | 
            ||
| 45 | protected $requiredFields = array();  | 
            ||
| 46 | /**  | 
            ||
| 47 | * @var string[]  | 
            ||
| 48 | */  | 
            ||
| 49 | protected $searchableFields = array();  | 
            ||
| 50 | /**  | 
            ||
| 51 | * @var DocumentRepository  | 
            ||
| 52 | */  | 
            ||
| 53 | private $repository;  | 
            ||
| 54 | /**  | 
            ||
| 55 | * @var Visitor  | 
            ||
| 56 | */  | 
            ||
| 57 | private $visitor;  | 
            ||
| 58 | /**  | 
            ||
| 59 | * @var array  | 
            ||
| 60 | */  | 
            ||
| 61 | protected $notModifiableOriginRecords;  | 
            ||
| 62 | /**  | 
            ||
| 63 | * @var integer  | 
            ||
| 64 | */  | 
            ||
| 65 | private $paginationDefaultLimit;  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * @var Logger  | 
            ||
| 69 | */  | 
            ||
| 70 | private $logger;  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * @var boolean  | 
            ||
| 74 | */  | 
            ||
| 75 | protected $filterByAuthUser;  | 
            ||
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * @var string  | 
            ||
| 79 | */  | 
            ||
| 80 | protected $filterByAuthField;  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * @var RqlTranslator  | 
            ||
| 84 | */  | 
            ||
| 85 | protected $translator;  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * @param Visitor $visitor rql query visitor  | 
            ||
| 89 | * @param RqlTranslator $translator Translator for query modification  | 
            ||
| 90 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values  | 
            ||
| 91 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context  | 
            ||
| 92 | * @param Logger $logger The defined system logger  | 
            ||
| 93 | */  | 
            ||
| 94 | public function __construct(  | 
            ||
| 108 | |||
| 109 | /**  | 
            ||
| 110 | * get repository instance  | 
            ||
| 111 | *  | 
            ||
| 112 | * @return DocumentRepository  | 
            ||
| 113 | */  | 
            ||
| 114 | public function getRepository()  | 
            ||
| 118 | |||
| 119 | /**  | 
            ||
| 120 | * create new app model  | 
            ||
| 121 | *  | 
            ||
| 122 | * @param DocumentRepository $repository Repository of countries  | 
            ||
| 123 | *  | 
            ||
| 124 | * @return \Graviton\RestBundle\Model\DocumentModel  | 
            ||
| 125 | */  | 
            ||
| 126 | public function setRepository(DocumentRepository $repository)  | 
            ||
| 132 | |||
| 133 | /**  | 
            ||
| 134 |      * {@inheritDoc} | 
            ||
| 135 | *  | 
            ||
| 136 | * @param Request $request The request object  | 
            ||
| 137 | * @param SecurityUser $user SecurityUser Object  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 138 | * @param SchemaDocument $schema Schema model used for search fields extraction  | 
            ||
| 139 | *  | 
            ||
| 140 | * @return array  | 
            ||
| 141 | */  | 
            ||
| 142 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null)  | 
            ||
| 249 | |||
| 250 | /**  | 
            ||
| 251 | * @return string the version of the MongoDB as a string  | 
            ||
| 252 | */  | 
            ||
| 253 | public function getMongoDBVersion()  | 
            ||
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert  | 
            ||
| 267 | *  | 
            ||
| 268 | * @return Object  | 
            ||
| 269 | */  | 
            ||
| 270 | View Code Duplication | public function insertRecord($entity)  | 
            |
| 279 | |||
| 280 | /**  | 
            ||
| 281 | * @param string $documentId id of entity to find  | 
            ||
| 282 | *  | 
            ||
| 283 | * @return Object  | 
            ||
| 284 | */  | 
            ||
| 285 | public function find($documentId)  | 
            ||
| 289 | |||
| 290 | /**  | 
            ||
| 291 |      * {@inheritDoc} | 
            ||
| 292 | *  | 
            ||
| 293 | * @param string $documentId id of entity to update  | 
            ||
| 294 | * @param Object $entity new entity  | 
            ||
| 295 | *  | 
            ||
| 296 | * @return Object  | 
            ||
| 297 | */  | 
            ||
| 298 | View Code Duplication | public function updateRecord($documentId, $entity)  | 
            |
| 309 | |||
| 310 | /**  | 
            ||
| 311 |      * {@inheritDoc} | 
            ||
| 312 | *  | 
            ||
| 313 | * @param string $documentId id of entity to delete  | 
            ||
| 314 | *  | 
            ||
| 315 | * @return null|Object  | 
            ||
| 316 | */  | 
            ||
| 317 | public function deleteRecord($documentId)  | 
            ||
| 332 | |||
| 333 | /**  | 
            ||
| 334 | * get classname of entity  | 
            ||
| 335 | *  | 
            ||
| 336 | * @return string  | 
            ||
| 337 | */  | 
            ||
| 338 | public function getEntityClass()  | 
            ||
| 342 | |||
| 343 | /**  | 
            ||
| 344 |      * {@inheritDoc} | 
            ||
| 345 | *  | 
            ||
| 346 | * Currently this is being used to build the route id used for redirecting  | 
            ||
| 347 | * to newly made documents. It might benefit from having a different name  | 
            ||
| 348 | * for those purposes.  | 
            ||
| 349 | *  | 
            ||
| 350 | * We might use a convention based mapping here:  | 
            ||
| 351 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core  | 
            ||
| 352 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core  | 
            ||
| 353 | *  | 
            ||
| 354 | * @todo implement this in a more convention based manner  | 
            ||
| 355 | *  | 
            ||
| 356 | * @return string  | 
            ||
| 357 | */  | 
            ||
| 358 | public function getConnectionName()  | 
            ||
| 364 | |||
| 365 | /**  | 
            ||
| 366 | * Does the actual query using the RQL Bundle.  | 
            ||
| 367 | *  | 
            ||
| 368 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder  | 
            ||
| 369 | * @param Query $query query from parser  | 
            ||
| 370 | *  | 
            ||
| 371 | * @return array  | 
            ||
| 372 | */  | 
            ||
| 373 | protected function doRqlQuery($queryBuilder, Query $query)  | 
            ||
| 379 | |||
| 380 | /**  | 
            ||
| 381 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed  | 
            ||
| 382 | *  | 
            ||
| 383 | * @param Object $record record  | 
            ||
| 384 | *  | 
            ||
| 385 | * @return void  | 
            ||
| 386 | */  | 
            ||
| 387 | protected function checkIfOriginRecord($record)  | 
            ||
| 402 | |||
| 403 | /**  | 
            ||
| 404 | * Determines the configured amount fo data records to be returned in pagination context.  | 
            ||
| 405 | *  | 
            ||
| 406 | * @return int  | 
            ||
| 407 | */  | 
            ||
| 408 | private function getDefaultLimit()  | 
            ||
| 416 | |||
| 417 | /**  | 
            ||
| 418 | * @param Boolean $active active  | 
            ||
| 419 | * @param String $field field  | 
            ||
| 420 | * @return void  | 
            ||
| 421 | */  | 
            ||
| 422 | public function setFilterByAuthUser($active, $field)  | 
            ||
| 427 | }  | 
            ||
| 428 | 
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.