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