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 JqueryActionsTrait 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 JqueryActionsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | trait JqueryActionsTrait { |
||
6 | |||
7 | /** |
||
8 | * Get or set the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element. |
||
9 | * @param string $element |
||
10 | * @param string $attributeName |
||
11 | * @param string $value |
||
12 | * @param boolean $immediatly delayed if false |
||
13 | */ |
||
14 | View Code Duplication | public function _attr($element='this', $attributeName, $value="", $immediatly=false) { |
|
25 | |||
26 | /** |
||
27 | * Insert content, specified by the parameter, after each element in the set of matched elements |
||
28 | * @param string $element |
||
29 | * @param string $value |
||
30 | * @param boolean $immediatly defers the execution if set to false |
||
31 | * @return string |
||
32 | */ |
||
33 | public function after($element='this', $value='', $immediatly=false){ |
||
41 | |||
42 | /** |
||
43 | * Execute a jQuery animate action |
||
44 | * |
||
45 | * @param string $element element |
||
46 | * @param string|array $params One of 'slow', 'normal', 'fast', or time in milliseconds |
||
47 | * @param string $speed |
||
48 | * @param string $extra |
||
49 | * @param boolean $immediatly delayed if false |
||
50 | * @return string |
||
51 | */ |
||
52 | public function _animate($element='this', $params=array(), $speed='', $extra='', $immediatly=false) { |
||
78 | |||
79 | // -------------------------------------------------------------------- |
||
80 | |||
81 | /** |
||
82 | * Execute a jQuery hide action |
||
83 | * |
||
84 | * @param string $element element |
||
85 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
86 | * @param string $callback Javascript callback function |
||
87 | * @param boolean $immediatly delayed if false |
||
88 | * @return string |
||
89 | */ |
||
90 | View Code Duplication | public function _fadeIn($element='this', $speed='', $callback='', $immediatly=false) { |
|
104 | |||
105 | // -------------------------------------------------------------------- |
||
106 | |||
107 | /** |
||
108 | * Execute a jQuery fadeOut action |
||
109 | * |
||
110 | * @param string $element element |
||
111 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
112 | * @param string $callback Javascript callback function |
||
113 | * @param boolean $immediatly delayed if false |
||
114 | * @return string |
||
115 | */ |
||
116 | View Code Duplication | public function _fadeOut($element='this', $speed='', $callback='', $immediatly=false) { |
|
130 | |||
131 | // -------------------------------------------------------------------- |
||
132 | |||
133 | /** |
||
134 | * Execute a jQuery hide action |
||
135 | * |
||
136 | * @param string $element element |
||
137 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
138 | * @param string $callback Javascript callback function |
||
139 | * @param boolean $immediatly delayed if false |
||
140 | * @return string |
||
141 | */ |
||
142 | View Code Duplication | public function _hide($element='this', $speed='', $callback='', $immediatly=false) { |
|
156 | |||
157 | // -------------------------------------------------------------------- |
||
158 | |||
159 | // -------------------------------------------------------------------- |
||
160 | |||
161 | /** |
||
162 | * Execute a jQuery slideUp action |
||
163 | * |
||
164 | * @param string $element element |
||
165 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
166 | * @param string $callback Javascript callback function |
||
167 | * @param boolean $immediatly delayed if false |
||
168 | * @return string |
||
169 | */ |
||
170 | View Code Duplication | public function _slideUp($element='this', $speed='', $callback='', $immediatly=false) { |
|
184 | |||
185 | // -------------------------------------------------------------------- |
||
186 | |||
187 | /** |
||
188 | * Execute a jQuery slideDown action |
||
189 | * |
||
190 | * @param string $element element |
||
191 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
192 | * @param string $callback Javascript callback function |
||
193 | * @param boolean $immediatly delayed if false |
||
194 | * @return string |
||
195 | */ |
||
196 | View Code Duplication | public function _slideDown($element='this', $speed='', $callback='', $immediatly=false) { |
|
210 | |||
211 | // -------------------------------------------------------------------- |
||
212 | |||
213 | /** |
||
214 | * Execute a jQuery slideToggle action |
||
215 | * |
||
216 | * @param string $element element |
||
217 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
218 | * @param string $callback Javascript callback function |
||
219 | * @param boolean $immediatly delayed if false |
||
220 | * @return string |
||
221 | */ |
||
222 | View Code Duplication | public function _slideToggle($element='this', $speed='', $callback='', $immediatly=false) { |
|
236 | |||
237 | // -------------------------------------------------------------------- |
||
238 | |||
239 | /** |
||
240 | * Outputs a jQuery toggle event |
||
241 | * |
||
242 | * @param string $element element |
||
243 | * @param boolean $immediatly delayed if false |
||
244 | * @return string |
||
245 | */ |
||
246 | View Code Duplication | public function _toggle($element='this', $immediatly=false) { |
|
254 | |||
255 | // -------------------------------------------------------------------- |
||
256 | |||
257 | /** |
||
258 | * Execute all handlers and behaviors attached to the matched elements for the given event. |
||
259 | * @param string $element |
||
260 | * @param string $event |
||
261 | * @param boolean $immediatly delayed if false |
||
262 | */ |
||
263 | View Code Duplication | public function _trigger($element='this', $event='click', $immediatly=false) { |
|
271 | |||
272 | // -------------------------------------------------------------------- |
||
273 | |||
274 | /** |
||
275 | * Execute a jQuery show action |
||
276 | * |
||
277 | * @param string $element element |
||
278 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
279 | * @param string $callback Javascript callback function |
||
280 | * @param boolean $immediatly delayed if false |
||
281 | * @return string |
||
282 | */ |
||
283 | View Code Duplication | public function _show($element='this', $speed='', $callback='', $immediatly=false) { |
|
297 | |||
298 | /** |
||
299 | * Places a condition |
||
300 | * @param string $condition |
||
301 | * @param string $jsCodeIfTrue |
||
302 | * @param string $jsCodeIfFalse |
||
303 | * @param boolean $immediatly delayed if false |
||
304 | * @return string |
||
305 | */ |
||
306 | public function _condition($condition, $jsCodeIfTrue, $jsCodeIfFalse=null, $immediatly=false) { |
||
316 | |||
317 | // ------------------------------------------------------------------------ |
||
318 | /** |
||
319 | * Call the JQuery method $jqueryCall on $element with parameters $param |
||
320 | * @param string $element |
||
321 | * @param string $jqueryCall |
||
322 | * @param mixed $param |
||
323 | * @param string $jsCallback javascript code to execute after the jquery call |
||
324 | * @param boolean $immediatly |
||
325 | * @return string |
||
326 | */ |
||
327 | public function _doJQuery($element, $jqueryCall, $param="", $jsCallback="", $immediatly=false) { |
||
337 | |||
338 | /** |
||
339 | * |
||
340 | * @param string $event |
||
341 | * @param string $element |
||
342 | * @param string $elementToModify |
||
343 | * @param string $jqueryCall |
||
344 | * @param string|array $param |
||
345 | * @param boolean $preventDefault |
||
346 | * @param boolean $stopPropagation |
||
347 | * @param string $jsCallback javascript code to execute after the jquery call |
||
348 | * @param boolean $immediatly |
||
349 | * @return string |
||
350 | */ |
||
351 | public function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $preventDefault=false, $stopPropagation=false, $jsCallback="",$immediatly=true) { |
||
354 | |||
355 | /** |
||
356 | * Execute the code $js |
||
357 | * @param string $js Code to execute |
||
358 | * @param boolean $immediatly diffère l'exécution si false |
||
359 | * @return String |
||
360 | */ |
||
361 | public function _exec($js, $immediatly=false) { |
||
367 | |||
368 | /** |
||
369 | * |
||
370 | * @param string $element |
||
371 | * @param string $event |
||
372 | * @param string $js Code to execute |
||
373 | * @param boolean $preventDefault |
||
374 | * @param boolean $stopPropagation |
||
375 | * @param boolean $immediatly |
||
376 | * @return String |
||
377 | */ |
||
378 | public function _execOn($element, $event, $js, $preventDefault=false, $stopPropagation=false,$immediatly=true) { |
||
381 | } |
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.