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 JsUtilsAjaxTrait 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 JsUtilsAjaxTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | trait JsUtilsAjaxTrait { |
||
14 | |||
15 | protected $ajaxTransition; |
||
16 | protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>'; |
||
17 | |||
18 | abstract public function getUrl($url); |
||
20 | |||
21 | protected function _ajax($method,$url,$responseElement="",$parameters=[]) { |
||
56 | |||
57 | protected function createAjaxParameters(&$original,$parameters){ |
||
65 | |||
66 | protected function implodeAjaxParameters($ajaxParameters){ |
||
70 | |||
71 | protected function _addJsCondition($jsCondition,$jsSource){ |
||
77 | |||
78 | |||
79 | protected function _getAjaxUrl($url,$attr){ |
||
98 | |||
99 | protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback){ |
||
115 | |||
116 | protected function _getResponseElement($responseElement){ |
||
122 | |||
123 | protected function _correctAjaxUrl($url) { |
||
131 | |||
132 | public static function _correctParams($params){ |
||
141 | |||
142 | public static function _implodeParams($parameters){ |
||
150 | |||
151 | protected function addLoading(&$retour, $responseElement) { |
||
162 | |||
163 | protected function setAjaxDataCall($params){ |
||
172 | |||
173 | protected function setDefaultParameters(&$parameters,$default){ |
||
179 | |||
180 | public function setAjaxLoader($loader) { |
||
183 | |||
184 | /** |
||
185 | * Performs an ajax GET request |
||
186 | * @param string $url The url of the request |
||
187 | * @param string $responseElement selector of the HTML element displaying the answer |
||
188 | */ |
||
189 | private function _get($url, $responseElement="",$parameters=[]) { |
||
192 | |||
193 | /** |
||
194 | * Performs an ajax GET request |
||
195 | * @param string $url The url of the request |
||
196 | * @param string $responseElement selector of the HTML element displaying the answer |
||
197 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
198 | */ |
||
199 | public function get($url, $responseElement="",$parameters=[]) { |
||
203 | |||
204 | /** |
||
205 | * Performs an ajax request |
||
206 | * @param string $method The http method (get, post, delete, put, head) |
||
207 | * @param string $url The url of the request |
||
208 | * @param string $responseElement selector of the HTML element displaying the answer |
||
209 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
210 | */ |
||
211 | public function ajax($method,$url, $responseElement="", $parameters=[]) { |
||
215 | |||
216 | /** |
||
217 | * Performs a deferred ajax request |
||
218 | * @param string $method The http method (get, post, delete, put, head) |
||
219 | * @param string $url The url of the request |
||
220 | * @param string $responseElement selector of the HTML element displaying the answer |
||
221 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
222 | */ |
||
223 | public function ajaxDeferred($method,$url, $responseElement="", $parameters=[]) { |
||
227 | |||
228 | /** |
||
229 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
230 | * @param string $url the request url |
||
231 | * @param string $params JSON parameters |
||
232 | * @param string $method Method used |
||
233 | * @param string $jsCallback javascript code to execute after the request |
||
234 | * @param string $attr |
||
235 | * @param string $context |
||
236 | * @param boolean $immediatly |
||
237 | */ |
||
238 | private function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) { |
||
251 | |||
252 | /** |
||
253 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
254 | * @param string $url the request url |
||
255 | * @param string $params JSON parameters |
||
256 | * @param string $method Method used |
||
257 | * @param string $jsCallback javascript code to execute after the request |
||
258 | * @param string $context |
||
259 | * @param boolean $immediatly |
||
260 | */ |
||
261 | public function json($url, $method="get", $params="{}", $jsCallback=NULL,$context="document",$immediatly=false) { |
||
264 | |||
265 | /** |
||
266 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
267 | * @param string $element |
||
268 | * @param string $event |
||
269 | * @param string $url the request address |
||
270 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
271 | */ |
||
272 | View Code Duplication | public function jsonOn($event,$element, $url,$parameters=array()) { |
|
284 | |||
285 | /** |
||
286 | * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name |
||
287 | * @param string $url the request url |
||
288 | * @param string $params Paramètres passés au format JSON |
||
289 | * @param string $method Method used |
||
290 | * @param string $jsCallback javascript code to execute after the request |
||
291 | * @param string $context jquery DOM element, array container. |
||
292 | */ |
||
293 | public function jsonDeferred($url, $method="get", $params="{}", $jsCallback=NULL,$context=NULL) { |
||
296 | |||
297 | /** |
||
298 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
299 | * @param string $url the request url |
||
300 | * @param string $params The JSON parameters |
||
301 | * @param string $method Method used |
||
302 | * @param string $jsCallback javascript code to execute after the request |
||
303 | * @param string $context jquery DOM element, array container. |
||
304 | * @param string $rowClass the css class for the new element |
||
305 | * @param boolean $immediatly |
||
306 | */ |
||
307 | private function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$attr="id",$immediatly=false) { |
||
330 | |||
331 | /** |
||
332 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
333 | * @param string $url the request url |
||
334 | * @param string $params The JSON parameters |
||
335 | * @param string $method Method used |
||
336 | * @param string $jsCallback javascript code to execute after the request |
||
337 | * @param string $rowClass the css class for the new element |
||
338 | * @param string $context jquery DOM element, array container. |
||
339 | * @param boolean $immediatly |
||
340 | */ |
||
341 | public function jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$immediatly=false) { |
||
344 | |||
345 | /** |
||
346 | * Peforms an ajax request delayed and receives a JSON array data types by copying and assigning them to the DOM elements with the same name |
||
347 | * @param string $maskSelector the selector of the element to clone |
||
348 | * @param string $url the request url |
||
349 | * @param string $params JSON parameters |
||
350 | * @param string $method Method used |
||
351 | * @param string $jsCallback javascript code to execute after the request |
||
352 | * @param string $rowClass the css class for the new element |
||
353 | * @param string $context jquery DOM element, array container. |
||
354 | */ |
||
355 | public function jsonArrayDeferred($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL) { |
||
358 | |||
359 | /** |
||
360 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element |
||
361 | * @param string $element |
||
362 | * @param string $event |
||
363 | * @param string $url the request url |
||
364 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true) |
||
365 | */ |
||
366 | View Code Duplication | public function jsonArrayOn($event,$element,$maskSelector, $url,$parameters=array()) { |
|
379 | |||
380 | /** |
||
381 | * Prepares a Get ajax request |
||
382 | * To use on an event |
||
383 | * @param string $url The url of the request |
||
384 | * @param string $responseElement selector of the HTML element displaying the answer |
||
385 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
386 | */ |
||
387 | public function getDeferred($url, $responseElement="", $parameters=[]) { |
||
391 | |||
392 | /** |
||
393 | * Performs a get to $url on the event $event on $element |
||
394 | * and display it in $responseElement |
||
395 | * @param string $event |
||
396 | * @param string $element |
||
397 | * @param string $url The url of the request |
||
398 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
399 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null) |
||
400 | */ |
||
401 | public function getOn($event, $element, $url, $responseElement="", $parameters=array()) { |
||
405 | |||
406 | /** |
||
407 | * Performs an ajax request to $url on the event $event on $element |
||
408 | * and display it in $responseElement |
||
409 | * @param string $event |
||
410 | * @param string $element |
||
411 | * @param string $url The url of the request |
||
412 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
413 | * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null) |
||
414 | */ |
||
415 | public function ajaxOn($event, $element, $url, $responseElement="", $parameters=array()) { |
||
419 | |||
420 | /** |
||
421 | * Performs a get to $url on the click event on $element |
||
422 | * and display it in $responseElement |
||
423 | * @param string $element |
||
424 | * @param string $url The url of the request |
||
425 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
426 | * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null) |
||
427 | */ |
||
428 | public function ajaxOnClick($element, $url, $responseElement="", $parameters=array()) { |
||
431 | |||
432 | /** |
||
433 | * Performs a get to $url on the click event on $element |
||
434 | * and display it in $responseElement |
||
435 | * @param string $element |
||
436 | * @param string $url The url of the request |
||
437 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
438 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null) |
||
439 | */ |
||
440 | public function getOnClick($element, $url, $responseElement="", $parameters=array()) { |
||
443 | |||
444 | private function _post($url, $params="{}",$responseElement="", $parameters=[]) { |
||
448 | |||
449 | /** |
||
450 | * Makes an ajax post |
||
451 | * @param string $url the request url |
||
452 | * @param string $responseElement selector of the HTML element displaying the answer |
||
453 | * @param string $params JSON parameters |
||
454 | * @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
455 | */ |
||
456 | public function post($url, $params="{}",$responseElement="", $parameters=[]) { |
||
459 | |||
460 | /** |
||
461 | * Prepares a delayed ajax POST |
||
462 | * to use on an event |
||
463 | * @param string $url the request url |
||
464 | * @param string $params JSON parameters |
||
465 | * @param string $responseElement selector of the HTML element displaying the answer |
||
466 | * @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
467 | */ |
||
468 | public function postDeferred($url, $params="{}",$responseElement="", $parameters=[]) { |
||
472 | |||
473 | /** |
||
474 | * Performs a post to $url on the event $event fired on $element and pass the parameters $params |
||
475 | * Display the result in $responseElement |
||
476 | * @param string $event |
||
477 | * @param string $element |
||
478 | * @param string $url The url of the request |
||
479 | * @param string $params The parameters to send |
||
480 | * @param string $responseElement selector of the HTML element displaying the answer |
||
481 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
482 | */ |
||
483 | public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
487 | |||
488 | /** |
||
489 | * Performs a post to $url on the click event fired on $element and pass the parameters $params |
||
490 | * Display the result in $responseElement |
||
491 | * @param string $element |
||
492 | * @param string $url The url of the request |
||
493 | * @param string $params The parameters to send |
||
494 | * @param string $responseElement selector of the HTML element displaying the answer |
||
495 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
496 | */ |
||
497 | public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
500 | |||
501 | private function _postForm($url, $form, $responseElement, $parameters=[]) { |
||
532 | |||
533 | /** |
||
534 | * Performs a post form with ajax |
||
535 | * @param string $url The url of the request |
||
536 | * @param string $form The form HTML id |
||
537 | * @param string $responseElement selector of the HTML element displaying the answer |
||
538 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
539 | */ |
||
540 | public function postForm($url, $form, $responseElement, $parameters=[]) { |
||
544 | |||
545 | /** |
||
546 | * Performs a delayed post form with ajax |
||
547 | * For use on an event |
||
548 | * @param string $url The url of the request |
||
549 | * @param string $form The form HTML id |
||
550 | * @param string $responseElement selector of the HTML element displaying the answer |
||
551 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
552 | */ |
||
553 | public function postFormDeferred($url, $form, $responseElement, $parameters=[]) { |
||
557 | |||
558 | /** |
||
559 | * Performs a post form with ajax in response to an event $event on $element |
||
560 | * display the result in $responseElement |
||
561 | * @param string $event |
||
562 | * @param string $element |
||
563 | * @param string $url |
||
564 | * @param string $form |
||
565 | * @param string $responseElement selector of the HTML element displaying the answer |
||
566 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null) |
||
567 | */ |
||
568 | public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) { |
||
572 | |||
573 | /** |
||
574 | * Performs a post form with ajax in response to the click event on $element |
||
575 | * display the result in $responseElement |
||
576 | * @param string $element |
||
577 | * @param string $url |
||
578 | * @param string $form |
||
579 | * @param string $responseElement selector of the HTML element displaying the answer |
||
580 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null) |
||
581 | */ |
||
582 | public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) { |
||
585 | } |
||
586 |
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.