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 |
||
| 37 | class DynamicAggregate extends AbstractFilter implements ViewDataFactoryInterface |
||
| 38 | { |
||
| 39 | use SortAwareTrait; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | public function getNameField() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return bool |
||
| 51 | */ |
||
| 52 | public function getShowZeroChoices() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function getState(Request $request) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function createViewData() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function getViewData(DocumentIterator $result, ViewData $data) |
||
| 157 | { |
||
| 158 | $unsortedChoices = []; |
||
| 159 | $activeNames = $data->getState()->isActive() ? array_keys($data->getState()->getValue()) : []; |
||
| 160 | $filterAggregations = $this->fetchAggregation($result, $data->getName(), $data->getState()->getValue()); |
||
| 161 | |||
| 162 | if ($this->getShowZeroChoices()) { |
||
| 163 | $unsortedChoices = $this->formInitialUnsortedChoices($result, $data); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** @var AggregationValue $bucket */ |
||
| 167 | foreach ($filterAggregations as $activeName => $filterAggregation) { |
||
| 168 | foreach ($filterAggregation as $nameAggregation) { |
||
| 169 | $name = $nameAggregation['key']; |
||
| 170 | |||
| 171 | if (($name != $activeName && $activeName != 'all-selected') || |
||
| 172 | ($activeName == 'all-selected' && in_array($name, $activeNames))) { |
||
| 173 | continue; |
||
| 174 | } |
||
| 175 | |||
| 176 | foreach ($nameAggregation['value']['buckets'] as $bucket) { |
||
| 177 | $choice = $this->createChoice($data, $name, $activeName, $bucket); |
||
| 178 | $unsortedChoices[$name][$bucket['key']] = $choice; |
||
| 179 | } |
||
| 180 | |||
| 181 | $this->addViewDataItem($data, $name, $unsortedChoices[$name]); |
||
| 182 | unset($unsortedChoices[$name]); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($this->getShowZeroChoices() && !empty($unsortedChoices)) { |
||
| 187 | foreach ($unsortedChoices as $name => $choices) { |
||
| 188 | $this->addViewDataItem($data, $name, $unsortedChoices[$name]); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** @var AggregateViewData $data */ |
||
| 193 | $data->sortItems(); |
||
| 194 | |||
| 195 | return $data; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function isRelated() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Fetches buckets from search results. |
||
| 208 | * |
||
| 209 | * @param DocumentIterator $result Search results. |
||
| 210 | * @param string $filterName Filter name. |
||
| 211 | * @param array $values Values from the state object |
||
| 212 | * |
||
| 213 | * @return array Buckets. |
||
| 214 | */ |
||
| 215 | protected function fetchAggregation(DocumentIterator $result, $filterName, $values) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * A method used to add an additional filter to the aggregations |
||
| 232 | * in preProcessSearch |
||
| 233 | * |
||
| 234 | * @param FilterAggregation $filterAggregation |
||
| 235 | * @param NestedAggregation $deepLevelAggregation |
||
| 236 | * @param array $terms Terms of additional filter |
||
| 237 | * @param string $aggName |
||
| 238 | * |
||
| 239 | * @return BuilderInterface |
||
| 240 | */ |
||
| 241 | protected function addSubFilterAggregation( |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $key |
||
| 265 | * @param string $name |
||
| 266 | * @param ViewData $data |
||
| 267 | * @param bool $active True when the choice is active |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | protected function getOptionUrlParameters($key, $name, ViewData $data, $active) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Returns whether choice with the specified key is active. |
||
| 294 | * |
||
| 295 | * @param string $key |
||
| 296 | * @param ViewData $data |
||
| 297 | * @param string $activeName |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | View Code Duplication | protected function isChoiceActive($key, ViewData $data, $activeName) |
|
| 302 | { |
||
| 303 | if ($data->getState()->isActive()) { |
||
| 304 | $value = $data->getState()->getValue(); |
||
| 305 | |||
| 306 | if (isset($value[$activeName]) && $key == $value[$activeName]) { |
||
| 307 | return true; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | return false; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Forms $unsortedChoices array with all possible choices. |
||
| 316 | * 0 is assigned to the document count of the choices. |
||
| 317 | * |
||
| 318 | * @param DocumentIterator $result |
||
| 319 | * @param ViewData $data |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | protected function formInitialUnsortedChoices($result, $data) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param AggregateViewData $data |
||
| 346 | * @param string $name |
||
| 347 | * @param string $activeName |
||
| 348 | * @param AggregationValue $bucket |
||
| 349 | * @param array $urlParameters |
||
| 350 | * |
||
| 351 | * @return ViewData\ChoiceAwareViewData |
||
| 352 | */ |
||
| 353 | protected function createChoice($data, $name, $activeName, $bucket, $urlParameters = null) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param AggregateViewData $data |
||
| 372 | * @param string $name |
||
| 373 | * @param ViewData\ChoiceAwareViewData[] $choices |
||
| 374 | */ |
||
| 375 | protected function addViewDataItem($data, $name, $choices) |
||
| 384 | } |
||
| 385 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: