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:
1 | <?php |
||
5 | trait JsUtilsActionsTrait { |
||
6 | |||
7 | /** |
||
8 | * add class to element |
||
9 | * |
||
10 | * @param string $element |
||
11 | * @param string $class to add |
||
12 | * @param boolean $immediatly defers the execution if set to false |
||
13 | * @return string |
||
14 | */ |
||
15 | public function addClass($element='this', $class='', $immediatly=false) { |
||
18 | |||
19 | /** |
||
20 | * Insert content, specified by the parameter, after each element in the set of matched elements |
||
21 | * @param string $to |
||
22 | * @param string $element |
||
23 | * @param boolean $immediatly defers the execution if set to false |
||
24 | * @return string |
||
25 | */ |
||
26 | public function after($to, $element, $immediatly=false){ |
||
29 | |||
30 | /** |
||
31 | * Insert content, specified by the parameter, before each element in the set of matched elements |
||
32 | * @param string $to |
||
33 | * @param string $element |
||
34 | * @param boolean $immediatly defers the execution if set to false |
||
35 | * @return string |
||
36 | */ |
||
37 | public function before($to, $element, $immediatly=false){ |
||
40 | |||
41 | /** |
||
42 | * 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. |
||
43 | * @param string $element |
||
44 | * @param string $attributeName |
||
45 | * @param string $value |
||
46 | * @param boolean $immediatly defers the execution if set to false |
||
47 | */ |
||
48 | public function attr($element='this', $attributeName='value', $value='', $immediatly=false) { |
||
51 | |||
52 | /** |
||
53 | * 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. |
||
54 | * @param string $element |
||
55 | * @param string $value |
||
56 | * @param boolean $immediatly defers the execution if set to false |
||
57 | */ |
||
58 | public function val($element='this',$value='',$immediatly=false){ |
||
61 | |||
62 | /** |
||
63 | * Get or set the html of an attribute for the first element in the set of matched elements. |
||
64 | * @param string $element |
||
65 | * @param string $value |
||
66 | * @param boolean $immediatly defers the execution if set to false |
||
67 | */ |
||
68 | public function html($element='this', $value='', $immediatly=false) { |
||
71 | |||
72 | /** |
||
73 | * Outputs a javascript library animate event |
||
74 | * |
||
75 | * @param string $element element |
||
76 | * @param array $params |
||
77 | * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds |
||
78 | * @param string $extra |
||
79 | * @param boolean $immediatly defers the execution if set to false |
||
80 | * @return string |
||
81 | */ |
||
82 | public function animate($element='this', $params=array(), $speed='', $extra='', $immediatly=false) { |
||
85 | |||
86 | /** |
||
87 | * Insert content, specified by the parameter $element, to the end of each element in the set of matched elements $to. |
||
88 | * @param string $to |
||
89 | * @param string $element |
||
90 | * @param boolean $immediatly defers the execution if set to false |
||
91 | * @return string |
||
92 | */ |
||
93 | public function append($to, $element, $immediatly=false) { |
||
96 | |||
97 | /** |
||
98 | * Insert content, specified by the parameter $element, to the beginning of each element in the set of matched elements $to. |
||
99 | * @param string $to |
||
100 | * @param string $element |
||
101 | * @param boolean $immediatly defers the execution if set to false |
||
102 | * @return string |
||
103 | */ |
||
104 | public function prepend($to, $element, $immediatly=false) { |
||
107 | |||
108 | /** |
||
109 | * Execute a javascript library hide action |
||
110 | * |
||
111 | * @param string - element |
||
112 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
113 | * @param string - Javascript callback function |
||
114 | * @param boolean $immediatly defers the execution if set to false |
||
115 | * @return string |
||
116 | */ |
||
117 | public function fadeIn($element='this', $speed='', $callback='', $immediatly=false) { |
||
120 | |||
121 | /** |
||
122 | * Execute a javascript library hide action |
||
123 | * |
||
124 | * @param string - element |
||
125 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
126 | * @param string - Javascript callback function |
||
127 | * @param boolean $immediatly defers the execution if set to false |
||
128 | * @return string |
||
129 | */ |
||
130 | public function fadeOut($element='this', $speed='', $callback='', $immediatly=false) { |
||
133 | |||
134 | /** |
||
135 | * Execute a javascript library slideUp action |
||
136 | * |
||
137 | * @param string - element |
||
138 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
139 | * @param string - Javascript callback function |
||
140 | * @param boolean $immediatly defers the execution if set to false |
||
141 | * @return string |
||
142 | */ |
||
143 | public function slideUp($element='this', $speed='', $callback='', $immediatly=false) { |
||
146 | |||
147 | /** |
||
148 | * Execute a javascript library removeClass action |
||
149 | * |
||
150 | * @param string - element |
||
151 | * @param string - Class to add |
||
152 | * @param boolean $immediatly defers the execution if set to false |
||
153 | * @return string |
||
154 | */ |
||
155 | public function removeClass($element='this', $class='', $immediatly=false) { |
||
158 | |||
159 | /** |
||
160 | * Execute a javascript library slideDown action |
||
161 | * |
||
162 | * @param string - element |
||
163 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
164 | * @param string - Javascript callback function |
||
165 | * @param boolean $immediatly defers the execution if set to false |
||
166 | * @return string |
||
167 | */ |
||
168 | public function slideDown($element='this', $speed='', $callback='', $immediatly=false) { |
||
171 | |||
172 | /** |
||
173 | * Execute a javascript library slideToggle action |
||
174 | * |
||
175 | * @param string - element |
||
176 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
177 | * @param string - Javascript callback function |
||
178 | * @param boolean $immediatly defers the execution if set to false |
||
179 | * @return string |
||
180 | */ |
||
181 | public function slideToggle($element='this', $speed='', $callback='', $immediatly=false) { |
||
184 | |||
185 | /** |
||
186 | * Execute a javascript library hide action |
||
187 | * |
||
188 | * @param string - element |
||
189 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
190 | * @param string - Javascript callback function |
||
191 | * @param boolean $immediatly defers the execution if set to false |
||
192 | * @return string |
||
193 | */ |
||
194 | public function hide($element='this', $speed='', $callback='', $immediatly=false) { |
||
197 | |||
198 | /** |
||
199 | * Execute a javascript library toggle action |
||
200 | * |
||
201 | * @param string - element |
||
202 | * @param boolean $immediatly defers the execution if set to false |
||
203 | * @return string |
||
204 | */ |
||
205 | public function toggle($element='this', $immediatly=false) { |
||
208 | |||
209 | /** |
||
210 | * Execute a javascript library toggle class action |
||
211 | * |
||
212 | * @param string - element |
||
213 | * @param boolean $immediatly defers the execution if set to false |
||
214 | * @return string |
||
215 | */ |
||
216 | public function toggleClass($element='this', $class='', $immediatly=false) { |
||
219 | |||
220 | /** |
||
221 | * Execute all handlers and behaviors attached to the matched elements for the given event. |
||
222 | * @param string $element |
||
223 | * @param string $event |
||
224 | * @param boolean $immediatly defers the execution if set to false |
||
225 | */ |
||
226 | public function trigger($element='this', $event='click', $immediatly=false) { |
||
229 | |||
230 | /** |
||
231 | * Execute a javascript library show action |
||
232 | * |
||
233 | * @param string - element |
||
234 | * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds |
||
235 | * @param string - Javascript callback function |
||
236 | * @param boolean $immediatly defers the execution if set to false |
||
237 | * @return string |
||
238 | */ |
||
239 | public function show($element='this', $speed='', $callback='', $immediatly=false) { |
||
242 | |||
243 | /** |
||
244 | * Allows to attach a condition |
||
245 | * @param string $condition |
||
246 | * @param string $jsCodeIfTrue |
||
247 | * @param string $jsCodeIfFalse |
||
248 | * @param boolean $immediatly defers the execution if set to false |
||
249 | */ |
||
250 | public function condition($condition, $jsCodeIfTrue, $jsCodeIfFalse=null, $immediatly=false) { |
||
253 | |||
254 | /** |
||
255 | * Calls the JQuery callback $someThing on $element with facultative parameter $param |
||
256 | * @param string $element the element |
||
257 | * @param string $jqueryCall the JQuery callback |
||
258 | * @param mixed $param array or string parameters |
||
259 | * @param string $jsCallback javascript code to execute after the jquery call |
||
260 | * @return mixed |
||
261 | */ |
||
262 | public function doJQuery($element, $jqueryCall, $param="", $jsCallback="") { |
||
265 | |||
266 | /** |
||
267 | * Calls the JQuery callback $someThing on $element with facultative parameter $param |
||
268 | * @param string $element the element |
||
269 | * @param string $jqueryCall the JQuery callback |
||
270 | * @param mixed $param array or string parameters |
||
271 | * @param string $jsCallback javascript code to execute after the jquery call |
||
272 | * @return mixed |
||
273 | */ |
||
274 | public function doJQueryDeferred($element, $jqueryCall, $param="", $jsCallback="") { |
||
277 | |||
278 | /** |
||
279 | * Calls the JQuery callback $jqueryCall on $element with facultative parameter $param in response to an event $event |
||
280 | * @param string $event |
||
281 | * @param string $element |
||
282 | * @param string $elementToModify |
||
283 | * @param string $jqueryCall |
||
284 | * @param string $param |
||
285 | * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"jsCallback"=>'',"immediatly"=>true) |
||
286 | */ |
||
287 | View Code Duplication | public function doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $parameters=array()) { |
|
295 | |||
296 | /** |
||
297 | * Executes the code $js |
||
298 | * @param string $js Code to execute |
||
299 | * @param boolean $immediatly delayed if false |
||
300 | * @return String |
||
301 | */ |
||
302 | public function exec($js, $immediatly=false) { |
||
306 | |||
307 | /** |
||
308 | * Executes the javascript code $js when $event fires on $element |
||
309 | * @param string $event |
||
310 | * @param string $element |
||
311 | * @param string $js Code to execute |
||
312 | * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true) |
||
313 | * @return String |
||
314 | */ |
||
315 | View Code Duplication | public function execOn($event, $element, $js, $parameters=array()) { |
|
323 | } |
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: