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 | 2 | */ |
|
64 | protected $notModifiableOriginRecords; |
||
65 | 2 | /** |
|
66 | 2 | * @var integer |
|
67 | 2 | */ |
|
68 | 2 | private $paginationDefaultLimit; |
|
69 | 2 | ||
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 | 2 | * @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 | 2 | */ |
|
91 | public function __construct( |
||
103 | |||
104 | 1 | /** |
|
105 | 1 | * get repository instance |
|
106 | 1 | * |
|
107 | * @return DocumentRepository |
||
108 | */ |
||
109 | 1 | public function getRepository() |
|
110 | 1 | { |
|
111 | return $this->repository; |
||
112 | } |
||
113 | 1 | ||
114 | /** |
||
115 | * create new app model |
||
116 | * |
||
117 | * @param DocumentRepository $repository Repository of countries |
||
118 | * |
||
119 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
120 | */ |
||
121 | 1 | public function setRepository(DocumentRepository $repository) |
|
122 | { |
||
123 | $this->repository = $repository; |
||
124 | |||
125 | 1 | return $this; |
|
126 | 1 | } |
|
127 | 1 | ||
128 | /** |
||
129 | * {@inheritDoc} |
||
130 | * |
||
131 | 1 | * @param Request $request The request object |
|
132 | 1 | * @param SecurityUser $user SecurityUser Object |
|
133 | 1 | * @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) |
||
138 | { |
||
139 | $pageNumber = $request->query->get('page', 1); |
||
140 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
||
141 | $startAt = ($pageNumber - 1) * $numberPerPage; |
||
142 | 1 | ||
143 | 1 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
|
144 | 1 | $queryBuilder = $this->repository |
|
145 | ->createQueryBuilder(); |
||
146 | |||
147 | 1 | if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
|
148 | 1 | $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
|
149 | } |
||
150 | 1 | ||
151 | 1 | ||
152 | 1 | $searchableFields = $this->getSearchableFields(); |
|
153 | 1 | if (!is_null($schema)) { |
|
154 | $searchableFields = $schema->getSearchable(); |
||
155 | } |
||
156 | |||
157 | // *** do we have an RQL expression, do we need to filter data? |
||
158 | if ($request->attributes->get('hasRql', false)) { |
||
159 | $queryBuilder = $this->doRqlQuery( |
||
160 | $queryBuilder, |
||
161 | $this->translator->translateSearchQuery( |
||
162 | 1 | $request->attributes->get('rqlQuery'), |
|
163 | $searchableFields |
||
|
|||
164 | ) |
||
165 | ); |
||
166 | } else { |
||
167 | // @todo [lapistano]: seems the offset is missing for this query. |
||
168 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
169 | $queryBuilder->find($this->repository->getDocumentName()); |
||
170 | 1 | } |
|
171 | |||
172 | 1 | // define offset and limit |
|
173 | 1 | if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
|
174 | 1 | $queryBuilder->skip($startAt); |
|
175 | 1 | } else { |
|
176 | $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
||
177 | 1 | } |
|
178 | |||
179 | if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
||
180 | $queryBuilder->limit($numberPerPage); |
||
181 | } else { |
||
182 | $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
||
183 | } |
||
184 | |||
185 | 1 | // Limit can not be negative nor null. |
|
186 | if ($numberPerPage < 1) { |
||
187 | 1 | throw new RqlSyntaxErrorException('negative or null limit in rql'); |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * add a default sort on id if none was specified earlier |
||
192 | * |
||
193 | * not specifying something to sort on leads to very weird cases when fetching references |
||
194 | */ |
||
195 | if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
196 | $queryBuilder->sort('_id'); |
||
197 | } |
||
198 | 1 | ||
199 | // run query |
||
200 | 1 | $query = $queryBuilder->getQuery(); |
|
201 | $records = array_values($query->execute()->toArray()); |
||
202 | 1 | ||
203 | 1 | $totalCount = $query->count(); |
|
204 | 1 | $numPages = (int) ceil($totalCount / $numberPerPage); |
|
205 | 1 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
|
206 | if ($numPages > 1) { |
||
207 | 1 | $request->attributes->set('paging', true); |
|
208 | $request->attributes->set('page', $page); |
||
209 | $request->attributes->set('numPages', $numPages); |
||
210 | $request->attributes->set('startAt', $startAt); |
||
211 | $request->attributes->set('perPage', $numberPerPage); |
||
212 | $request->attributes->set('totalCount', $totalCount); |
||
213 | } |
||
214 | |||
215 | return $records; |
||
216 | } |
||
217 | 1 | ||
218 | /** |
||
219 | 1 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
|
220 | 1 | * |
|
221 | * @return Object |
||
222 | 1 | */ |
|
223 | 1 | View Code Duplication | public function insertRecord($entity) |
224 | 1 | { |
|
225 | 1 | $this->checkIfOriginRecord($entity); |
|
226 | 1 | $manager = $this->repository->getDocumentManager(); |
|
227 | 1 | $manager->persist($entity); |
|
228 | 1 | $manager->flush($entity); |
|
229 | |||
230 | 1 | return $this->find($entity->getId()); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param string $documentId id of entity to find |
||
235 | * |
||
236 | * @return Object |
||
237 | */ |
||
238 | 1 | 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) |
|
252 | { |
||
253 | $manager = $this->repository->getDocumentManager(); |
||
254 | // In both cases the document attribute named originRecord must not be 'core' |
||
255 | $this->checkIfOriginRecord($entity); |
||
256 | $this->checkIfOriginRecord($this->find($documentId)); |
||
257 | $entity = $manager->merge($entity); |
||
258 | $manager->flush(); |
||
259 | |||
260 | return $entity; |
||
261 | } |
||
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 | 7 | * get classname of entity |
|
288 | * |
||
289 | * @return string |
||
290 | 7 | */ |
|
291 | 7 | public function getEntityClass() |
|
292 | 3 | { |
|
293 | 3 | return $this->repository->getDocumentName(); |
|
294 | } |
||
295 | 3 | ||
296 | 1 | /** |
|
297 | * {@inheritDoc} |
||
298 | 1 | * |
|
299 | * Currently this is being used to build the route id used for redirecting |
||
300 | 2 | * to newly made documents. It might benefit from having a different name |
|
301 | 6 | * 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 | 1 | * |
|
309 | * @return string |
||
310 | 1 | */ |
|
311 | 1 | public function getConnectionName() |
|
312 | 1 | { |
|
313 | $bundle = strtolower(substr(explode('\\', get_class($this))[1], 0, -6)); |
||
314 | |||
315 | return 'graviton.' . $bundle; |
||
316 | } |
||
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) |
||
327 | { |
||
328 | $this->visitor->setBuilder($queryBuilder); |
||
329 | |||
330 | return $this->visitor->visit($query); |
||
331 | } |
||
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 | protected function checkIfOriginRecord($record) |
||
341 | { |
||
342 | if ($record instanceof RecordOriginInterface |
||
343 | && !$record->isRecordOriginModifiable() |
||
344 | ) { |
||
345 | $values = $this->notModifiableOriginRecords; |
||
346 | $originValue = strtolower(trim($record->getRecordOrigin())); |
||
347 | |||
348 | if (in_array($originValue, $values)) { |
||
349 | $msg = sprintf("Must not be one of the following keywords: %s", implode(', ', $values)); |
||
350 | |||
351 | throw new RecordOriginModifiedException($msg); |
||
352 | } |
||
353 | } |
||
354 | } |
||
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.