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 |
||
24 | class DocumentModel extends SchemaModel implements ModelInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $description; |
||
30 | /** |
||
31 | * @var string[] |
||
32 | */ |
||
33 | protected $fieldTitles; |
||
34 | /** |
||
35 | * @var string[] |
||
36 | */ |
||
37 | protected $fieldDescriptions; |
||
38 | /** |
||
39 | * @var string[] |
||
40 | */ |
||
41 | protected $requiredFields = array(); |
||
42 | /** |
||
43 | * @var DocumentRepository |
||
44 | */ |
||
45 | private $repository; |
||
46 | /** |
||
47 | * @var Visitor |
||
48 | */ |
||
49 | private $visitor; |
||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $notModifiableOriginRecords; |
||
54 | /** |
||
55 | * @var integer |
||
56 | */ |
||
57 | private $paginationDefaultLimit; |
||
58 | |||
59 | /** |
||
60 | * @var boolean |
||
61 | */ |
||
62 | protected $filterByAuthUser; |
||
63 | 2 | ||
64 | /** |
||
65 | 2 | * @var string |
|
66 | 2 | */ |
|
67 | 2 | protected $filterByAuthField; |
|
68 | 2 | ||
69 | 2 | /** |
|
70 | * @param Visitor $visitor rql query visitor |
||
71 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
72 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context. |
||
73 | */ |
||
74 | public function __construct(Visitor $visitor, $notModifiableOriginRecords, $paginationDefaultLimit) |
||
75 | { |
||
76 | parent::__construct(); |
||
77 | $this->visitor = $visitor; |
||
78 | $this->notModifiableOriginRecords = $notModifiableOriginRecords; |
||
79 | $this->paginationDefaultLimit = (int) $paginationDefaultLimit; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * get repository instance |
||
84 | * |
||
85 | * @return DocumentRepository |
||
86 | */ |
||
87 | public function getRepository() |
||
88 | 2 | { |
|
89 | return $this->repository; |
||
90 | 2 | } |
|
91 | |||
92 | 2 | /** |
|
93 | * create new app model |
||
94 | * |
||
95 | * @param DocumentRepository $repository Repository of countries |
||
96 | * |
||
97 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
98 | */ |
||
99 | public function setRepository(DocumentRepository $repository) |
||
105 | 1 | ||
106 | 1 | /** |
|
107 | * {@inheritDoc} |
||
108 | * |
||
109 | 1 | * @param Request $request The request object |
|
110 | 1 | * @param SecurityUser $user SecurityUser Object |
|
111 | * |
||
112 | * @return array |
||
113 | 1 | */ |
|
114 | public function findAll(Request $request, SecurityUser $user = null) |
||
115 | { |
||
116 | $pageNumber = $request->query->get('page', 1); |
||
117 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
||
118 | $startAt = ($pageNumber - 1) * $numberPerPage; |
||
119 | |||
120 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
||
121 | 1 | $queryBuilder = $this->repository |
|
122 | ->createQueryBuilder(); |
||
123 | |||
124 | if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
||
125 | 1 | $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
|
126 | 1 | } |
|
127 | 1 | ||
128 | // *** do we have an RQL expression, do we need to filter data? |
||
129 | if ($request->attributes->get('hasRql', false)) { |
||
130 | $queryBuilder = $this->doRqlQuery( |
||
131 | 1 | $queryBuilder, |
|
132 | 1 | $request->attributes->get('rqlQuery') |
|
133 | 1 | ); |
|
134 | } else { |
||
135 | // @todo [lapistano]: seems the offset is missing for this query. |
||
136 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
137 | $queryBuilder->find($this->repository->getDocumentName()); |
||
138 | } |
||
139 | |||
140 | // define offset and limit |
||
141 | if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
||
142 | 1 | $queryBuilder->skip($startAt); |
|
|
|||
143 | 1 | } else { |
|
144 | 1 | $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
|
145 | } |
||
146 | |||
147 | 1 | if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
|
148 | 1 | $queryBuilder->limit($numberPerPage); |
|
149 | } else { |
||
150 | 1 | $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
|
151 | 1 | } |
|
152 | 1 | ||
153 | 1 | /** |
|
154 | * add a default sort on id if none was specified earlier |
||
155 | * |
||
156 | * not specifying something to sort on leads to very weird cases when fetching references |
||
157 | */ |
||
158 | if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
159 | $queryBuilder->sort('_id'); |
||
160 | } |
||
161 | |||
162 | 1 | // run query |
|
163 | $query = $queryBuilder->getQuery(); |
||
164 | $records = array_values($query->execute()->toArray()); |
||
165 | |||
166 | $totalCount = $query->count(); |
||
167 | $numPages = (int) ceil($totalCount / $numberPerPage); |
||
168 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
||
169 | if ($numPages > 1) { |
||
170 | 1 | $request->attributes->set('paging', true); |
|
171 | $request->attributes->set('page', $page); |
||
172 | 1 | $request->attributes->set('numPages', $numPages); |
|
173 | 1 | $request->attributes->set('startAt', $startAt); |
|
174 | 1 | $request->attributes->set('perPage', $numberPerPage); |
|
175 | 1 | $request->attributes->set('totalCount', $totalCount); |
|
176 | } |
||
177 | 1 | ||
178 | return $records; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
||
183 | * |
||
184 | * @return Object |
||
185 | 1 | */ |
|
186 | View Code Duplication | public function insertRecord($entity) |
|
187 | 1 | { |
|
188 | $this->checkIfOriginRecord($entity); |
||
189 | $manager = $this->repository->getDocumentManager(); |
||
190 | $manager->persist($entity); |
||
191 | $manager->flush($entity); |
||
192 | |||
193 | return $this->find($entity->getId()); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param string $documentId id of entity to find |
||
198 | 1 | * |
|
199 | * @return Object |
||
200 | 1 | */ |
|
201 | public function find($documentId) |
||
202 | 1 | { |
|
203 | 1 | return $this->repository->find($documentId); |
|
204 | 1 | } |
|
205 | 1 | ||
206 | /** |
||
207 | 1 | * {@inheritDoc} |
|
208 | * |
||
209 | * @param string $documentId id of entity to update |
||
210 | * @param Object $entity new entity |
||
211 | * |
||
212 | * @return Object |
||
213 | */ |
||
214 | View Code Duplication | public function updateRecord($documentId, $entity) |
|
225 | 1 | ||
226 | 1 | /** |
|
227 | 1 | * {@inheritDoc} |
|
228 | 1 | * |
|
229 | * @param string $documentId id of entity to delete |
||
230 | 1 | * |
|
231 | * @return null|Object |
||
232 | */ |
||
233 | public function deleteRecord($documentId) |
||
234 | { |
||
235 | $manager = $this->repository->getDocumentManager(); |
||
236 | $entity = $this->find($documentId); |
||
237 | |||
238 | 1 | $return = $entity; |
|
239 | if ($entity) { |
||
240 | 1 | $this->checkIfOriginRecord($entity); |
|
241 | $manager->remove($entity); |
||
242 | $manager->flush(); |
||
243 | $return = null; |
||
244 | } |
||
245 | |||
246 | return $return; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * get classname of entity |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getEntityClass() |
||
255 | { |
||
256 | return $this->repository->getDocumentName(); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * {@inheritDoc} |
||
261 | * |
||
262 | * Currently this is being used to build the route id used for redirecting |
||
263 | * to newly made documents. It might benefit from having a different name |
||
264 | * for those purposes. |
||
265 | * |
||
266 | * We might use a convention based mapping here: |
||
267 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
268 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
269 | * |
||
270 | * @todo implement this in a more convention based manner |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | public function getConnectionName() |
||
280 | |||
281 | /** |
||
282 | * Does the actual query using the RQL Bundle. |
||
283 | * |
||
284 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
285 | * @param Query $query query from parser |
||
286 | * |
||
287 | 7 | * @return array |
|
288 | */ |
||
289 | protected function doRqlQuery($queryBuilder, Query $query) |
||
295 | 3 | ||
296 | 1 | /** |
|
297 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
298 | 1 | * |
|
299 | * @param Object $record record |
||
300 | 2 | * |
|
301 | 6 | * @return void |
|
302 | */ |
||
303 | protected function checkIfOriginRecord($record) |
||
318 | |||
319 | /** |
||
320 | * Determines the configured amount fo data records to be returned in pagination context. |
||
321 | * |
||
322 | * @return int |
||
323 | */ |
||
324 | private function getDefaultLimit() |
||
332 | |||
333 | /** |
||
334 | * @param Boolean $active active |
||
335 | * @param String $field field |
||
336 | * @return void |
||
337 | */ |
||
338 | public function setFilterByAuthUser($active, $field) |
||
343 | } |
||
344 |
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: