Total Complexity | 51 |
Total Lines | 472 |
Duplicated Lines | 22.88 % |
Coverage | 32.02% |
Changes | 0 |
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:
Complex classes like BaseRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class BaseRepository extends EntityRepository |
||
15 | { |
||
16 | protected $fields; |
||
17 | |||
18 | protected $request; |
||
19 | |||
20 | protected $use_result_cache = false; |
||
21 | |||
22 | protected $entityAlias; |
||
23 | |||
24 | protected $route_name; |
||
25 | |||
26 | protected $currentEntityAlias; |
||
27 | |||
28 | protected $embeddedFields; |
||
29 | |||
30 | protected $joins = []; |
||
31 | |||
32 | protected $queryBuilderFactory; |
||
33 | |||
34 | protected $queryOptions; |
||
35 | |||
36 | 5 | public function __construct($manager, $class) |
|
37 | { |
||
38 | 5 | parent::__construct($manager, $class); |
|
39 | |||
40 | 5 | $this->fields = array_keys($this->getClassMetadata()->fieldMappings); |
|
41 | |||
42 | 5 | $entityName = explode('\\', strtolower($this->getEntityName()) ); |
|
43 | 5 | $entityName = $entityName[count($entityName)-1]; |
|
44 | 5 | $entityAlias = $entityName[0]; |
|
45 | 5 | $this->entityAlias = $entityAlias; |
|
46 | |||
47 | 5 | $this->queryBuilderFactory = new QueryBuilderFactory($this->getEntityManager()); |
|
48 | 5 | } |
|
49 | |||
50 | public function initFromQueryBuilderOptions(QueryBuilderOptions $options) |
||
51 | { |
||
52 | $this->queryBuilderFactory->createQueryBuilder($this->getEntityName(), $this->entityAlias); |
||
53 | |||
54 | $fieldMappings = $this->getClassMetadata()->fieldMappings; |
||
55 | $this->fields = array_keys($fieldMappings); |
||
56 | |||
57 | $this->queryBuilderFactory->setFields($this->fields ?? []); |
||
58 | $this->queryBuilderFactory->setFilters($options->getFilters()); |
||
59 | $this->queryBuilderFactory->setOrFilters($options->getOrFilters()); |
||
60 | $this->queryBuilderFactory->setSorting($options->getSorting()); |
||
61 | $this->queryBuilderFactory->setRel($options->getRel()); |
||
62 | $this->queryBuilderFactory->setPrinting($options->getPrinting()); |
||
63 | $this->queryBuilderFactory->setSelect($options->getSelect()); |
||
64 | } |
||
65 | |||
66 | public function getQueryBuilderFactory() |
||
67 | { |
||
68 | $this->initFromQueryBuilderOptions($this->queryOptions); |
||
69 | |||
70 | return $this->queryBuilderFactory; |
||
71 | } |
||
72 | |||
73 | public function useResultCache($bool) |
||
74 | { |
||
75 | $this->use_result_cache = $bool; |
||
76 | } |
||
77 | |||
78 | 1 | public function setRequest(Request $request) |
|
79 | { |
||
80 | 1 | return $this->setQueryOptionsFromRequest($request); |
|
81 | } |
||
82 | |||
83 | public function setRequestWithFilter(Request $request, $filter) |
||
84 | { |
||
85 | return $this->setQueryOptionsFromRequestWithCustomFilter($request, $filter); |
||
86 | } |
||
87 | |||
88 | public function setRequestWithOrFilter(Request $request, $orFilter) |
||
89 | { |
||
90 | return $this->setQueryOptionsFromRequestWithCustomOrFilter($request, $orFilter); |
||
91 | } |
||
92 | |||
93 | public function setQueryOptions(QueryBuilderOptions $options) |
||
94 | { |
||
95 | $this->queryOptions = $options; |
||
96 | } |
||
97 | |||
98 | 3 | public function setQueryOptionsFromRequest(Request $request = null) |
|
99 | { |
||
100 | 3 | $requestAttributes = self::getRequestAttributes($request); |
|
101 | |||
102 | 3 | $filters = $request->query->get('filtering', []); |
|
103 | 3 | $orFilters = $request->query->get('filtering_or', []); |
|
104 | 3 | $sorting = $request->query->get('sorting', []); |
|
105 | 3 | $printing = $request->query->get('printing', []); |
|
106 | 3 | $rel = $request->query->get('rel', ''); |
|
107 | 3 | $page = $request->query->get('page', ''); |
|
108 | 3 | $select = $request->query->get('select', $this->entityAlias); |
|
109 | 3 | $filtering = $request->query->get('filtering', ''); |
|
110 | 3 | $limit = $request->query->get('limit', ''); |
|
111 | |||
112 | 3 | $filterOrCorrected = []; |
|
113 | |||
114 | 3 | $count = 0; |
|
115 | 3 | foreach ($orFilters as $key => $filter) { |
|
116 | if (is_array($filter)) { |
||
117 | foreach ($filter as $keyInternal => $internal) { |
||
118 | $filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
||
119 | $count = $count + 1; |
||
120 | } |
||
121 | } else { |
||
122 | $filterOrCorrected[$key] = $filter; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | $requestProperties = [ |
||
127 | 3 | 'filtering' => $filtering, |
|
128 | 3 | 'orFiltering' => $filterOrCorrected, |
|
129 | 3 | 'limit' => $limit, |
|
130 | 3 | 'page' => $page, |
|
131 | 3 | 'filters' => $filters, |
|
132 | 3 | 'orFilters' => $filterOrCorrected, |
|
133 | 3 | 'sorting' => $sorting, |
|
134 | 3 | 'rel' => $rel, |
|
135 | 3 | 'printing' => $printing, |
|
136 | 3 | 'select' => $select, |
|
137 | ]; |
||
138 | |||
139 | 3 | $options = array_merge( |
|
140 | 3 | $requestAttributes, |
|
141 | 3 | $requestProperties |
|
142 | ); |
||
143 | |||
144 | 3 | $this->queryOptions = QueryBuilderOptions::fromArray($options); |
|
145 | |||
146 | 3 | return $this; |
|
147 | } |
||
148 | |||
149 | 4 | public static function getRequestAttributes(Request $request) |
|
150 | { |
||
151 | 4 | $requestAttributes = []; |
|
152 | |||
153 | 4 | foreach ($request->attributes->all() as $attributeName => $attributeValue) { |
|
154 | $requestAttributes[$attributeName] = $request->attributes->get( |
||
155 | $attributeName, |
||
156 | $attributeValue |
||
157 | ); |
||
158 | } |
||
159 | |||
160 | 4 | return $requestAttributes; |
|
161 | } |
||
162 | |||
163 | View Code Duplication | public function setQueryOptionsFromRequestWithCustomFilter(Request $request = null, $filter) |
|
164 | { |
||
165 | $requestAttributes = self::getRequestAttributes($request); |
||
166 | |||
167 | $filters = $request->query->get('filtering', []); |
||
168 | $orFilters = $request->query->get('filtering_or', []); |
||
169 | $sorting = $request->query->get('sorting', []); |
||
170 | $printing = $request->query->get('printing', []); |
||
171 | $rel = $request->query->get('rel', ''); |
||
172 | $page = $request->query->get('page', ''); |
||
173 | $select = $request->query->get('select', $this->entityAlias); |
||
174 | $filtering = $request->query->get('filtering', ''); |
||
175 | $limit = $request->query->get('limit', ''); |
||
176 | |||
177 | $filters = array_merge($filters, $filter); |
||
178 | |||
179 | $filterOrCorrected = []; |
||
180 | |||
181 | $count = 0; |
||
182 | foreach ($orFilters as $key => $filter) { |
||
183 | if (is_array($filter)) { |
||
184 | foreach ($filter as $keyInternal => $internal) { |
||
185 | $filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
||
186 | $count = $count + 1; |
||
187 | } |
||
188 | } else { |
||
189 | $filterOrCorrected[$key] = $filter; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | $requestProperties = [ |
||
194 | '_route' => $request->attributes->get('_route'), |
||
195 | 'customer_id' => $request->attributes->get('customer_id'), |
||
196 | 'id' => $request->attributes->get('id'), |
||
197 | 'filtering' => $filtering, |
||
198 | 'limit' => $limit, |
||
199 | 'page' => $page, |
||
200 | 'filters' => $filters, |
||
201 | 'orFilters' => $filterOrCorrected, |
||
202 | 'sorting' => $sorting, |
||
203 | 'rel' => $rel, |
||
204 | 'printing' => $printing, |
||
205 | 'select' => $select, |
||
206 | ]; |
||
207 | |||
208 | $options = array_merge( |
||
209 | $requestAttributes, |
||
210 | $requestProperties |
||
211 | ); |
||
212 | |||
213 | $this->queryOptions = QueryBuilderOptions::fromArray($options); |
||
214 | |||
215 | return $this; |
||
216 | } |
||
217 | |||
218 | 1 | View Code Duplication | public function setQueryOptionsFromRequestWithCustomOrFilter(Request $request = null, $orFilter) |
219 | { |
||
220 | 1 | $requestAttributes = self::getRequestAttributes($request); |
|
221 | |||
222 | 1 | $filters = $request->query->get('filtering', []); |
|
223 | 1 | $orFilters = $request->query->get('filtering_or', []); |
|
224 | 1 | $sorting = $request->query->get('sorting', []); |
|
225 | 1 | $printing = $request->query->get('printing', []); |
|
226 | 1 | $rel = $request->query->get('rel', ''); |
|
227 | 1 | $page = $request->query->get('page', ''); |
|
228 | 1 | $select = $request->query->get('select', $this->entityAlias); |
|
229 | 1 | $filtering = $request->query->get('filtering', ''); |
|
230 | 1 | $limit = $request->query->get('limit', ''); |
|
231 | |||
232 | 1 | $orFilters = array_merge($orFilters, $orFilter); |
|
233 | |||
234 | 1 | $filterOrCorrected = []; |
|
235 | |||
236 | 1 | $count = 0; |
|
237 | 1 | foreach ($orFilters as $key => $filter) { |
|
238 | if (is_array($filter)) { |
||
239 | foreach ($filter as $keyInternal => $internal) { |
||
240 | $filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
||
241 | $count = $count + 1; |
||
242 | } |
||
243 | } else { |
||
244 | $filterOrCorrected[$key] = $filter; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | $requestProperties = [ |
||
249 | 1 | '_route' => $request->attributes->get('_route'), |
|
250 | 1 | 'customer_id' => $request->attributes->get('customer_id'), |
|
251 | 1 | 'id' => $request->attributes->get('id'), |
|
252 | 1 | 'filtering' => $filtering, |
|
253 | 1 | 'limit' => $limit, |
|
254 | 1 | 'page' => $page, |
|
255 | 1 | 'filters' => $filters, |
|
256 | 1 | 'orFilters' => $filterOrCorrected, |
|
257 | 1 | 'sorting' => $sorting, |
|
258 | 1 | 'rel' => $rel, |
|
259 | 1 | 'printing' => $printing, |
|
260 | 1 | 'select' => $select, |
|
261 | ]; |
||
262 | |||
263 | 1 | $options = array_merge( |
|
264 | 1 | $requestAttributes, |
|
265 | 1 | $requestProperties |
|
266 | ); |
||
267 | |||
268 | 1 | $this->queryOptions = QueryBuilderOptions::fromArray($options); |
|
269 | |||
270 | 1 | return $this; |
|
271 | } |
||
272 | |||
273 | 4 | public function getQueryBuilderOptions() |
|
274 | { |
||
275 | 4 | return $this->queryOptions; |
|
276 | } |
||
277 | |||
278 | public function getRequest() |
||
279 | { |
||
280 | return $this->request; |
||
281 | } |
||
282 | |||
283 | public function setRouteName($route_name = '') |
||
284 | { |
||
285 | $this->route_name = $route_name; |
||
286 | return $this; |
||
287 | } |
||
288 | |||
289 | public function findAllPaginated() |
||
290 | { |
||
291 | $this->initFromQueryBuilderOptions($this->queryOptions); |
||
292 | |||
293 | $this->queryBuilderFactory->filter(); |
||
294 | $this->queryBuilderFactory->sort(); |
||
295 | |||
296 | return $this->paginateResults($this->queryBuilderFactory->getQueryBuilder()); |
||
297 | } |
||
298 | |||
299 | protected function paginateResults( |
||
300 | \Doctrine\ORM\QueryBuilder $queryBuilder |
||
301 | ) { |
||
302 | $limit = $this->queryOptions->get('limit', 10); |
||
303 | $page = $this->queryOptions->get('page', 1); |
||
304 | |||
305 | |||
306 | $pagerAdapter = new DoctrineORMAdapter($queryBuilder); |
||
307 | |||
308 | $query = $pagerAdapter->getQuery(); |
||
309 | if(isset($this->use_result_cache) and $this->use_result_cache){ |
||
310 | $query->useResultCache(true, 600); |
||
311 | } |
||
312 | |||
313 | $pager = new Pagerfanta($pagerAdapter); |
||
314 | $pager->setNormalizeOutOfRangePages(true); |
||
315 | $pager->setMaxPerPage($limit); |
||
316 | $pager->setCurrentPage($page); |
||
317 | |||
318 | $pagerFactory = new PagerfantaFactory(); |
||
319 | |||
320 | $router = $this->createRouter(); |
||
321 | |||
322 | $results = $pagerFactory->createRepresentation($pager, $router); |
||
323 | |||
324 | return $results; |
||
325 | } |
||
326 | |||
327 | protected function customQueryStringValues() |
||
328 | { |
||
329 | return []; |
||
330 | } |
||
331 | |||
332 | protected function createRouter() |
||
333 | { |
||
334 | $params = []; |
||
335 | |||
336 | $list = array_merge([ |
||
337 | 'filtering', |
||
338 | 'limit', |
||
339 | 'page', |
||
340 | 'sorting', |
||
341 | ], $this->customQueryStringValues()); |
||
342 | |||
343 | foreach ($list as $itemKey => $itemValue) { |
||
344 | $params[$itemValue] = $this->queryOptions->get($itemValue); |
||
345 | } |
||
346 | |||
347 | if(!isset($this->route_name)){ |
||
348 | $this->route_name = $this->queryOptions->get('_route'); |
||
349 | } |
||
350 | |||
351 | return new Route($this->route_name, $params); |
||
352 | } |
||
353 | |||
354 | /** @deprecate use QueryBuilderFactory instead */ |
||
355 | public function noExistsJoin($prevEntityAlias, $currentEntityAlias) |
||
356 | { |
||
357 | $needle = $prevEntityAlias . "_" . $currentEntityAlias; |
||
358 | return ! in_array($needle, $this->joins); |
||
359 | } |
||
360 | |||
361 | /** @deprecate use QueryBuilderFactory instead */ |
||
362 | public function storeJoin($prevEntityAlias, $currentEntityAlias) |
||
363 | { |
||
364 | $needle = $prevEntityAlias . "_" . $currentEntityAlias; |
||
365 | $this->joins[$needle] = $needle; |
||
366 | } |
||
367 | |||
368 | /** @deprecate use QueryBuilderFactory instead */ |
||
369 | public function join($queryBuilder, $key, $val) |
||
370 | { |
||
371 | if (strstr($key, '_embedded.')) { |
||
372 | $embeddedFields = explode('.', $key); |
||
373 | $numFields = count($embeddedFields); |
||
374 | |||
375 | $prevEntityAlias = $this->entityAlias; |
||
376 | $prevEntityName = $this->getEntityName(); |
||
377 | |||
378 | for ($i = 1; $i < $numFields - 1; $i++) { |
||
379 | $metadata = $this->getEntityManager()->getClassMetadata($prevEntityName); |
||
380 | |||
381 | $currentRelation = $embeddedFields[$i]; |
||
382 | |||
383 | if ($metadata->hasAssociation($currentRelation)) { |
||
384 | |||
385 | $association = $metadata->getAssociationMapping($currentRelation); |
||
386 | |||
387 | $currentEntityAlias = $this->getEntityAlias($association['targetEntity']); |
||
388 | |||
389 | if ($this->noExistsJoin($prevEntityAlias, $currentRelation)) { |
||
390 | if ($association['isOwningSide']) { |
||
391 | $queryBuilder->join($association['targetEntity'], "$currentEntityAlias", "WITH", "$currentEntityAlias.id = " . "$prevEntityAlias.$currentRelation"); |
||
392 | } else { |
||
393 | $mappedBy = $association['mappedBy']; |
||
394 | $queryBuilder->join($association['targetEntity'], "$currentEntityAlias", "WITH", "$currentEntityAlias.$mappedBy = " . "$prevEntityAlias.id"); |
||
395 | } |
||
396 | |||
397 | $this->storeJoin($prevEntityAlias, $currentRelation); |
||
398 | } |
||
399 | } |
||
400 | |||
401 | $prevEntityAlias = $currentEntityAlias; |
||
402 | $prevEntityName = $association['targetEntity']; |
||
403 | } |
||
404 | |||
405 | $this->setEmbeddedFields($embeddedFields); |
||
406 | $this->setCurrentEntityAlias($currentEntityAlias); |
||
407 | } |
||
408 | |||
409 | return $queryBuilder; |
||
410 | } |
||
411 | |||
412 | protected function getCurrentEntityAlias() : string |
||
413 | { |
||
414 | return $this->currentEntityAlias; |
||
415 | } |
||
416 | |||
417 | protected function setCurrentEntityAlias(string $currentEntityAlias) |
||
418 | { |
||
419 | $this->currentEntityAlias = $currentEntityAlias; |
||
420 | } |
||
421 | |||
422 | protected function getEmbeddedFields() : array |
||
423 | { |
||
424 | return $this->embeddedFields; |
||
425 | } |
||
426 | |||
427 | protected function setEmbeddedFields(array $embeddedFields) |
||
428 | { |
||
429 | $this->embeddedFields = $embeddedFields; |
||
430 | } |
||
431 | |||
432 | 1 | public function getEntityAlias(string $entityName) : string |
|
433 | { |
||
434 | 1 | $arrayEntityName = explode('\\', strtolower($entityName) ); |
|
435 | 1 | $entityAlias = $arrayEntityName[count($arrayEntityName)-1]; |
|
436 | 1 | return $entityAlias; |
|
437 | } |
||
438 | |||
439 | protected function relationship($queryBuilder) |
||
440 | { |
||
441 | return $queryBuilder; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * |
||
446 | * @param type $insertFields |
||
447 | * @param type $updateFields |
||
448 | * |
||
449 | * USE: |
||
450 | * |
||
451 | * $this->getEntityManager() |
||
452 | * ->getRepository('User') |
||
453 | * ->onDuplicateUpdate(['column1' => 'user_reminder_1', 'column2' => 235], ['column2' => 255]); |
||
454 | */ |
||
455 | public function onDuplicateUpdate($insertFields, $updateFields) |
||
481 | } |
||
482 | |||
483 | public function getQueryBuilderFactoryWithoutInitialization() |
||
484 | { |
||
485 | return $this->queryBuilderFactory; |
||
486 | } |
||
487 | } |
||
488 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths