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:
1 | <?php |
||
22 | class MultiDynamicAggregateFilter extends DynamicAggregateFilter |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public function getState(Request $request) |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null) |
||
70 | |||
71 | /** |
||
72 | * Fetches buckets from search results. |
||
73 | * |
||
74 | * @param DocumentIterator $result Search results. |
||
75 | * @param string $filterName Filter name. |
||
76 | * @param array $values Values from the state object |
||
77 | * |
||
78 | * @return array Buckets. |
||
79 | */ |
||
80 | View Code Duplication | protected function fetchAggregation(DocumentIterator $result, $filterName, $values) |
|
104 | |||
105 | /** |
||
106 | * A method used to add an additional filter to the aggregations |
||
107 | * in preProcessSearch |
||
108 | * |
||
109 | * @param FilterAggregation $filterAggregation |
||
110 | * @param NestedAggregation $deepLevelAggregation |
||
111 | * @param array $terms Terms of additional filter |
||
112 | * @param string $aggName |
||
113 | * |
||
114 | * @return BuilderInterface |
||
115 | */ |
||
116 | View Code Duplication | protected function addSubFilterAggregation( |
|
142 | |||
143 | /** |
||
144 | * @param string $key |
||
145 | * @param string $name |
||
146 | * @param ViewData $data |
||
147 | * @param bool $active True when the choice is active |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | protected function getOptionUrlParameters($key, $name, ViewData $data, $active) |
||
171 | |||
172 | /** |
||
173 | * Returns whether choice with the specified key is active. |
||
174 | * |
||
175 | * @param string $key |
||
176 | * @param ViewData $data |
||
177 | * @param string $activeName |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | protected function isChoiceActive($key, ViewData $data, $activeName) |
||
187 | } |
||
188 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.