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 |
||
23 | class DocumentModel extends SchemaModel implements ModelInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $description; |
||
29 | /** |
||
30 | * @var string[] |
||
31 | */ |
||
32 | protected $fieldTitles; |
||
33 | /** |
||
34 | * @var string[] |
||
35 | */ |
||
36 | protected $fieldDescriptions; |
||
37 | /** |
||
38 | * @var string[] |
||
39 | */ |
||
40 | protected $requiredFields = array(); |
||
41 | /** |
||
42 | * @var DocumentRepository |
||
43 | */ |
||
44 | private $repository; |
||
45 | /** |
||
46 | * @var Visitor |
||
47 | */ |
||
48 | private $visitor; |
||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $notModifiableOriginRecords; |
||
53 | /** |
||
54 | * @var integer |
||
55 | */ |
||
56 | private $paginationDefaultLimit; |
||
57 | |||
58 | /** |
||
59 | * @var boolean |
||
60 | */ |
||
61 | protected $filterByAuthUser; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $filterByAuthField; |
||
67 | |||
68 | /** |
||
69 | * @param Visitor $visitor rql query visitor |
||
70 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
71 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context. |
||
72 | */ |
||
73 | 2 | public function __construct(Visitor $visitor, $notModifiableOriginRecords, $paginationDefaultLimit) |
|
80 | |||
81 | /** |
||
82 | * get repository instance |
||
83 | * |
||
84 | * @return DocumentRepository |
||
85 | */ |
||
86 | public function getRepository() |
||
90 | |||
91 | /** |
||
92 | * create new app model |
||
93 | * |
||
94 | * @param DocumentRepository $repository Repository of countries |
||
95 | * |
||
96 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
97 | */ |
||
98 | 2 | public function setRepository(DocumentRepository $repository) |
|
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | * |
||
108 | * @param Request $request The request object |
||
109 | * @param Object $user The user |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | 1 | public function findAll(Request $request, $user = false) |
|
179 | |||
180 | /** |
||
181 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
||
182 | * |
||
183 | * @return Object |
||
184 | */ |
||
185 | View Code Duplication | public function insertRecord($entity) |
|
194 | |||
195 | /** |
||
196 | * @param string $documentId id of entity to find |
||
197 | * |
||
198 | * @return Object |
||
199 | */ |
||
200 | 1 | public function find($documentId) |
|
204 | |||
205 | /** |
||
206 | * {@inheritDoc} |
||
207 | * |
||
208 | * @param string $documentId id of entity to update |
||
209 | * @param Object $entity new entity |
||
210 | * |
||
211 | * @return Object |
||
212 | */ |
||
213 | 1 | View Code Duplication | public function updateRecord($documentId, $entity) |
224 | |||
225 | /** |
||
226 | * {@inheritDoc} |
||
227 | * |
||
228 | * @param string $documentId id of entity to delete |
||
229 | * |
||
230 | * @return null|Object |
||
231 | */ |
||
232 | 1 | public function deleteRecord($documentId) |
|
247 | |||
248 | /** |
||
249 | * get classname of entity |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | 1 | public function getEntityClass() |
|
257 | |||
258 | /** |
||
259 | * {@inheritDoc} |
||
260 | * |
||
261 | * Currently this is being used to build the route id used for redirecting |
||
262 | * to newly made documents. It might benefit from having a different name |
||
263 | * for those purposes. |
||
264 | * |
||
265 | * We might use a convention based mapping here: |
||
266 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
267 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
268 | * |
||
269 | * @todo implement this in a more convention based manner |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | public function getConnectionName() |
||
279 | |||
280 | /** |
||
281 | * Does the actual query using the RQL Bundle. |
||
282 | * |
||
283 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
284 | * @param Query $query query from parser |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | protected function doRqlQuery($queryBuilder, Query $query) |
||
294 | |||
295 | /** |
||
296 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
297 | * |
||
298 | * @param Object $record record |
||
299 | * |
||
300 | * @return void |
||
301 | */ |
||
302 | 7 | protected function checkIfOriginRecord($record) |
|
317 | |||
318 | /** |
||
319 | * Determines the configured amount fo data records to be returned in pagination context. |
||
320 | * |
||
321 | * @return int |
||
322 | */ |
||
323 | 1 | private function getDefaultLimit() |
|
331 | |||
332 | /** |
||
333 | * @param Boolean $active active |
||
334 | * @param String $field field |
||
335 | 2 | * @return void |
|
336 | */ |
||
337 | 2 | public function setFilterByAuthUser($active, $field) |
|
342 | } |
||
343 |
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: