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 Jquery 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 Jquery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Jquery { |
||
| 20 | use JqueryEventsTrait,JqueryAjaxTrait,JqueryActionsTrait; |
||
| 21 | protected $_di; |
||
| 22 | protected $_ui; |
||
| 23 | protected $_bootstrap; |
||
| 24 | protected $_semantic; |
||
| 25 | protected $libraryFile; |
||
| 26 | protected $_javascript_folder='js'; |
||
| 27 | protected $jquery_code_for_load=array (); |
||
| 28 | protected $jquery_code_for_compile=array (); |
||
| 29 | protected $jquery_corner_active=FALSE; |
||
| 30 | protected $jquery_table_sorter_active=FALSE; |
||
| 31 | protected $jquery_table_sorter_pager_active=FALSE; |
||
| 32 | protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>'; |
||
| 33 | protected $jquery_events=array ( |
||
| 34 | "bind","blur","change","click","dblclick","delegate","die","error","focus","focusin","focusout","hover","keydown","keypress","keyup","live","load","mousedown","mousseenter","mouseleave","mousemove","mouseout","mouseover","mouseup","off","on","one","ready","resize","scroll","select","submit","toggle","trigger","triggerHandler","undind","undelegate","unload" |
||
| 35 | ); |
||
| 36 | |||
| 37 | public function setDi($di) { |
||
| 40 | |||
| 41 | public function ui($ui=NULL) { |
||
| 47 | |||
| 48 | public function bootstrap($bootstrap=NULL) { |
||
| 54 | |||
| 55 | public function semantic($semantic=NULL) { |
||
| 61 | |||
| 62 | public function __construct($params) { |
||
| 68 | |||
| 69 | // -------------------------------------------------------------------- |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Inline |
||
| 73 | * |
||
| 74 | * Outputs a <script> tag |
||
| 75 | * |
||
| 76 | * @access public |
||
| 77 | * @param string $script |
||
| 78 | * @param boolean $cdata a CDATA section should be added |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function inline($script, $cdata=TRUE) { |
|
| 88 | |||
| 89 | // -------------------------------------------------------------------- |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Open Script |
||
| 93 | * |
||
| 94 | * Outputs an opening <script> |
||
| 95 | * |
||
| 96 | * @access private |
||
| 97 | * @param string $src |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | private function _open_script($src='') { |
||
| 105 | |||
| 106 | // -------------------------------------------------------------------- |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Close Script |
||
| 110 | * |
||
| 111 | * Outputs an closing </script> |
||
| 112 | * |
||
| 113 | * @param string |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | private function _close_script($extra="\n") { |
||
| 119 | |||
| 120 | public function getLibraryScript() { |
||
| 125 | |||
| 126 | public function setLibraryFile($name) { |
||
| 129 | |||
| 130 | public function _setAjaxLoader($loader) { |
||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | // -------------------------------------------------------------------- |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Outputs script directly |
||
| 140 | * |
||
| 141 | * @param string The element to attach the event to |
||
| 142 | * @param string The code to execute |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function _output($array_js='') { |
||
| 156 | |||
| 157 | // -------------------------------------------------------------------- |
||
| 158 | // Effects |
||
| 159 | // -------------------------------------------------------------------- |
||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * Execute a generic jQuery call with a value. |
||
| 166 | * @param string $jQueryCall |
||
| 167 | * @param string $element |
||
| 168 | * @param string $param |
||
| 169 | * @param boolean $immediatly delayed if false |
||
| 170 | */ |
||
| 171 | View Code Duplication | public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) { |
|
| 182 | /** |
||
| 183 | * Execute a generic jQuery call with 2 elements. |
||
| 184 | * @param string $jQueryCall |
||
| 185 | * @param string $to |
||
| 186 | * @param string $element |
||
| 187 | * @param boolean $immediatly delayed if false |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function _genericCallElement($jQueryCall,$to='this', $element, $immediatly=false) { |
||
| 198 | // -------------------------------------------------------------------- |
||
| 199 | |||
| 200 | |||
| 201 | // -------------------------------------------------------------------- |
||
| 202 | // Plugins |
||
| 203 | // -------------------------------------------------------------------- |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Creates a jQuery sortable |
||
| 207 | * |
||
| 208 | * @param string $element |
||
| 209 | * @param array $options |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | public function sortable($element, $options=array()) { |
||
| 225 | |||
| 226 | // -------------------------------------------------------------------- |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Table Sorter Plugin |
||
| 230 | * |
||
| 231 | * @param string $table table name |
||
| 232 | * @param string $options plugin location |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function tablesorter($table='', $options='') { |
||
| 238 | |||
| 239 | // -------------------------------------------------------------------- |
||
| 240 | // Class functions |
||
| 241 | // -------------------------------------------------------------------- |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Constructs the syntax for an event, and adds to into the array for compilation |
||
| 245 | * |
||
| 246 | * @param string $element The element to attach the event to |
||
| 247 | * @param string $js The code to execute |
||
| 248 | * @param string $event The event to pass |
||
| 249 | * @param boolean $preventDefault If set to true, the default action of the event will not be triggered. |
||
| 250 | * @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true) { |
||
| 271 | |||
| 272 | // -------------------------------------------------------------------- |
||
| 273 | |||
| 274 | /** |
||
| 275 | * As events are specified, they are stored in an array |
||
| 276 | * This function compiles them all for output on a page |
||
| 277 | * @param view $view |
||
| 278 | * @param string $view_var |
||
| 279 | * @param boolean $script_tags |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function _compile($view=NULL, $view_var='script_foot', $script_tags=TRUE) { |
||
| 333 | |||
| 334 | public function _addToCompile($jsScript) { |
||
| 337 | |||
| 338 | // -------------------------------------------------------------------- |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Clears the array of script events collected for output |
||
| 342 | * |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | public function _clear_compile() { |
||
| 348 | |||
| 349 | // -------------------------------------------------------------------- |
||
| 350 | |||
| 351 | /** |
||
| 352 | * A wrapper for writing document.ready() |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function _document_ready($js) { |
||
| 367 | |||
| 368 | // -------------------------------------------------------------------- |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Puts HTML element in quotes for use in jQuery code |
||
| 372 | * unless the supplied element is the Javascript 'this' |
||
| 373 | * object, in which case no quotes are added |
||
| 374 | * |
||
| 375 | * @param string $element |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function _prep_element($element) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Puts HTML values in quotes for use in jQuery code |
||
| 387 | * unless the supplied value contains the Javascript 'this' or 'event' |
||
| 388 | * object, in which case no quotes are added |
||
| 389 | * |
||
| 390 | * @param string $value |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | public function _prep_value($value) { |
||
| 402 | |||
| 403 | // -------------------------------------------------------------------- |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Ensures the speed parameter is valid for jQuery |
||
| 407 | * |
||
| 408 | * @param string|int $speed |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | private function _validate_speed($speed) { |
||
| 422 | |||
| 423 | private function minify($input) { |
||
| 447 | } |
||
| 448 | /* End of file Jquery.php */ |
||
| 449 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: