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 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $filterByAuthField; |
||
79 | |||
80 | /** |
||
81 | * @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 | /** |
||
105 | * get repository instance |
||
106 | * |
||
107 | * @return DocumentRepository |
||
108 | */ |
||
109 | public function getRepository() |
||
110 | { |
||
111 | return $this->repository; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * create new app model |
||
116 | * |
||
117 | * @param DocumentRepository $repository Repository of countries |
||
118 | * |
||
119 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
120 | */ |
||
121 | public function setRepository(DocumentRepository $repository) |
||
122 | { |
||
123 | $this->repository = $repository; |
||
124 | |||
125 | return $this; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * {@inheritDoc} |
||
130 | * |
||
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 | * |
||
221 | * @return Object |
||
222 | */ |
||
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 | |||
243 | /** |
||
244 | * {@inheritDoc} |
||
245 | * |
||
246 | * @param string $documentId id of entity to update |
||
247 | * @param Object $entity new entity |
||
248 | * |
||
249 | * @return Object |
||
250 | */ |
||
251 | View Code Duplication | public function updateRecord($documentId, $entity) |
|
262 | |||
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 | * @return string |
||
310 | */ |
||
311 | public function getConnectionName() |
||
317 | |||
318 | /** |
||
319 | * Does the actual query using the RQL Bundle. |
||
320 | * |
||
321 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
322 | * @param Query $query query from parser |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | protected function doRqlQuery($queryBuilder, Query $query) |
||
332 | |||
333 | /** |
||
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 | 12 | 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 |
This check looks for
@param
annotations 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.