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 JsUtilsActionsTrait 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 JsUtilsActionsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait JsUtilsActionsTrait { |
||
13 | |||
14 | abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true); |
||
15 | /** |
||
16 | * show or hide with effect |
||
17 | * |
||
18 | * @param string $action |
||
19 | * @param string - element |
||
20 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
21 | * @param string - Javascript callback function |
||
22 | * @param boolean $immediatly defers the execution if set to false |
||
23 | * @return string |
||
24 | */ |
||
25 | protected function _showHideWithEffect($action,$element='this', $speed='', $callback='', $immediatly=false) { |
||
36 | /** |
||
37 | * Ensures the speed parameter is valid for jQuery |
||
38 | * @param string|int $speed |
||
39 | * @return string |
||
40 | */ |
||
41 | private function _validate_speed($speed) { |
||
52 | |||
53 | /** |
||
54 | * Execute a generic jQuery call with a value. |
||
55 | * @param string $jQueryCall |
||
56 | * @param string $element |
||
57 | * @param string $param |
||
58 | * @param boolean $immediatly delayed if false |
||
59 | */ |
||
60 | View Code Duplication | public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) { |
|
71 | |||
72 | /** |
||
73 | * Execute a generic jQuery call with 2 elements. |
||
74 | * @param string $jQueryCall |
||
75 | * @param string $to |
||
76 | * @param string $element |
||
77 | * @param boolean $immediatly delayed if false |
||
78 | * @return string |
||
79 | */ |
||
80 | public function _genericCallElement($jQueryCall,$to='this', $element, $immediatly=false) { |
||
88 | /** |
||
89 | * add class to element |
||
90 | * |
||
91 | * @param string $element |
||
92 | * @param string $class to add |
||
93 | * @param boolean $immediatly defers the execution if set to false |
||
94 | * @return string |
||
95 | */ |
||
96 | public function addClass($element='this', $class='', $immediatly=false) { |
||
99 | |||
100 | /** |
||
101 | * Insert content, specified by the parameter, after each element in the set of matched elements |
||
102 | * @param string $to |
||
103 | * @param string $element |
||
104 | * @param boolean $immediatly defers the execution if set to false |
||
105 | * @return string |
||
106 | */ |
||
107 | public function after($to, $element, $immediatly=false){ |
||
110 | |||
111 | /** |
||
112 | * Insert content, specified by the parameter, before each element in the set of matched elements |
||
113 | * @param string $to |
||
114 | * @param string $element |
||
115 | * @param boolean $immediatly defers the execution if set to false |
||
116 | * @return string |
||
117 | */ |
||
118 | public function before($to, $element, $immediatly=false){ |
||
121 | |||
122 | /** |
||
123 | * 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. |
||
124 | * @param string $element |
||
125 | * @param string $attributeName |
||
126 | * @param string $value |
||
127 | * @param boolean $immediatly delayed if false |
||
128 | */ |
||
129 | View Code Duplication | public function attr($element='this', $attributeName, $value="", $immediatly=false) { |
|
140 | |||
141 | /** |
||
142 | * Get or set the value of the first element in the set of matched elements or set one or more attributes for every matched element. |
||
143 | * @param string $element |
||
144 | * @param string $value |
||
145 | * @param boolean $immediatly defers the execution if set to false |
||
146 | */ |
||
147 | public function val($element='this',$value='',$immediatly=false){ |
||
150 | |||
151 | /** |
||
152 | * Get or set the html of an attribute for the first element in the set of matched elements. |
||
153 | * @param string $element |
||
154 | * @param string $value |
||
155 | * @param boolean $immediatly defers the execution if set to false |
||
156 | */ |
||
157 | public function html($element='this', $value='', $immediatly=false) { |
||
160 | |||
161 | /** |
||
162 | * Outputs a javascript library animate event |
||
163 | * |
||
164 | * @param string $element element |
||
165 | * @param array $params |
||
166 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
167 | * @param string $extra |
||
168 | * @param boolean $immediatly defers the execution if set to false |
||
169 | * @return string |
||
170 | */ |
||
171 | public function animate($element='this', $params=array(), $speed='', $extra='', $immediatly=false) { |
||
197 | |||
198 | /** |
||
199 | * Insert content, specified by the parameter $element, to the end of each element in the set of matched elements $to. |
||
200 | * @param string $to |
||
201 | * @param string $element |
||
202 | * @param boolean $immediatly defers the execution if set to false |
||
203 | * @return string |
||
204 | */ |
||
205 | public function append($to, $element, $immediatly=false) { |
||
208 | |||
209 | /** |
||
210 | * Insert content, specified by the parameter $element, to the beginning of each element in the set of matched elements $to. |
||
211 | * @param string $to |
||
212 | * @param string $element |
||
213 | * @param boolean $immediatly defers the execution if set to false |
||
214 | * @return string |
||
215 | */ |
||
216 | public function prepend($to, $element, $immediatly=false) { |
||
219 | |||
220 | /** |
||
221 | * Execute a javascript library hide action |
||
222 | * |
||
223 | * @param string - element |
||
224 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
225 | * @param string - Javascript callback function |
||
226 | * @param boolean $immediatly defers the execution if set to false |
||
227 | * @return string |
||
228 | */ |
||
229 | public function fadeIn($element='this', $speed='', $callback='', $immediatly=false) { |
||
232 | |||
233 | /** |
||
234 | * Execute a javascript library hide action |
||
235 | * |
||
236 | * @param string - element |
||
237 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
238 | * @param string - Javascript callback function |
||
239 | * @param boolean $immediatly defers the execution if set to false |
||
240 | * @return string |
||
241 | */ |
||
242 | public function fadeOut($element='this', $speed='', $callback='', $immediatly=false) { |
||
245 | |||
246 | /** |
||
247 | * Execute a javascript library slideUp action |
||
248 | * |
||
249 | * @param string - element |
||
250 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
251 | * @param string - Javascript callback function |
||
252 | * @param boolean $immediatly defers the execution if set to false |
||
253 | * @return string |
||
254 | */ |
||
255 | public function slideUp($element='this', $speed='', $callback='', $immediatly=false) { |
||
258 | |||
259 | /** |
||
260 | * Execute a javascript library removeClass action |
||
261 | * |
||
262 | * @param string - element |
||
263 | * @param string - Class to add |
||
264 | * @param boolean $immediatly defers the execution if set to false |
||
265 | * @return string |
||
266 | */ |
||
267 | public function removeClass($element='this', $class='', $immediatly=false) { |
||
270 | |||
271 | /** |
||
272 | * Execute a javascript library slideDown action |
||
273 | * |
||
274 | * @param string - element |
||
275 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
276 | * @param string - Javascript callback function |
||
277 | * @param boolean $immediatly defers the execution if set to false |
||
278 | * @return string |
||
279 | */ |
||
280 | public function slideDown($element='this', $speed='', $callback='', $immediatly=false) { |
||
283 | |||
284 | /** |
||
285 | * Execute a javascript library slideToggle action |
||
286 | * |
||
287 | * @param string - element |
||
288 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
289 | * @param string - Javascript callback function |
||
290 | * @param boolean $immediatly defers the execution if set to false |
||
291 | * @return string |
||
292 | */ |
||
293 | public function slideToggle($element='this', $speed='', $callback='', $immediatly=false) { |
||
296 | |||
297 | /** |
||
298 | * Execute a javascript library hide action |
||
299 | * |
||
300 | * @param string - element |
||
301 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
302 | * @param string - Javascript callback function |
||
303 | * @param boolean $immediatly defers the execution if set to false |
||
304 | * @return string |
||
305 | */ |
||
306 | public function hide($element='this', $speed='', $callback='', $immediatly=false) { |
||
309 | |||
310 | /** |
||
311 | * Execute a javascript library toggle action |
||
312 | * |
||
313 | * @param string - element |
||
314 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
315 | * @param string - Javascript callback function |
||
316 | * @param boolean $immediatly defers the execution if set to false |
||
317 | * @return string |
||
318 | */ |
||
319 | public function toggle($element='this', $speed='', $callback='', $immediatly=false) { |
||
322 | |||
323 | /** |
||
324 | * Execute a javascript library toggle class action |
||
325 | * |
||
326 | * @param string - element |
||
327 | * @param boolean $immediatly defers the execution if set to false |
||
328 | * @return string |
||
329 | */ |
||
330 | public function toggleClass($element='this', $class='', $immediatly=false) { |
||
333 | |||
334 | /** |
||
335 | * Execute all handlers and behaviors attached to the matched elements for the given event. |
||
336 | * @param string $element |
||
337 | * @param string $event |
||
338 | * @param boolean $immediatly defers the execution if set to false |
||
339 | */ |
||
340 | public function trigger($element='this', $event='click', $immediatly=false) { |
||
348 | |||
349 | /** |
||
350 | * Execute a javascript library show action |
||
351 | * |
||
352 | * @param string - element |
||
353 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
354 | * @param string - Javascript callback function |
||
355 | * @param boolean $immediatly defers the execution if set to false |
||
356 | * @return string |
||
357 | */ |
||
358 | public function show($element='this', $speed='', $callback='', $immediatly=false) { |
||
361 | |||
362 | /** |
||
363 | * Creates a jQuery sortable |
||
364 | * |
||
365 | * @param string $element |
||
366 | * @param array $options |
||
367 | * @return void |
||
368 | */ |
||
369 | public function sortable($element, $options=array()) { |
||
382 | |||
383 | /** |
||
384 | * Table Sorter Plugin |
||
385 | * |
||
386 | * @param string $table table name |
||
387 | * @param string $options plugin location |
||
388 | * @return string |
||
389 | */ |
||
390 | public function tablesorter($table='', $options='') { |
||
393 | |||
394 | /** |
||
395 | * Allows to attach a condition |
||
396 | * @param string $condition |
||
397 | * @param string $jsCodeIfTrue |
||
398 | * @param string $jsCodeIfFalse |
||
399 | * @param boolean $immediatly defers the execution if set to false |
||
400 | */ |
||
401 | public function condition($condition, $jsCodeIfTrue, $jsCodeIfFalse=null, $immediatly=false) { |
||
411 | |||
412 | /** |
||
413 | * Call the JQuery method $jqueryCall on $element with parameters $param |
||
414 | * @param string $element |
||
415 | * @param string $jqueryCall |
||
416 | * @param mixed $param |
||
417 | * @param string $jsCallback javascript code to execute after the jquery call |
||
418 | * @param boolean $immediatly |
||
419 | * @return string |
||
420 | */ |
||
421 | private function _doJQuery($element, $jqueryCall, $param="", $jsCallback="", $immediatly=false) { |
||
431 | |||
432 | /** |
||
433 | * Calls the JQuery callback $someThing on $element with facultative parameter $param |
||
434 | * @param string $element the element |
||
435 | * @param string $jqueryCall the JQuery callback |
||
436 | * @param mixed $param array or string parameters |
||
437 | * @param string $jsCallback javascript code to execute after the jquery call |
||
438 | * @return mixed |
||
439 | */ |
||
440 | public function doJQuery($element, $jqueryCall, $param="", $jsCallback="") { |
||
443 | |||
444 | /** |
||
445 | * Calls the JQuery callback $someThing on $element with facultative parameter $param |
||
446 | * @param string $element the element |
||
447 | * @param string $jqueryCall the JQuery callback |
||
448 | * @param mixed $param array or string parameters |
||
449 | * @param string $jsCallback javascript code to execute after the jquery call |
||
450 | * @return mixed |
||
451 | */ |
||
452 | public function doJQueryDeferred($element, $jqueryCall, $param="", $jsCallback="") { |
||
455 | |||
456 | /** |
||
457 | * |
||
458 | * @param string $event |
||
459 | * @param string $element |
||
460 | * @param string $elementToModify |
||
461 | * @param string $jqueryCall |
||
462 | * @param string|array $param |
||
463 | * @param boolean $preventDefault |
||
464 | * @param boolean $stopPropagation |
||
465 | * @param string $jsCallback javascript code to execute after the jquery call |
||
466 | * @param boolean $immediatly |
||
467 | * @return string |
||
468 | */ |
||
469 | private function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $preventDefault=false, $stopPropagation=false, $jsCallback="",$immediatly=true) { |
||
472 | |||
473 | /** |
||
474 | * Calls the JQuery callback $jqueryCall on $element with facultative parameter $param in response to an event $event |
||
475 | * @param string $event |
||
476 | * @param string $element |
||
477 | * @param string $elementToModify |
||
478 | * @param string $jqueryCall |
||
479 | * @param string $param |
||
480 | * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"jsCallback"=>'',"immediatly"=>true) |
||
481 | */ |
||
482 | public function doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $parameters=array()) { |
||
490 | |||
491 | /** |
||
492 | * Executes the code $js |
||
493 | * @param string $js Code to execute |
||
494 | * @param boolean $immediatly delayed if false |
||
495 | * @return String |
||
496 | */ |
||
497 | public function exec($js, $immediatly=false) { |
||
503 | |||
504 | /** |
||
505 | * Executes the code $js |
||
506 | * @param string $js Code to execute |
||
507 | * @param boolean $immediatly delayed if false |
||
508 | * @return String |
||
509 | */ |
||
510 | public function execAtLast($js) { |
||
515 | |||
516 | /** |
||
517 | * Executes the javascript code $js when $event fires on $element |
||
518 | * @param string $event |
||
519 | * @param string $element |
||
520 | * @param string $js Code to execute |
||
521 | * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true) |
||
522 | * @return String |
||
523 | */ |
||
524 | public function execOn($event, $element, $js, $parameters=array()) { |
||
532 | } |
||
533 |
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.