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 $_ui; |
||
22 | protected $_bootstrap; |
||
23 | protected $_semantic; |
||
24 | protected $libraryFile; |
||
25 | protected $_javascript_folder='js'; |
||
26 | protected $jquery_code_for_load=array (); |
||
27 | protected $jquery_code_for_compile=array (); |
||
28 | protected $jquery_corner_active=FALSE; |
||
29 | protected $jquery_table_sorter_active=FALSE; |
||
30 | protected $jquery_table_sorter_pager_active=FALSE; |
||
31 | protected $jsUtils; |
||
32 | |||
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 ui($ui=NULL) { |
||
43 | |||
44 | public function bootstrap($bootstrap=NULL) { |
||
50 | |||
51 | public function semantic($semantic=NULL) { |
||
57 | |||
58 | public function __construct($params,$jsUtils) { |
||
65 | |||
66 | /** |
||
67 | * Inline |
||
68 | * |
||
69 | * Outputs a <script> tag |
||
70 | * |
||
71 | * @access public |
||
72 | * @param string $script |
||
73 | * @param boolean $cdata a CDATA section should be added |
||
74 | * @return string |
||
75 | */ |
||
76 | View Code Duplication | public function inline($script, $cdata=TRUE) { |
|
83 | |||
84 | /** |
||
85 | * Open Script |
||
86 | * |
||
87 | * Outputs an opening <script> |
||
88 | * |
||
89 | * @access private |
||
90 | * @param string $src |
||
91 | * @return string |
||
92 | */ |
||
93 | private function _open_script($src='') { |
||
98 | |||
99 | /** |
||
100 | * Close Script |
||
101 | * |
||
102 | * Outputs an closing </script> |
||
103 | * |
||
104 | * @param string |
||
105 | * @return string |
||
106 | */ |
||
107 | private function _close_script($extra="\n") { |
||
110 | |||
111 | public function setLibraryFile($name) { |
||
114 | |||
115 | public function _setAjaxLoader($loader) { |
||
118 | |||
119 | /** |
||
120 | * Outputs script directly |
||
121 | * |
||
122 | * @param string The element to attach the event to |
||
123 | * @param string The code to execute |
||
124 | * @return string |
||
125 | */ |
||
126 | public function _output($array_js='') { |
||
137 | |||
138 | /** |
||
139 | * Execute a generic jQuery call with a value. |
||
140 | * @param string $jQueryCall |
||
141 | * @param string $element |
||
142 | * @param string $param |
||
143 | * @param boolean $immediatly delayed if false |
||
144 | */ |
||
145 | View Code Duplication | public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) { |
|
156 | /** |
||
157 | * Execute a generic jQuery call with 2 elements. |
||
158 | * @param string $jQueryCall |
||
159 | * @param string $to |
||
160 | * @param string $element |
||
161 | * @param boolean $immediatly delayed if false |
||
162 | * @return string |
||
163 | */ |
||
164 | public function _genericCallElement($jQueryCall,$to='this', $element, $immediatly=false) { |
||
172 | |||
173 | /** |
||
174 | * Creates a jQuery sortable |
||
175 | * |
||
176 | * @param string $element |
||
177 | * @param array $options |
||
178 | * @return void |
||
179 | */ |
||
180 | public function sortable($element, $options=array()) { |
||
193 | |||
194 | /** |
||
195 | * Table Sorter Plugin |
||
196 | * |
||
197 | * @param string $table table name |
||
198 | * @param string $options plugin location |
||
199 | * @return string |
||
200 | */ |
||
201 | public function tablesorter($table='', $options='') { |
||
204 | |||
205 | /** |
||
206 | * Constructs the syntax for an event, and adds to into the array for compilation |
||
207 | * |
||
208 | * @param string $element The element to attach the event to |
||
209 | * @param string $js The code to execute |
||
210 | * @param string $event The event to pass |
||
211 | * @param boolean $preventDefault If set to true, the default action of the event will not be triggered. |
||
212 | * @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. |
||
213 | * @return string |
||
214 | */ |
||
215 | public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true) { |
||
233 | |||
234 | /** |
||
235 | * As events are specified, they are stored in an array |
||
236 | * This function compiles them all for output on a page |
||
237 | * @param view $view |
||
238 | * @param string $view_var |
||
239 | * @param boolean $script_tags |
||
240 | * @return string |
||
241 | */ |
||
242 | public function _compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE) { |
||
294 | |||
295 | public function _addToCompile($jsScript) { |
||
298 | |||
299 | /** |
||
300 | * Clears the array of script events collected for output |
||
301 | * |
||
302 | * @return void |
||
303 | */ |
||
304 | public function _clear_compile() { |
||
307 | |||
308 | /** |
||
309 | * A wrapper for writing document.ready() |
||
310 | * @return string |
||
311 | */ |
||
312 | public function _document_ready($js) { |
||
323 | |||
324 | /** |
||
325 | * Puts HTML element in quotes for use in jQuery code |
||
326 | * unless the supplied element is the Javascript 'this' |
||
327 | * object, in which case no quotes are added |
||
328 | * |
||
329 | * @param string $element |
||
330 | * @return string |
||
331 | */ |
||
332 | public function _prep_element($element) { |
||
338 | |||
339 | /** |
||
340 | * Puts HTML values in quotes for use in jQuery code |
||
341 | * unless the supplied value contains the Javascript 'this' or 'event' |
||
342 | * object, in which case no quotes are added |
||
343 | * |
||
344 | * @param string $value |
||
345 | * @return string |
||
346 | */ |
||
347 | public function _prep_value($value) { |
||
356 | |||
357 | private function minify($input) { |
||
381 | } |
||
382 |
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: