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 Scriptaculous 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 Scriptaculous, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | View Code Duplication | class Scriptaculous extends Prototype |
|
|
|||
17 | { |
||
18 | public $TOGGLE_EFFECTS = ['toggle_appear', 'toggle_slide', 'toggle_blind']; |
||
19 | |||
20 | /** |
||
21 | * Scriptaculous constructor. |
||
22 | */ |
||
23 | public function __construct() |
||
26 | |||
27 | /** |
||
28 | * @param $element_id |
||
29 | * @param null $options |
||
30 | * @return string |
||
31 | */ |
||
32 | public function dragable_element($element_id, $options = null) |
||
36 | |||
37 | /** |
||
38 | * @param $element_id |
||
39 | * @param null $options |
||
40 | * @return string |
||
41 | */ |
||
42 | public function drop_receiving_element($element_id, $options = null) |
||
46 | |||
47 | /** |
||
48 | * @param $name |
||
49 | * @param bool $element_id |
||
50 | * @param null $js_options |
||
51 | * @return string |
||
52 | */ |
||
53 | public function visual_effect($name, $element_id = false, $js_options = null) |
||
69 | |||
70 | /** |
||
71 | * @param $element_id |
||
72 | * @param null $options |
||
73 | * @return string |
||
74 | */ |
||
75 | public function sortabe_element($element_id, $options = null) |
||
79 | |||
80 | ///////////////////////////////////////////////////////////////////////////////////// |
||
81 | // Private functions |
||
82 | ///////////////////////////////////////////////////////////////////////////////////// |
||
83 | |||
84 | /** |
||
85 | * @param $element_id |
||
86 | * @param $options |
||
87 | * @return string |
||
88 | */ |
||
89 | |||
90 | public function _sortabe_element($element_id, $options) |
||
126 | |||
127 | /** |
||
128 | * @param $element_id |
||
129 | * @param $options |
||
130 | * @return string |
||
131 | */ |
||
132 | public function _dragable_element_js($element_id, $options) |
||
136 | |||
137 | /** |
||
138 | * @param $element_id |
||
139 | * @param $options |
||
140 | * @return string |
||
141 | */ |
||
142 | public function _drop_receiving_element($element_id, $options) |
||
173 | |||
174 | ///////////////////////////////////////////////////////////////////////////////////// |
||
175 | // Merged Javascript macro |
||
176 | ///////////////////////////////////////////////////////////////////////////////////// |
||
177 | |||
178 | /** |
||
179 | * @param $field_id |
||
180 | * @param $options |
||
181 | * @param bool $tag |
||
182 | * @return string |
||
183 | */ |
||
184 | public function in_place_editor($field_id, $options, $tag = true) |
||
232 | |||
233 | /** |
||
234 | * @param $object |
||
235 | * @param null $tag_options |
||
236 | * @param null $in_place_editor_options |
||
237 | * @return string |
||
238 | */ |
||
239 | public function in_place_editor_field($object, $tag_options = null, $in_place_editor_options = null) |
||
247 | |||
248 | /** |
||
249 | * @param $field_id |
||
250 | * @param $options |
||
251 | * @return mixed |
||
252 | */ |
||
253 | public function auto_complete_field($field_id, $options) |
||
284 | |||
285 | /** |
||
286 | * @param $entries |
||
287 | * @param $field |
||
288 | * @param null $phrase |
||
289 | */ |
||
290 | public function auto_complete_results($entries, $field, $phrase = null) |
||
298 | |||
299 | /** |
||
300 | * @param $object |
||
301 | * @param null $tag_options |
||
302 | * @param null $completion_options |
||
303 | * @return string |
||
304 | */ |
||
305 | public function text_field_with_auto_complete($object, $tag_options = null, $completion_options = null) |
||
327 | |||
328 | /** |
||
329 | * @return string |
||
330 | */ |
||
331 | public function _auto_complete_stylesheet() |
||
358 | } |
||
359 |
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.