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 DynamicAggregate 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DynamicAggregate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class DynamicAggregate extends AbstractSingleRequestValueFilter implements |
||
40 | FieldAwareInterface, |
||
41 | ViewDataFactoryInterface |
||
42 | { |
||
43 | use FieldAwareTrait; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $sortType; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $nameField; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $showZeroChoices; |
||
59 | |||
60 | /** |
||
61 | * @param array $sortType |
||
62 | */ |
||
63 | public function setSortType($sortType) |
||
67 | |||
68 | /** |
||
69 | * @return array |
||
70 | */ |
||
71 | public function getSortType() |
||
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | public function getNameField() |
||
83 | |||
84 | /** |
||
85 | * @param string $nameField |
||
86 | */ |
||
87 | public function setNameField($nameField) |
||
91 | |||
92 | /** |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function getShowZeroChoices() |
||
96 | { |
||
97 | return $this->showZeroChoices; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param bool $showZeroChoices |
||
102 | */ |
||
103 | public function setShowZeroChoices($showZeroChoices) |
||
104 | { |
||
105 | $this->showZeroChoices = $showZeroChoices; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function getState(Request $request) |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null) |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null) |
||
150 | { |
||
151 | list($path, $field) = explode('>', $this->getField()); |
||
152 | $name = $state->getName(); |
||
|
|||
153 | $aggregation = new NestedAggregation( |
||
154 | $name, |
||
155 | $path |
||
156 | ); |
||
157 | $termsAggregation = new TermsAggregation('query', $field); |
||
158 | $termsAggregation->addParameter('size', 0); |
||
159 | |||
160 | if ($this->getSortType()) { |
||
161 | $termsAggregation->addParameter('order', [$this->getSortType()['type'] => $this->getSortType()['order']]); |
||
162 | } |
||
163 | |||
164 | $termsAggregation->addAggregation( |
||
165 | new TermsAggregation('name', $this->getNameField()) |
||
166 | ); |
||
167 | $aggregation->addAggregation($termsAggregation); |
||
168 | $filterAggregation = new FilterAggregation($name . '-filter'); |
||
169 | |||
170 | if (!empty($relatedSearch->getPostFilters())) { |
||
171 | $filterAggregation->setFilter($relatedSearch->getPostFilters()); |
||
172 | } else { |
||
173 | $filterAggregation->setFilter(new MatchAllQuery()); |
||
174 | } |
||
175 | |||
176 | if ($state->isActive()) { |
||
177 | foreach ($state->getValue() as $key => $term) { |
||
178 | $terms = $state->getValue(); |
||
179 | unset($terms[$key]); |
||
180 | |||
181 | $this->addSubFilterAggregation( |
||
182 | $filterAggregation, |
||
183 | $aggregation, |
||
184 | $terms, |
||
185 | $key |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | $this->addSubFilterAggregation( |
||
190 | $filterAggregation, |
||
191 | $aggregation, |
||
192 | $state->getValue(), |
||
193 | 'all-selected' |
||
194 | ); |
||
195 | } else { |
||
196 | $filterAggregation->addAggregation($aggregation); |
||
197 | } |
||
198 | |||
199 | $search->addAggregation($filterAggregation); |
||
200 | |||
201 | if ($this->getShowZeroChoices()) { |
||
202 | $search->addAggregation($aggregation); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function createViewData() |
||
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | */ |
||
217 | public function getViewData(DocumentIterator $result, ViewData $data) |
||
218 | { |
||
219 | $unsortedChoices = []; |
||
220 | $activeNames = $data->getState()->isActive() ? array_keys($data->getState()->getValue()) : []; |
||
221 | $filterAggregations = $this->fetchAggregation($result, $data->getName(), $data->getState()->getValue()); |
||
222 | |||
223 | if ($this->getShowZeroChoices() && $data->getState()->isActive()) { |
||
224 | $unsortedChoices = $this->formInitialUnsortedChoices($result, $data); |
||
225 | } |
||
226 | |||
227 | /** @var AggregationValue $bucket */ |
||
228 | foreach ($filterAggregations as $activeName => $aggregation) { |
||
229 | foreach ($aggregation as $bucket) { |
||
230 | $name = $bucket->getAggregation('name')->getBuckets()[0]['key']; |
||
231 | |||
232 | if ($name != $activeName && $activeName != 'all-selected') { |
||
233 | continue; |
||
234 | } |
||
235 | |||
236 | $active = $this->isChoiceActive($bucket['key'], $data, $activeName); |
||
237 | $choice = new ViewData\Choice(); |
||
238 | $choice->setLabel($bucket->getValue('key')); |
||
239 | $choice->setCount($bucket['doc_count']); |
||
240 | $choice->setActive($active); |
||
241 | |||
242 | $choice->setUrlParameters( |
||
243 | $this->getOptionUrlParameters($bucket['key'], $name, $data, $active) |
||
244 | ); |
||
245 | |||
246 | if ($activeName == 'all-selected') { |
||
247 | $unsortedChoices[$activeName][$name][$bucket['key']] = $choice; |
||
248 | } else { |
||
249 | $unsortedChoices[$activeName][$bucket['key']] = $choice; |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | |||
254 | if (isset($unsortedChoices['all-selected'])) { |
||
255 | foreach ($unsortedChoices['all-selected'] as $name => $buckets) { |
||
256 | if (in_array($name, $activeNames)) { |
||
257 | continue; |
||
258 | } |
||
259 | |||
260 | $unsortedChoices[$name] = $buckets; |
||
261 | } |
||
262 | |||
263 | unset($unsortedChoices['all-selected']); |
||
264 | } |
||
265 | |||
266 | ksort($unsortedChoices); |
||
267 | |||
268 | /** @var AggregateViewData $data */ |
||
269 | foreach ($unsortedChoices as $name => $choices) { |
||
270 | $choiceViewData = new ViewData\ChoicesAwareViewData(); |
||
271 | $choiceViewData->setName($name); |
||
272 | $choiceViewData->setChoices($choices); |
||
273 | $choiceViewData->setUrlParameters([]); |
||
274 | $choiceViewData->setResetUrlParameters([]); |
||
275 | $data->addItem($choiceViewData); |
||
276 | } |
||
277 | |||
278 | return $data; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function isRelated() |
||
288 | |||
289 | /** |
||
290 | * Fetches buckets from search results. |
||
291 | * |
||
292 | * @param DocumentIterator $result Search results. |
||
293 | * @param string $filterName Filter name. |
||
294 | * @param array $values Values from the state object |
||
295 | * |
||
296 | * @return array Buckets. |
||
297 | */ |
||
298 | View Code Duplication | protected function fetchAggregation(DocumentIterator $result, $filterName, $values) |
|
322 | |||
323 | /** |
||
324 | * A method used to add an additional filter to the aggregations |
||
325 | * in preProcessSearch |
||
326 | * |
||
327 | * @param FilterAggregation $filterAggregation |
||
328 | * @param NestedAggregation $deepLevelAggregation |
||
329 | * @param array $terms Terms of additional filter |
||
330 | * @param string $aggName |
||
331 | * |
||
332 | * @return BuilderInterface |
||
333 | */ |
||
334 | View Code Duplication | protected function addSubFilterAggregation( |
|
360 | |||
361 | /** |
||
362 | * @param string $key |
||
363 | * @param string $name |
||
364 | * @param ViewData $data |
||
365 | * @param bool $active True when the choice is active |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | protected function getOptionUrlParameters($key, $name, ViewData $data, $active) |
||
389 | |||
390 | /** |
||
391 | * Returns whether choice with the specified key is active. |
||
392 | * |
||
393 | * @param string $key |
||
394 | * @param ViewData $data |
||
395 | * @param string $activeName |
||
396 | * |
||
397 | * @return bool |
||
398 | */ |
||
399 | protected function isChoiceActive($key, ViewData $data, $activeName) |
||
403 | |||
404 | /** |
||
405 | * Forms $unsortedChoices array with all possible choices. |
||
406 | * 0 is assigned to the document count of the choices. |
||
407 | * |
||
408 | * @param DocumentIterator $result |
||
409 | * @param ViewData $data |
||
410 | * |
||
411 | * @return array |
||
412 | */ |
||
413 | private function formInitialUnsortedChoices($result, $data) |
||
434 | } |
||
435 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: