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:
| 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) |
||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * @return string the version of the MongoDB as a string |
||
| 244 | */ |
||
| 245 | public function getMongoDBVersion() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
||
| 259 | * |
||
| 260 | * @return Object |
||
| 261 | */ |
||
| 262 | View Code Duplication | public function insertRecord($entity) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $documentId id of entity to find |
||
| 274 | * |
||
| 275 | * @return Object |
||
| 276 | */ |
||
| 277 | public function find($documentId) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritDoc} |
||
| 284 | * |
||
| 285 | * @param string $documentId id of entity to update |
||
| 286 | * @param Object $entity new entity |
||
| 287 | * |
||
| 288 | * @return Object |
||
| 289 | */ |
||
| 290 | View Code Duplication | public function updateRecord($documentId, $entity) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritDoc} |
||
| 304 | * |
||
| 305 | * @param string $documentId id of entity to delete |
||
| 306 | * |
||
| 307 | * @return null|Object |
||
| 308 | */ |
||
| 309 | public function deleteRecord($documentId) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * get classname of entity |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function getEntityClass() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritDoc} |
||
| 337 | * |
||
| 338 | * Currently this is being used to build the route id used for redirecting |
||
| 339 | * to newly made documents. It might benefit from having a different name |
||
| 340 | * for those purposes. |
||
| 341 | * |
||
| 342 | * We might use a convention based mapping here: |
||
| 343 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 344 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 345 | * |
||
| 346 | * @todo implement this in a more convention based manner |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public function getConnectionName() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Does the actual query using the RQL Bundle. |
||
| 359 | * |
||
| 360 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 361 | * @param Query $query query from parser |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 374 | * |
||
| 375 | * @param Object $record record |
||
| 376 | * |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | protected function checkIfOriginRecord($record) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 397 | * |
||
| 398 | * @return int |
||
| 399 | */ |
||
| 400 | private function getDefaultLimit() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param Boolean $active active |
||
| 411 | * @param String $field field |
||
| 412 | * @return void |
||
| 413 | */ |
||
| 414 | public function setFilterByAuthUser($active, $field) |
||
| 419 | } |
||
| 420 |
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.