Total Complexity | 43 |
Total Lines | 383 |
Duplicated Lines | 24.02 % |
Coverage | 36.28% |
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 |
||
16 | class BaseRepository extends EntityRepository |
||
17 | { |
||
18 | protected $fields; |
||
19 | |||
20 | protected $request; |
||
21 | |||
22 | protected $use_result_cache = false; |
||
23 | |||
24 | protected $entityAlias; |
||
25 | |||
26 | protected $route_name; |
||
27 | |||
28 | protected $currentEntityAlias; |
||
29 | |||
30 | protected $embeddedFields; |
||
31 | |||
32 | protected $joins = []; |
||
33 | |||
34 | protected $queryBuilderFactory; |
||
35 | |||
36 | 5 | protected $queryOptions; |
|
37 | |||
38 | 5 | protected $configProvider; |
|
39 | |||
40 | 5 | public function __construct($manager, $class) |
|
41 | { |
||
42 | 5 | parent::__construct($manager, $class); |
|
43 | 5 | ||
44 | 5 | $this->fields = array_keys($this->getClassMetadata()->fieldMappings); |
|
45 | 5 | ||
46 | $entityName = explode('\\', strtolower($this->getEntityName()) ); |
||
47 | 5 | $entityName = $entityName[count($entityName)-1]; |
|
48 | 5 | //$entityAlias = $entityName[0]; |
|
49 | $this->entityAlias = $entityName; |
||
50 | |||
51 | $this->queryBuilderFactory = new QueryBuilderFactory($this->getEntityManager()); |
||
52 | } |
||
53 | |||
54 | public function initFromQueryBuilderOptions(QueryBuilderOptions $options) |
||
55 | { |
||
56 | $this->queryBuilderFactory->createQueryBuilder($this->getEntityName(), $this->entityAlias); |
||
57 | |||
58 | $fieldMappings = $this->getClassMetadata()->fieldMappings; |
||
59 | $this->fields = array_keys($fieldMappings); |
||
60 | |||
61 | $this->queryBuilderFactory->setFields($this->fields ?? []); |
||
62 | $this->queryBuilderFactory->setFilters($options->getFilters()); |
||
63 | $this->queryBuilderFactory->setOrFilters($options->getOrFilters()); |
||
64 | $this->queryBuilderFactory->setSorting($options->getSorting()); |
||
65 | $this->queryBuilderFactory->setRel($options->getRel()); |
||
66 | $this->queryBuilderFactory->setPrinting($options->getPrinting()); |
||
67 | $this->queryBuilderFactory->setSelect($options->getSelect()); |
||
68 | } |
||
69 | |||
70 | public function getQueryBuilderFactory() |
||
71 | { |
||
72 | $this->ensureQueryOptionIsDefined(); |
||
73 | |||
74 | $this->initFromQueryBuilderOptions($this->queryOptions); |
||
75 | |||
76 | return $this->queryBuilderFactory; |
||
77 | } |
||
78 | 1 | ||
79 | public function useResultCache($bool) |
||
80 | 1 | { |
|
81 | $this->use_result_cache = $bool; |
||
82 | } |
||
83 | |||
84 | public function setRequest(Request $request) |
||
85 | { |
||
86 | return $this->setQueryOptionsFromRequest($request); |
||
87 | } |
||
88 | |||
89 | public function setRequestWithFilter(Request $request, $filter) |
||
90 | { |
||
91 | return $this->setQueryOptionsFromRequestWithCustomFilter($request, $filter); |
||
92 | } |
||
93 | |||
94 | public function setRequestWithOrFilter(Request $request, $orFilter) |
||
95 | { |
||
96 | return $this->setQueryOptionsFromRequestWithCustomOrFilter($request, $orFilter); |
||
97 | } |
||
98 | 3 | ||
99 | public function setQueryOptions(QueryBuilderOptions $options) |
||
100 | 3 | { |
|
101 | $this->queryOptions = $options; |
||
102 | 3 | } |
|
103 | 3 | ||
104 | 3 | public function setQueryOptionsFromRequest(Request $request = null) |
|
105 | 3 | { |
|
106 | 3 | $requestAttributes = []; |
|
107 | 3 | foreach ($request->attributes->all() as $attributeName => $attributeValue) { |
|
108 | 3 | $requestAttributes[$attributeName] = $request->attributes->get( |
|
109 | 3 | $attributeName, |
|
110 | 3 | $attributeValue |
|
111 | ); |
||
112 | 3 | } |
|
113 | |||
114 | 3 | $filters = $request->query->get('filtering', []); |
|
115 | 3 | $orFilters = $request->query->get('filtering_or', []); |
|
116 | $sorting = $request->query->get('sorting', []); |
||
117 | $printing = $request->query->get('printing', []); |
||
118 | $rel = $request->query->get('rel', ''); |
||
119 | $page = $request->query->get('page', ''); |
||
120 | $select = $request->query->get('select', $this->entityAlias); |
||
121 | $pageLength = $request->query->get('limit', 666); |
||
122 | $filtering = $request->query->get('filtering', ''); |
||
123 | $limit = $request->query->get('limit', ''); |
||
124 | |||
125 | $filterOrCorrected = []; |
||
126 | |||
127 | 3 | $count = 0; |
|
128 | 3 | foreach ($orFilters as $key => $filter) { |
|
129 | 3 | if (is_array($filter)) { |
|
130 | 3 | foreach ($filter as $keyInternal => $internal) { |
|
131 | 3 | $filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
|
132 | 3 | $count = $count + 1; |
|
133 | 3 | } |
|
134 | 3 | } else { |
|
135 | 3 | $filterOrCorrected[$key] = $filter; |
|
136 | 3 | } |
|
137 | } |
||
138 | |||
139 | 3 | $requestProperties = [ |
|
140 | 3 | 'filtering' => $filtering, |
|
141 | 3 | 'orFiltering' => $filterOrCorrected, |
|
142 | 'limit' => $limit, |
||
143 | 'page' => $page, |
||
144 | 3 | 'filters' => $filters, |
|
145 | 'orFilters' => $filterOrCorrected, |
||
146 | 3 | 'sorting' => $sorting, |
|
147 | 'rel' => $rel, |
||
148 | 'printing' => $printing, |
||
149 | 4 | 'select' => $select, |
|
150 | ]; |
||
151 | 4 | ||
152 | $options = array_merge( |
||
153 | 4 | $requestAttributes, |
|
154 | $requestProperties |
||
155 | ); |
||
156 | |||
157 | $this->queryOptions = QueryBuilderOptions::fromArray($options); |
||
158 | |||
159 | return $this; |
||
160 | 4 | } |
|
161 | |||
162 | View Code Duplication | public function setQueryOptionsFromRequestWithCustomFilter(Request $request = null, $filter) |
|
163 | { |
||
164 | $filters = $request->query->get('filtering', []); |
||
165 | $orFilters = $request->query->get('filtering_or', []); |
||
166 | $sorting = $request->query->get('sorting', []); |
||
167 | $printing = $request->query->get('printing', []); |
||
168 | $rel = $request->query->get('rel', ''); |
||
169 | $page = $request->query->get('page', ''); |
||
170 | $select = $request->query->get('select', $this->entityAlias); |
||
171 | $pageLength = $request->query->get('limit', 666); |
||
172 | $filtering = $request->query->get('filtering', ''); |
||
173 | $limit = $request->query->get('limit', ''); |
||
174 | |||
175 | $filters = array_merge($filters, $filter); |
||
176 | |||
177 | $filterOrCorrected = []; |
||
178 | |||
179 | $count = 0; |
||
180 | foreach ($orFilters as $key => $filter) { |
||
181 | if (is_array($filter)) { |
||
182 | foreach ($filter as $keyInternal => $internal) { |
||
183 | $filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
||
184 | $count = $count + 1; |
||
185 | } |
||
186 | } else { |
||
187 | $filterOrCorrected[$key] = $filter; |
||
188 | } |
||
189 | } |
||
190 | |||
191 | $this->queryOptions = QueryBuilderOptions::fromArray([ |
||
192 | '_route' => $request->attributes->get('_route'), |
||
193 | 'customer_id' => $request->attributes->get('customer_id'), |
||
194 | 'id' => $request->attributes->get('id'), |
||
195 | 'filtering' => $filtering, |
||
196 | 'limit' => $limit, |
||
197 | 'page' => $page, |
||
198 | 'filters' => $filters, |
||
199 | 'orFilters' => $filterOrCorrected, |
||
200 | 'sorting' => $sorting, |
||
201 | 'rel' => $rel, |
||
202 | 'printing' => $printing, |
||
203 | 'select' => $select, |
||
204 | ]); |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | View Code Duplication | public function setQueryOptionsFromRequestWithCustomOrFilter(Request $request = null, $orFilter) |
|
210 | { |
||
211 | $filters = $request->query->get('filtering', []); |
||
212 | $orFilters = $request->query->get('filtering_or', []); |
||
213 | $sorting = $request->query->get('sorting', []); |
||
214 | $printing = $request->query->get('printing', []); |
||
215 | $rel = $request->query->get('rel', ''); |
||
216 | $page = $request->query->get('page', ''); |
||
217 | $select = $request->query->get('select', $this->entityAlias); |
||
218 | 1 | $pageLength = $request->query->get('limit', 666); |
|
219 | $filtering = $request->query->get('filtering', ''); |
||
220 | 1 | $limit = $request->query->get('limit', ''); |
|
221 | |||
222 | 1 | $orFilters = array_merge($orFilters, $orFilter); |
|
223 | 1 | ||
224 | 1 | $filterOrCorrected = []; |
|
225 | 1 | ||
226 | 1 | $count = 0; |
|
227 | 1 | foreach ($orFilters as $key => $filter) { |
|
228 | 1 | if (is_array($filter)) { |
|
229 | 1 | foreach ($filter as $keyInternal => $internal) { |
|
230 | 1 | $filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
|
231 | $count = $count + 1; |
||
232 | 1 | } |
|
233 | } else { |
||
234 | 1 | $filterOrCorrected[$key] = $filter; |
|
235 | } |
||
236 | 1 | } |
|
237 | 1 | ||
238 | $this->queryOptions = QueryBuilderOptions::fromArray([ |
||
239 | '_route' => $request->attributes->get('_route'), |
||
240 | 'customer_id' => $request->attributes->get('customer_id'), |
||
241 | 'id' => $request->attributes->get('id'), |
||
242 | 'filtering' => $filtering, |
||
243 | 'limit' => $limit, |
||
244 | 'page' => $page, |
||
245 | 'filters' => $filters, |
||
246 | 'orFilters' => $filterOrCorrected, |
||
247 | 'sorting' => $sorting, |
||
248 | 'rel' => $rel, |
||
249 | 1 | 'printing' => $printing, |
|
250 | 1 | 'select' => $select, |
|
251 | 1 | ]); |
|
252 | 1 | ||
253 | 1 | return $this; |
|
254 | 1 | } |
|
255 | 1 | ||
256 | 1 | public function getRequest() |
|
257 | 1 | { |
|
258 | 1 | return $this->request; |
|
259 | 1 | } |
|
260 | 1 | ||
261 | public function setRouteName($route_name = '') |
||
262 | { |
||
263 | 1 | $this->route_name = $route_name; |
|
264 | 1 | return $this; |
|
265 | 1 | } |
|
266 | |||
267 | public function findAllPaginated() |
||
268 | 1 | { |
|
269 | if ($this->configProvider) { |
||
270 | 1 | $this->setRequest($this->configProvider->getRequest()); |
|
271 | $this->queryBuilderFactory->setConfigProvider($this->configProvider); |
||
272 | } |
||
273 | 4 | ||
274 | $this->ensureQueryOptionIsDefined(); |
||
275 | 4 | ||
276 | $this->initFromQueryBuilderOptions($this->queryOptions); |
||
277 | |||
278 | $this->queryBuilderFactory->filter(); |
||
279 | $this->queryBuilderFactory->sort(); |
||
280 | |||
281 | $qb = $this->queryBuilderFactory->getQueryBuilder(); |
||
282 | |||
283 | if ($this->configProvider) { |
||
284 | $qb = $this->configProvider->filterRelation($qb); |
||
285 | } |
||
286 | |||
287 | return $this->paginateResults($qb); |
||
288 | } |
||
289 | |||
290 | protected function paginateResults( |
||
291 | \Doctrine\ORM\QueryBuilder $queryBuilder |
||
292 | ) { |
||
293 | $this->ensureQueryOptionIsDefined(); |
||
294 | |||
295 | $limit = $this->queryOptions->get('limit', 10); |
||
296 | $page = $this->queryOptions->get('page', 1); |
||
297 | |||
298 | |||
299 | $pagerAdapter = new DoctrineORMAdapter($queryBuilder); |
||
300 | |||
301 | $query = $pagerAdapter->getQuery(); |
||
302 | if(isset($this->use_result_cache) and $this->use_result_cache){ |
||
303 | $query->useResultCache(true, 600); |
||
304 | } |
||
305 | |||
306 | $pager = new Pagerfanta($pagerAdapter); |
||
307 | $pager->setNormalizeOutOfRangePages(true); |
||
308 | $pager->setMaxPerPage($limit); |
||
309 | $pager->setCurrentPage($page); |
||
310 | |||
311 | $pagerFactory = new PagerfantaFactory(); |
||
312 | |||
313 | $router = $this->createRouter(); |
||
314 | |||
315 | $results = $pagerFactory->createRepresentation($pager, $router); |
||
316 | |||
317 | return $results; |
||
318 | } |
||
319 | |||
320 | protected function customQueryStringValues() |
||
323 | } |
||
324 | |||
325 | protected function createRouter() |
||
326 | { |
||
327 | $request = $this->getRequest(); |
||
328 | $params = []; |
||
329 | |||
330 | $list = array_merge([ |
||
331 | 'filtering', |
||
332 | 'limit', |
||
333 | 'page', |
||
334 | 'sorting', |
||
335 | ], $this->customQueryStringValues()); |
||
336 | |||
337 | $this->ensureQueryOptionIsDefined(); |
||
338 | |||
339 | foreach ($list as $itemKey => $itemValue) { |
||
340 | $params[$itemValue] = $this->queryOptions->get($itemValue); |
||
341 | } |
||
342 | |||
343 | if(!isset($this->route_name)){ |
||
344 | $this->route_name = $this->queryOptions->get('_route'); |
||
345 | } |
||
346 | |||
347 | return new Route($this->route_name, $params); |
||
348 | } |
||
349 | |||
350 | protected function getCurrentEntityAlias() : string |
||
351 | { |
||
352 | return $this->currentEntityAlias; |
||
353 | } |
||
354 | |||
355 | protected function setCurrentEntityAlias(string $currentEntityAlias) |
||
356 | { |
||
357 | $this->currentEntityAlias = $currentEntityAlias; |
||
358 | } |
||
359 | |||
360 | protected function getEmbeddedFields() : array |
||
361 | { |
||
362 | return $this->embeddedFields; |
||
363 | } |
||
364 | |||
365 | protected function setEmbeddedFields(array $embeddedFields) |
||
366 | { |
||
367 | $this->embeddedFields = $embeddedFields; |
||
368 | } |
||
369 | |||
370 | public function getEntityAlias(string $entityName) : string |
||
371 | { |
||
372 | $arrayEntityName = explode('\\', strtolower($entityName) ); |
||
373 | return $arrayEntityName[count($arrayEntityName)-1]; |
||
374 | } |
||
375 | |||
376 | protected function relationship($queryBuilder) |
||
379 | } |
||
380 | |||
381 | public function getQueryBuilderFactoryWithoutInitialization() |
||
382 | { |
||
383 | return $this->queryBuilderFactory; |
||
384 | } |
||
385 | |||
386 | public function setConfigProvider(ConfigProvider $provider, array $domainConfiguration = []) |
||
387 | { |
||
392 | } |
||
393 | |||
394 | public function ensureQueryOptionIsDefined() |
||
395 | { |
||
399 | ); |
||
400 | } |
||
401 | } |
||
402 | } |
||
403 |
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