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 |
||
| 25 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $description; |
||
| 31 | /** |
||
| 32 | * @var string[] |
||
| 33 | */ |
||
| 34 | protected $fieldTitles; |
||
| 35 | /** |
||
| 36 | * @var string[] |
||
| 37 | */ |
||
| 38 | protected $fieldDescriptions; |
||
| 39 | /** |
||
| 40 | * @var string[] |
||
| 41 | */ |
||
| 42 | protected $requiredFields = array(); |
||
| 43 | /** |
||
| 44 | * @var DocumentRepository |
||
| 45 | */ |
||
| 46 | private $repository; |
||
| 47 | /** |
||
| 48 | * @var Visitor |
||
| 49 | */ |
||
| 50 | private $visitor; |
||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $notModifiableOriginRecords; |
||
| 55 | /** |
||
| 56 | * @var integer |
||
| 57 | */ |
||
| 58 | private $paginationDefaultLimit; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var boolean |
||
| 62 | */ |
||
| 63 | protected $filterByAuthUser; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $filterByAuthField; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param Visitor $visitor rql query visitor |
||
| 72 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 73 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context. |
||
| 74 | */ |
||
| 75 | 2 | public function __construct(Visitor $visitor, $notModifiableOriginRecords, $paginationDefaultLimit) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * get repository instance |
||
| 85 | * |
||
| 86 | * @return DocumentRepository |
||
| 87 | */ |
||
| 88 | public function getRepository() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * create new app model |
||
| 95 | * |
||
| 96 | * @param DocumentRepository $repository Repository of countries |
||
| 97 | * |
||
| 98 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 99 | */ |
||
| 100 | 2 | public function setRepository(DocumentRepository $repository) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritDoc} |
||
| 109 | * |
||
| 110 | * @param Request $request The request object |
||
| 111 | * @param SecurityUser $user SecurityUser Object |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | 1 | public function findAll(Request $request, SecurityUser $user = null) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
||
| 189 | * |
||
| 190 | * @return Object |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function insertRecord($entity) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $documentId id of entity to find |
||
| 204 | * |
||
| 205 | * @return Object |
||
| 206 | */ |
||
| 207 | 1 | public function find($documentId) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritDoc} |
||
| 214 | * |
||
| 215 | * @param string $documentId id of entity to update |
||
| 216 | * @param Object $entity new entity |
||
| 217 | * |
||
| 218 | * @return Object |
||
| 219 | */ |
||
| 220 | 1 | View Code Duplication | public function updateRecord($documentId, $entity) |
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritDoc} |
||
| 234 | * |
||
| 235 | * @param string $documentId id of entity to delete |
||
| 236 | * |
||
| 237 | * @return null|Object |
||
| 238 | */ |
||
| 239 | 1 | public function deleteRecord($documentId) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * get classname of entity |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 1 | public function getEntityClass() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritDoc} |
||
| 267 | * |
||
| 268 | * Currently this is being used to build the route id used for redirecting |
||
| 269 | * to newly made documents. It might benefit from having a different name |
||
| 270 | * for those purposes. |
||
| 271 | * |
||
| 272 | * We might use a convention based mapping here: |
||
| 273 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 274 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 275 | * |
||
| 276 | * @todo implement this in a more convention based manner |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getConnectionName() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Does the actual query using the RQL Bundle. |
||
| 289 | * |
||
| 290 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 291 | * @param Query $query query from parser |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 304 | * |
||
| 305 | * @param Object $record record |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | 8 | protected function checkIfOriginRecord($record) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 327 | * |
||
| 328 | * @return int |
||
| 329 | */ |
||
| 330 | 1 | private function getDefaultLimit() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @param Boolean $active active |
||
| 341 | * @param String $field field |
||
| 342 | * @return void |
||
| 343 | */ |
||
| 344 | 2 | public function setFilterByAuthUser($active, $field) |
|
| 349 | } |
||
| 350 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: