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) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function createViewData() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function getViewData(DocumentIterator $result, ViewData $data) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public function isRelated() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Fetches buckets from search results. |
||
| 289 | * |
||
| 290 | * @param DocumentIterator $result Search results. |
||
| 291 | * @param string $filterName Filter name. |
||
| 292 | * @param array $values Values from the state object |
||
| 293 | * |
||
| 294 | * @return array Buckets. |
||
| 295 | */ |
||
| 296 | protected function fetchAggregation(DocumentIterator $result, $filterName, $values) |
||
| 297 | { |
||
| 298 | $data = []; |
||
| 299 | $values = empty($values) ? [] : $values; |
||
| 300 | $aggregation = $result->getAggregation(sprintf('%s-filter', $filterName)); |
||
| 301 | |||
| 302 | foreach ($values as $name => $value) { |
||
| 303 | $data[$name] = $aggregation->find(sprintf('%s.%s.query', $name, $filterName)); |
||
| 304 | } |
||
| 305 | |||
| 306 | $data['all-selected'] = $aggregation->find(sprintf('all-selected.%s.query', $filterName)); |
||
| 307 | |||
| 308 | return $data; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * A method used to add an additional filter to the aggregations |
||
| 313 | * in preProcessSearch |
||
| 314 | * |
||
| 315 | * @param FilterAggregation $filterAggregation |
||
| 316 | * @param NestedAggregation $deepLevelAggregation |
||
| 317 | * @param array $terms Terms of additional filter |
||
| 318 | * @param string $aggName |
||
| 319 | * |
||
| 320 | * @return BuilderInterface |
||
| 321 | */ |
||
| 322 | View Code Duplication | protected function addSubFilterAggregation( |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $key |
||
| 351 | * @param string $name |
||
| 352 | * @param ViewData $data |
||
| 353 | * @param bool $active True when the choice is active |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | protected function getOptionUrlParameters($key, $name, ViewData $data, $active) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns whether choice with the specified key is active. |
||
| 380 | * |
||
| 381 | * @param string $key |
||
| 382 | * @param ViewData $data |
||
| 383 | * @param string $activeName |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | protected function isChoiceActive($key, ViewData $data, $activeName) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Forms $unsortedChoices array with all possible choices. |
||
| 394 | * 0 is assigned to the document count of the choices. |
||
| 395 | * |
||
| 396 | * @param DocumentIterator $result |
||
| 397 | * @param ViewData $data |
||
| 398 | * |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | private function formInitialUnsortedChoices($result, $data) |
||
| 422 | } |
||
| 423 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: