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 |
||
| 31 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $description; |
||
| 37 | /** |
||
| 38 | * @var string[] |
||
| 39 | */ |
||
| 40 | protected $fieldTitles; |
||
| 41 | /** |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | protected $fieldDescriptions; |
||
| 45 | /** |
||
| 46 | * @var string[] |
||
| 47 | */ |
||
| 48 | protected $requiredFields = array(); |
||
| 49 | /** |
||
| 50 | * @var string[] |
||
| 51 | */ |
||
| 52 | protected $searchableFields = array(); |
||
| 53 | /** |
||
| 54 | * @var DocumentRepository |
||
| 55 | */ |
||
| 56 | private $repository; |
||
| 57 | /** |
||
| 58 | * @var Visitor |
||
| 59 | */ |
||
| 60 | private $visitor; |
||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $notModifiableOriginRecords; |
||
| 65 | /** |
||
| 66 | * @var integer |
||
| 67 | */ |
||
| 68 | private $paginationDefaultLimit; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var boolean |
||
| 72 | */ |
||
| 73 | protected $filterByAuthUser; |
||
| 74 | |||
| 75 | 2 | /** |
|
| 76 | * @var string |
||
| 77 | 2 | */ |
|
| 78 | 2 | protected $filterByAuthField; |
|
| 79 | 2 | ||
| 80 | 2 | /** |
|
| 81 | 2 | * @var RqlTranslator |
|
| 82 | */ |
||
| 83 | protected $translator; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param Visitor $visitor rql query visitor |
||
| 87 | * @param RqlTranslator $translator Translator for query modification |
||
| 88 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 89 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 90 | */ |
||
| 91 | public function __construct( |
||
| 103 | |||
| 104 | 2 | /** |
|
| 105 | * get repository instance |
||
| 106 | * |
||
| 107 | * @return DocumentRepository |
||
| 108 | */ |
||
| 109 | public function getRepository() |
||
| 113 | |||
| 114 | /** |
||
| 115 | 1 | * create new app model |
|
| 116 | * |
||
| 117 | 1 | * @param DocumentRepository $repository Repository of countries |
|
| 118 | 1 | * |
|
| 119 | 1 | * @return \Graviton\RestBundle\Model\DocumentModel |
|
| 120 | */ |
||
| 121 | public function setRepository(DocumentRepository $repository) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritDoc} |
||
| 130 | 1 | * |
|
| 131 | * @param Request $request The request object |
||
| 132 | * @param SecurityUser $user SecurityUser Object |
||
| 133 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
||
| 220 | 1 | * |
|
| 221 | * @return Object |
||
| 222 | 1 | */ |
|
| 223 | View Code Duplication | public function insertRecord($entity) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $documentId id of entity to find |
||
| 235 | * |
||
| 236 | * @return Object |
||
| 237 | */ |
||
| 238 | public function find($documentId) |
||
| 242 | 1 | ||
| 243 | /** |
||
| 244 | 1 | * {@inheritDoc} |
|
| 245 | 1 | * |
|
| 246 | 1 | * @param string $documentId id of entity to update |
|
| 247 | 1 | * @param Object $entity new entity |
|
| 248 | 1 | * |
|
| 249 | 1 | * @return Object |
|
| 250 | */ |
||
| 251 | View Code Duplication | public function updateRecord($documentId, $entity) |
|
| 262 | 1 | ||
| 263 | /** |
||
| 264 | * {@inheritDoc} |
||
| 265 | * |
||
| 266 | * @param string $documentId id of entity to delete |
||
| 267 | * |
||
| 268 | * @return null|Object |
||
| 269 | */ |
||
| 270 | public function deleteRecord($documentId) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * get classname of entity |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getEntityClass() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritDoc} |
||
| 298 | * |
||
| 299 | * Currently this is being used to build the route id used for redirecting |
||
| 300 | * to newly made documents. It might benefit from having a different name |
||
| 301 | * for those purposes. |
||
| 302 | * |
||
| 303 | * We might use a convention based mapping here: |
||
| 304 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 305 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 306 | * |
||
| 307 | * @todo implement this in a more convention based manner |
||
| 308 | * |
||
| 309 | 7 | * @return string |
|
| 310 | */ |
||
| 311 | 7 | public function getConnectionName() |
|
| 317 | 3 | ||
| 318 | 1 | /** |
|
| 319 | * Does the actual query using the RQL Bundle. |
||
| 320 | 1 | * |
|
| 321 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 322 | * @param Query $query query from parser |
||
| 323 | 6 | * |
|
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 332 | 1 | ||
| 333 | 1 | /** |
|
| 334 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 335 | * |
||
| 336 | * @param Object $record record |
||
| 337 | * |
||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | protected function checkIfOriginRecord($record) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 358 | * |
||
| 359 | * @return int |
||
| 360 | */ |
||
| 361 | private function getDefaultLimit() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param Boolean $active active |
||
| 372 | * @param String $field field |
||
| 373 | * @return void |
||
| 374 | */ |
||
| 375 | public function setFilterByAuthUser($active, $field) |
||
| 380 | } |
||
| 381 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.