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 |
||
26 | class GridBuilder implements GridBuilderInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var ColumnBuilderInterface |
||
30 | */ |
||
31 | private $columnBuilder; |
||
32 | |||
33 | /** |
||
34 | * @var FilterBuilderInterface |
||
35 | */ |
||
36 | private $filterBuilder; |
||
37 | |||
38 | /** |
||
39 | * @var SortBuilderInterface |
||
40 | */ |
||
41 | private $sortBuilder; |
||
42 | |||
43 | /** |
||
44 | * @var ActionBuilderInterface |
||
45 | */ |
||
46 | private $actionBuilder; |
||
47 | |||
48 | /** |
||
49 | * @var BatchBuilderInterface |
||
50 | */ |
||
51 | private $batchBuilder; |
||
52 | |||
53 | /** |
||
54 | * @param ColumnBuilderInterface $columnBuilder |
||
55 | * @param FilterBuilderInterface $filterBuilder |
||
56 | * @param SortBuilderInterface $sortBuilder |
||
57 | * @param ActionBuilderInterface $actionBuilder |
||
58 | * @param BatchBuilderInterface $batchBuilder |
||
59 | */ |
||
60 | 7 | public function __construct( |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 5 | public function build(array $config) |
|
93 | |||
94 | /** |
||
95 | * @param mixed[] $config |
||
96 | * |
||
97 | * @return ResourceInterface |
||
98 | */ |
||
99 | 5 | protected function buildResource(array $config) |
|
107 | |||
108 | /** |
||
109 | * @param mixed[] $config |
||
110 | * |
||
111 | * @return ColumnInterface[] |
||
112 | */ |
||
113 | 4 | View Code Duplication | protected function buildColumns(array $config) |
124 | |||
125 | /** |
||
126 | * @param mixed[] $config |
||
127 | * @param mixed[] $parentConfig |
||
128 | * |
||
129 | * @return ColumnInterface |
||
130 | */ |
||
131 | 2 | protected function buildColumn(array $config, array $parentConfig) |
|
135 | |||
136 | /** |
||
137 | * @param mixed[] $config |
||
138 | * |
||
139 | * @return FilterInterface[] |
||
140 | */ |
||
141 | 4 | View Code Duplication | protected function buildFilters(array $config) |
152 | |||
153 | /** |
||
154 | * @param mixed[] $config |
||
155 | * @param mixed[] $parentConfig |
||
156 | * |
||
157 | * @return FilterInterface |
||
158 | */ |
||
159 | 2 | protected function buildFilter(array $config, array $parentConfig) |
|
163 | |||
164 | /** |
||
165 | * @param mixed[] $config |
||
166 | * |
||
167 | * @return SortInterface[] |
||
168 | */ |
||
169 | 4 | View Code Duplication | protected function buildSorts(array $config) |
180 | |||
181 | /** |
||
182 | * @param mixed[] $config |
||
183 | * @param mixed[] $parentConfig |
||
184 | * |
||
185 | * @return SortInterface |
||
186 | */ |
||
187 | 2 | protected function buildSort(array $config, array $parentConfig) |
|
191 | |||
192 | /** |
||
193 | * @param mixed[] $config |
||
194 | * |
||
195 | * @return ActionInterface[] |
||
196 | */ |
||
197 | 4 | View Code Duplication | protected function buildGlobalActions(array $config) |
208 | |||
209 | /** |
||
210 | * @param mixed[] $config |
||
211 | * @param mixed[] $parentConfig |
||
212 | * |
||
213 | * @return ActionInterface |
||
214 | */ |
||
215 | 2 | protected function buildGlobalAction(array $config, array $parentConfig) |
|
219 | |||
220 | /** |
||
221 | * @param mixed[] $config |
||
222 | * |
||
223 | * @return ActionInterface[] |
||
224 | */ |
||
225 | 4 | View Code Duplication | protected function buildColumnActions(array $config) |
236 | |||
237 | /** |
||
238 | * @param mixed[] $config |
||
239 | * @param mixed[] $parentConfig |
||
240 | * |
||
241 | * @return ActionInterface |
||
242 | */ |
||
243 | 2 | protected function buildColumnAction(array $config, array $parentConfig) |
|
247 | |||
248 | /** |
||
249 | * @param mixed[] $config |
||
250 | * |
||
251 | * @return BatchInterface[] |
||
252 | */ |
||
253 | 4 | View Code Duplication | protected function buildBatches(array $config) |
264 | |||
265 | /** |
||
266 | * @param mixed[] $config |
||
267 | * @param mixed[] $parentConfig |
||
268 | * |
||
269 | * @return BatchInterface |
||
270 | */ |
||
271 | 2 | protected function buildBatch(array $config, array $parentConfig) |
|
275 | |||
276 | /** |
||
277 | * @param mixed[] $config |
||
278 | * |
||
279 | * @return mixed[] |
||
280 | */ |
||
281 | 4 | protected function buildData(array $config) |
|
285 | |||
286 | /** |
||
287 | * @param mixed[] $config |
||
288 | * |
||
289 | * @return mixed[] |
||
290 | */ |
||
291 | 4 | protected function buildOptions(array $config) |
|
295 | |||
296 | /** |
||
297 | * @param mixed[] $config |
||
298 | * @param mixed[] $parentConfig |
||
299 | * |
||
300 | * @return mixed[] |
||
301 | */ |
||
302 | 5 | protected function prepareConfig(array $config, array $parentConfig = []) |
|
306 | } |
||
307 |
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.