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 Action 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 Action, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Action |
||
13 | { |
||
14 | protected static $panelButtons = []; |
||
15 | |||
16 | protected static $iblockElementArray = []; |
||
17 | |||
18 | protected static $iblockSectionArray = []; |
||
19 | |||
20 | protected static $hlblockIdsByTableName= []; |
||
21 | |||
22 | /** |
||
23 | * Get edit area id for specific type |
||
24 | * |
||
25 | * @param CBitrixComponentTemplate $template |
||
26 | * @param $type |
||
27 | * @param $element |
||
28 | * @return string |
||
29 | */ |
||
30 | public static function getEditArea($template, $type, $element) |
||
35 | |||
36 | /** |
||
37 | * @param CBitrixComponentTemplate $template |
||
38 | * @param $element |
||
39 | */ |
||
40 | View Code Duplication | public static function editIBlockElement($template, $element) |
|
58 | |||
59 | /** |
||
60 | * @param CBitrixComponentTemplate $template |
||
61 | * @param $element |
||
62 | * @param string $confirm |
||
63 | */ |
||
64 | View Code Duplication | public static function deleteIBlockElement($template, $element, $confirm = 'Вы уверены, что хотите удалить элемент?') |
|
83 | |||
84 | /** |
||
85 | * @param CBitrixComponentTemplate $template |
||
86 | * @param $element |
||
87 | */ |
||
88 | public static function editAndDeleteIBlockElement($template, $element) |
||
93 | |||
94 | /** |
||
95 | * @param CBitrixComponentTemplate $template |
||
96 | * @param $element |
||
97 | * @return string |
||
98 | */ |
||
99 | public static function areaForIBlockElement($template, $element) |
||
103 | |||
104 | /** |
||
105 | * @param CBitrixComponentTemplate $template |
||
106 | * @param $section |
||
107 | */ |
||
108 | View Code Duplication | public static function editIBlockSection($template, $section) |
|
127 | |||
128 | /** |
||
129 | * @param CBitrixComponentTemplate $template |
||
130 | * @param $section |
||
131 | * @param string $confirm |
||
132 | */ |
||
133 | View Code Duplication | public static function deleteIBlockSection($template, $section, $confirm = 'Вы уверены, что хотите удалить раздел?') |
|
152 | |||
153 | /** |
||
154 | * @param CBitrixComponentTemplate $template |
||
155 | * @param $section |
||
156 | */ |
||
157 | public static function editAndDeleteIBlockSection($template, $section) |
||
162 | |||
163 | /** |
||
164 | * @param CBitrixComponentTemplate $template |
||
165 | * @param $section |
||
166 | * @return string |
||
167 | */ |
||
168 | public static function areaForIBlockSection($template, $section) |
||
172 | |||
173 | /** |
||
174 | * @param CBitrixComponentTemplate $template |
||
175 | * @param $element |
||
176 | * @param string $label |
||
177 | */ |
||
178 | View Code Duplication | public static function editHLBlockElement($template, $element, $label = 'Изменить элемент') |
|
197 | |||
198 | /** |
||
199 | * @param CBitrixComponentTemplate $template |
||
200 | * @param $element |
||
201 | * @param string $label |
||
202 | * @param string $confirm |
||
203 | */ |
||
204 | View Code Duplication | public static function deleteHLBlockElement($template, $element, $label = 'Удалить элемент', $confirm = 'Вы уверены, что хотите удалить элемент?') |
|
223 | |||
224 | /** |
||
225 | * @param CBitrixComponentTemplate $template |
||
226 | * @param $element |
||
227 | */ |
||
228 | public static function editAndDeleteHLBlockElement($template, $element) |
||
233 | |||
234 | /** |
||
235 | * @param CBitrixComponentTemplate $template |
||
236 | * @param $element |
||
237 | * @return string |
||
238 | */ |
||
239 | public static function areaForHLBlockElement($template, $element) |
||
243 | |||
244 | /** |
||
245 | * @param CBitrixComponent|CBitrixComponentTemplate $componentOrTemplate |
||
246 | * @param $iblockId |
||
247 | * @param array $options |
||
248 | */ |
||
249 | public static function addForIBlock($componentOrTemplate, $iblockId, $options = []) |
||
264 | |||
265 | /** |
||
266 | * @param $element |
||
267 | * @return array |
||
268 | */ |
||
269 | View Code Duplication | protected static function getIBlockElementPanelButtons($element) |
|
282 | |||
283 | /** |
||
284 | * @param $section |
||
285 | * @return array |
||
286 | */ |
||
287 | View Code Duplication | protected static function getIBlockSectionPanelButtons($section) |
|
300 | |||
301 | /** |
||
302 | * @param int $id |
||
303 | * @return array |
||
304 | */ |
||
305 | View Code Duplication | protected static function prepareIBlockElementArrayById($id) |
|
320 | |||
321 | /** |
||
322 | * @param int $id |
||
323 | * @return array |
||
324 | */ |
||
325 | View Code Duplication | protected static function prepareIBlockSectionArrayById($id) |
|
340 | |||
341 | /** |
||
342 | * @param string $tableName |
||
343 | * @return int |
||
344 | */ |
||
345 | protected static function prepareHLBlockIdByTableName($tableName) |
||
360 | } |
||
361 |
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.