Total Complexity | 70 |
Total Lines | 416 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CriteriaParser 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 CriteriaParser, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
47 | class CriteriaParser |
||
48 | { |
||
49 | /** |
||
50 | * @var EntityDefinitionQueryHelper |
||
51 | */ |
||
52 | private $helper; |
||
53 | |||
54 | public function __construct(EntityDefinitionQueryHelper $helper) |
||
57 | } |
||
58 | |||
59 | public function buildAccessor(EntityDefinition $definition, string $fieldName, Context $context): string |
||
|
|||
60 | { |
||
61 | $root = $definition->getEntityName(); |
||
62 | |||
63 | $parts = explode('.', $fieldName); |
||
64 | if ($root === $parts[0]) { |
||
65 | array_shift($parts); |
||
66 | } |
||
67 | |||
68 | $field = $this->helper->getField($fieldName, $definition, $root, false); |
||
69 | if ($field instanceof TranslatedField) { |
||
70 | $ordered = []; |
||
71 | foreach ($parts as $part) { |
||
72 | if ($part === $field->getPropertyName()) { |
||
73 | $ordered[] = 'translated'; |
||
74 | } |
||
75 | $ordered[] = $part; |
||
76 | } |
||
77 | $parts = $ordered; |
||
78 | } |
||
79 | |||
80 | if ($field instanceof PriceField) { |
||
81 | $parts[] = 'gross'; |
||
82 | } |
||
83 | |||
84 | return implode('.', $parts); |
||
85 | } |
||
86 | |||
87 | public function parseSorting(FieldSorting $sorting, EntityDefinition $definition, Context $context): FieldSort |
||
92 | } |
||
93 | |||
94 | public function parseAggregation(Aggregation $aggregation, EntityDefinition $definition, Context $context): ?AbstractAggregation |
||
115 | } |
||
116 | |||
117 | public function parseFilter(Filter $filter, EntityDefinition $definition, string $root, Context $context): BuilderInterface |
||
118 | { |
||
119 | switch (true) { |
||
120 | case $filter instanceof NotFilter: |
||
121 | return $this->parseNotFilter($filter, $definition, $root, $context); |
||
122 | |||
123 | case $filter instanceof MultiFilter: |
||
124 | return $this->parseMultiFilter($filter, $definition, $root, $context); |
||
125 | |||
126 | case $filter instanceof EqualsFilter: |
||
127 | return $this->parseEqualsFilter($filter, $definition, $context); |
||
128 | |||
129 | case $filter instanceof EqualsAnyFilter: |
||
130 | return $this->parseEqualsAnyFilter($filter, $definition, $context); |
||
131 | |||
132 | case $filter instanceof ContainsFilter: |
||
133 | return $this->parseContainsFilter($filter, $definition, $context); |
||
134 | |||
135 | case $filter instanceof RangeFilter: |
||
136 | return $this->parseRangeFilter($filter, $definition, $context); |
||
137 | |||
138 | default: |
||
139 | throw new \RuntimeException(sprintf('Unsupported filter %s', \get_class($filter))); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | protected function parseFilterAggregation(FilterAggregation $aggregation, EntityDefinition $definition, Context $context): Bucketing\FilterAggregation |
||
144 | { |
||
145 | $query = new BoolQuery(); |
||
146 | foreach ($aggregation->getFilter() as $filter) { |
||
147 | $parsed = $this->parseFilter($filter, $definition, $definition->getEntityName(), $context); |
||
148 | if ($parsed instanceof NestedQuery) { |
||
149 | $parsed = $parsed->getQuery(); |
||
150 | } |
||
151 | $query->add($parsed); |
||
152 | } |
||
153 | |||
154 | $filter = new Bucketing\FilterAggregation($aggregation->getName(), $query); |
||
155 | |||
156 | $nested = $aggregation->getAggregation(); |
||
157 | |||
158 | if (!$nested) { |
||
159 | throw new \RuntimeException(sprintf('Filter aggregation %s contains no nested aggregation.', $aggregation->getName())); |
||
160 | } |
||
161 | |||
162 | $filter->addAggregation( |
||
163 | $this->parseNestedAggregation($nested, $definition, $context) |
||
164 | ); |
||
165 | |||
166 | return $filter; |
||
167 | } |
||
168 | |||
169 | protected function parseTermsAggregation(TermsAggregation $aggregation, string $fieldName, EntityDefinition $definition, Context $context): AbstractAggregation |
||
170 | { |
||
171 | if ($aggregation->getSorting() === null) { |
||
172 | $terms = new Bucketing\TermsAggregation($aggregation->getName(), $fieldName); |
||
173 | |||
174 | if ($nested = $aggregation->getAggregation()) { |
||
175 | $terms->addAggregation( |
||
176 | $this->parseNestedAggregation($nested, $definition, $context) |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | // set default size to 10.000 => max for default configuration |
||
181 | $terms->addParameter('size', ElasticsearchHelper::MAX_SIZE_VALUE); |
||
182 | |||
183 | if ($aggregation->getLimit()) { |
||
184 | $terms->addParameter('size', (string) $aggregation->getLimit()); |
||
185 | } |
||
186 | |||
187 | return $terms; |
||
188 | } |
||
189 | |||
190 | $composite = new CompositeAggregation($aggregation->getName()); |
||
191 | |||
192 | $accessor = $this->buildAccessor($definition, $aggregation->getSorting()->getField(), $context); |
||
193 | |||
194 | $sorting = new Bucketing\TermsAggregation($aggregation->getName() . '.sorting', $accessor); |
||
195 | $sorting->addParameter('order', $aggregation->getSorting()->getDirection()); |
||
196 | $composite->addSource($sorting); |
||
197 | |||
198 | $terms = new Bucketing\TermsAggregation($aggregation->getName() . '.key', $fieldName); |
||
199 | $composite->addSource($terms); |
||
200 | |||
201 | if ($nested = $aggregation->getAggregation()) { |
||
202 | $composite->addAggregation( |
||
203 | $this->parseNestedAggregation($nested, $definition, $context) |
||
204 | ); |
||
205 | } |
||
206 | |||
207 | // set default size to 10.000 => max for default configuration |
||
208 | $composite->addParameter('size', ElasticsearchHelper::MAX_SIZE_VALUE); |
||
209 | |||
210 | if ($aggregation->getLimit()) { |
||
211 | $composite->addParameter('size', (string) $aggregation->getLimit()); |
||
212 | } |
||
213 | |||
214 | return $composite; |
||
215 | } |
||
216 | |||
217 | protected function parseEntityAggregation(EntityAggregation $aggregation, string $fieldName): Bucketing\TermsAggregation |
||
218 | { |
||
219 | $bucketingAggregation = new Bucketing\TermsAggregation($aggregation->getName(), $fieldName); |
||
220 | |||
221 | $bucketingAggregation->addParameter('size', ElasticsearchHelper::MAX_SIZE_VALUE); |
||
222 | |||
223 | return $bucketingAggregation; |
||
224 | } |
||
225 | |||
226 | protected function parseDateHistogramAggregation(DateHistogramAggregation $aggregation, string $fieldName, EntityDefinition $definition, Context $context): CompositeAggregation |
||
227 | { |
||
228 | $composite = new CompositeAggregation($aggregation->getName()); |
||
229 | |||
230 | if ($aggregation->getSorting()) { |
||
231 | $accessor = $this->buildAccessor($definition, $aggregation->getSorting()->getField(), $context); |
||
232 | |||
233 | $sorting = new Bucketing\TermsAggregation($aggregation->getName() . '.sorting', $accessor); |
||
234 | $sorting->addParameter('order', $aggregation->getSorting()->getDirection()); |
||
235 | |||
236 | $composite->addSource($sorting); |
||
237 | } |
||
238 | |||
239 | $histogram = new Bucketing\DateHistogramAggregation( |
||
240 | $aggregation->getName() . '.key', |
||
241 | $fieldName, |
||
242 | $aggregation->getInterval(), |
||
243 | 'yyyy-MM-dd HH:mm:ss' |
||
244 | ); |
||
245 | $composite->addSource($histogram); |
||
246 | |||
247 | if ($nested = $aggregation->getAggregation()) { |
||
248 | $composite->addAggregation( |
||
249 | $this->parseNestedAggregation($nested, $definition, $context) |
||
250 | ); |
||
251 | } |
||
252 | |||
253 | return $composite; |
||
254 | } |
||
255 | |||
256 | private function parseNestedAggregation(Aggregation $aggregation, EntityDefinition $definition, Context $context): AbstractAggregation |
||
257 | { |
||
258 | $fieldName = $this->buildAccessor($definition, $aggregation->getField(), $context); |
||
259 | |||
260 | return $this->createAggregation($aggregation, $fieldName, $definition, $context); |
||
261 | } |
||
262 | |||
263 | private function createAggregation(Aggregation $aggregation, string $fieldName, EntityDefinition $definition, Context $context): AbstractAggregation |
||
264 | { |
||
265 | switch (true) { |
||
266 | case $aggregation instanceof StatsAggregation: |
||
267 | return new Metric\StatsAggregation($aggregation->getName(), $fieldName); |
||
268 | |||
269 | case $aggregation instanceof AvgAggregation: |
||
270 | return new Metric\AvgAggregation($aggregation->getName(), $fieldName); |
||
271 | |||
272 | case $aggregation instanceof EntityAggregation: |
||
273 | return $this->parseEntityAggregation($aggregation, $fieldName); |
||
274 | |||
275 | case $aggregation instanceof MaxAggregation: |
||
276 | return new Metric\MaxAggregation($aggregation->getName(), $fieldName); |
||
277 | |||
278 | case $aggregation instanceof MinAggregation: |
||
279 | return new Metric\MinAggregation($aggregation->getName(), $fieldName); |
||
280 | |||
281 | case $aggregation instanceof SumAggregation: |
||
282 | return new Metric\SumAggregation($aggregation->getName(), $fieldName); |
||
283 | |||
284 | case $aggregation instanceof CountAggregation: |
||
285 | return new ValueCountAggregation($aggregation->getName(), $fieldName); |
||
286 | |||
287 | case $aggregation instanceof FilterAggregation: |
||
288 | return $this->parseFilterAggregation($aggregation, $definition, $context); |
||
289 | |||
290 | case $aggregation instanceof TermsAggregation: |
||
291 | return $this->parseTermsAggregation($aggregation, $fieldName, $definition, $context); |
||
292 | |||
293 | case $aggregation instanceof DateHistogramAggregation: |
||
294 | return $this->parseDateHistogramAggregation($aggregation, $fieldName, $definition, $context); |
||
295 | default: |
||
296 | throw new \RuntimeException(sprintf('Provided aggregation of class %s not supported', \get_class($aggregation))); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | private function parseEqualsFilter(EqualsFilter $filter, EntityDefinition $definition, Context $context): BuilderInterface |
||
301 | { |
||
302 | $fieldName = $this->buildAccessor($definition, $filter->getField(), $context); |
||
303 | |||
304 | if ($filter->getValue() === null) { |
||
305 | $query = new BoolQuery(); |
||
306 | $query->add(new ExistsQuery($fieldName), BoolQuery::MUST_NOT); |
||
307 | } else { |
||
308 | $query = new TermQuery($fieldName, $filter->getValue()); |
||
309 | } |
||
310 | |||
311 | return $this->createNestedQuery($query, $definition, $filter->getField()); |
||
312 | } |
||
313 | |||
314 | private function parseEqualsAnyFilter(EqualsAnyFilter $filter, EntityDefinition $definition, Context $context): BuilderInterface |
||
322 | ); |
||
323 | } |
||
324 | |||
325 | private function parseContainsFilter(ContainsFilter $filter, EntityDefinition $definition, Context $context): BuilderInterface |
||
326 | { |
||
327 | $accessor = $this->buildAccessor($definition, $filter->getField(), $context); |
||
328 | |||
329 | /** @var string $value */ |
||
330 | $value = $filter->getValue(); |
||
331 | |||
332 | return $this->createNestedQuery( |
||
333 | new WildcardQuery($accessor, '*' . $value . '*'), |
||
334 | $definition, |
||
335 | $filter->getField() |
||
336 | ); |
||
337 | } |
||
338 | |||
339 | private function parseRangeFilter(RangeFilter $filter, EntityDefinition $definition, Context $context): BuilderInterface |
||
347 | ); |
||
348 | } |
||
349 | |||
350 | private function parseNotFilter(NotFilter $filter, EntityDefinition $definition, string $root, Context $context): BuilderInterface |
||
351 | { |
||
352 | $bool = new BoolQuery(); |
||
353 | foreach ($filter->getQueries() as $nested) { |
||
354 | $bool->add( |
||
355 | $this->parseFilter($nested, $definition, $root, $context), |
||
356 | BoolQuery::MUST_NOT |
||
357 | ); |
||
358 | } |
||
359 | |||
360 | return $bool; |
||
361 | } |
||
362 | |||
363 | private function parseMultiFilter(MultiFilter $filter, EntityDefinition $definition, string $root, Context $context): BuilderInterface |
||
364 | { |
||
365 | switch ($filter->getOperator()) { |
||
366 | case MultiFilter::CONNECTION_OR: |
||
367 | return $this->parseOrMultiFilter($filter, $definition, $root, $context); |
||
368 | case MultiFilter::CONNECTION_AND: |
||
369 | return $this->parseAndMultiFilter($filter, $definition, $root, $context); |
||
370 | case MultiFilter::CONNECTION_XOR: |
||
371 | return $this->parseXorMultiFilter($filter, $definition, $root, $context); |
||
372 | } |
||
373 | |||
374 | throw new \InvalidArgumentException('Operator ' . $filter->getOperator() . ' not allowed'); |
||
375 | } |
||
376 | |||
377 | private function parseAndMultiFilter(MultiFilter $filter, EntityDefinition $definition, string $root, Context $context): BuilderInterface |
||
378 | { |
||
379 | $bool = new BoolQuery(); |
||
380 | |||
381 | foreach ($filter->getQueries() as $nested) { |
||
382 | $bool->add( |
||
383 | $this->parseFilter($nested, $definition, $root, $context), |
||
384 | BoolQuery::MUST |
||
385 | ); |
||
386 | } |
||
387 | |||
388 | return $bool; |
||
389 | } |
||
390 | |||
391 | private function parseOrMultiFilter(MultiFilter $filter, EntityDefinition $definition, string $root, Context $context): BuilderInterface |
||
403 | } |
||
404 | |||
405 | private function parseXorMultiFilter(MultiFilter $filter, EntityDefinition $definition, string $root, Context $context): BuilderInterface |
||
406 | { |
||
407 | $bool = new BoolQuery(); |
||
408 | |||
409 | foreach ($filter->getQueries() as $nested) { |
||
410 | $xorQuery = new BoolQuery(); |
||
411 | foreach ($filter->getQueries() as $mustNot) { |
||
412 | if ($nested === $mustNot) { |
||
413 | $xorQuery->add($this->parseFilter($nested, $definition, $root, $context), BoolQuery::MUST); |
||
414 | |||
415 | continue; |
||
416 | } |
||
417 | |||
418 | $xorQuery->add($this->parseFilter($mustNot, $definition, $root, $context), BoolQuery::MUST_NOT); |
||
419 | } |
||
420 | |||
421 | $bool->add( |
||
422 | $xorQuery, |
||
423 | BoolQuery::SHOULD |
||
424 | ); |
||
425 | } |
||
426 | |||
427 | return $bool; |
||
428 | } |
||
429 | |||
430 | private function createNestedQuery(BuilderInterface $query, EntityDefinition $definition, string $field): BuilderInterface |
||
439 | } |
||
440 | |||
441 | private function getNestedPath(EntityDefinition $definition, string $accessor): ?string |
||
463 | } |
||
464 | } |
||
465 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.