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, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | protected function _getAjaxUrl($url,$attr){ |
||
| 55 | |||
| 56 | protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback){ |
||
| 72 | |||
| 73 | protected function _getResponseElement($responseElement){ |
||
| 79 | |||
| 80 | protected function _correctAjaxUrl($url) { |
||
| 88 | |||
| 89 | protected function addLoading(&$retour, $responseElement) { |
||
| 100 | |||
| 101 | protected function setAjaxDataCall($params){ |
||
| 110 | |||
| 111 | public function setAjaxLoader($loader) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Performs an ajax GET request |
||
| 117 | * @param string $url The url of the request |
||
| 118 | * @param string $params JSON parameters |
||
| 119 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 120 | * @param string $jsCallback javascript code to execute after the request |
||
| 121 | * @param boolean $hasLoader true for showing ajax loader. default : true |
||
| 122 | * @param string $jqueryDone the jquery function call on ajax data. default:html |
||
| 123 | * @param string|callable $ajaxTransition |
||
| 124 | */ |
||
| 125 | private function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Performs an ajax GET request |
||
| 131 | * @param string $url The url of the request |
||
| 132 | * @param string $params JSON parameters |
||
| 133 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 134 | * @param string $jsCallback javascript code to execute after the request |
||
| 135 | * @param boolean $hasLoader true for showing ajax loader. default : true |
||
| 136 | * @param string $jqueryDone the jquery function call on ajax data. default:html |
||
| 137 | * @param string|callable $ajaxTransition |
||
| 138 | */ |
||
| 139 | public function get($url, $responseElement="", $params="{}", $jsCallback=NULL,$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
| 145 | * @param string $url the request url |
||
| 146 | * @param string $params JSON parameters |
||
| 147 | * @param string $method Method used |
||
| 148 | * @param string $jsCallback javascript code to execute after the request |
||
| 149 | * @param string $attr |
||
| 150 | * @param string $context |
||
| 151 | * @param boolean $immediatly |
||
| 152 | */ |
||
| 153 | private function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
| 169 | * @param string $url the request url |
||
| 170 | * @param string $params JSON parameters |
||
| 171 | * @param string $method Method used |
||
| 172 | * @param string $jsCallback javascript code to execute after the request |
||
| 173 | * @param string $context |
||
| 174 | * @param boolean $immediatly |
||
| 175 | */ |
||
| 176 | public function json($url, $method="get", $params="{}", $jsCallback=NULL,$context="document",$immediatly=false) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
| 182 | * @param string $element |
||
| 183 | * @param string $event |
||
| 184 | * @param string $url the request address |
||
| 185 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
| 186 | */ |
||
| 187 | View Code Duplication | public function jsonOn($event,$element, $url,$parameters=array()) { |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name |
||
| 202 | * @param string $url the request url |
||
| 203 | * @param string $params Paramètres passés au format JSON |
||
| 204 | * @param string $method Method used |
||
| 205 | * @param string $jsCallback javascript code to execute after the request |
||
| 206 | * @param string $context jquery DOM element, array container. |
||
| 207 | */ |
||
| 208 | public function jsonDeferred($url, $method="get", $params="{}", $jsCallback=NULL,$context=NULL) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
| 214 | * @param string $url the request url |
||
| 215 | * @param string $params The JSON parameters |
||
| 216 | * @param string $method Method used |
||
| 217 | * @param string $jsCallback javascript code to execute after the request |
||
| 218 | * @param string $context jquery DOM element, array container. |
||
| 219 | * @param string $rowClass the css class for the new element |
||
| 220 | * @param boolean $immediatly |
||
| 221 | */ |
||
| 222 | private function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$attr="id",$immediatly=false) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
| 248 | * @param string $url the request url |
||
| 249 | * @param string $params The JSON parameters |
||
| 250 | * @param string $method Method used |
||
| 251 | * @param string $jsCallback javascript code to execute after the request |
||
| 252 | * @param string $rowClass the css class for the new element |
||
| 253 | * @param string $context jquery DOM element, array container. |
||
| 254 | * @param boolean $immediatly |
||
| 255 | */ |
||
| 256 | public function jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$immediatly=false) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * 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 |
||
| 262 | * @param string $maskSelector the selector of the element to clone |
||
| 263 | * @param string $url the request url |
||
| 264 | * @param string $params JSON parameters |
||
| 265 | * @param string $method Method used |
||
| 266 | * @param string $jsCallback javascript code to execute after the request |
||
| 267 | * @param string $rowClass the css class for the new element |
||
| 268 | * @param string $context jquery DOM element, array container. |
||
| 269 | */ |
||
| 270 | public function jsonArrayDeferred($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element |
||
| 276 | * @param string $element |
||
| 277 | * @param string $event |
||
| 278 | * @param string $url the request url |
||
| 279 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true) |
||
| 280 | */ |
||
| 281 | View Code Duplication | public function jsonArrayOn($event,$element,$maskSelector, $url,$parameters=array()) { |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Prepares a Get ajax request |
||
| 297 | * To use on an event |
||
| 298 | * @param string $url The url of the request |
||
| 299 | * @param string $params JSON parameters |
||
| 300 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 301 | * @param string $jsCallback javascript code to execute after the request |
||
| 302 | * @param string $attr the html attribute added to the request |
||
| 303 | * @param string $jqueryDone the jquery function call on ajax data. default:html |
||
| 304 | * @param string|callable $ajaxTransition |
||
| 305 | */ |
||
| 306 | public function getDeferred($url, $responseElement="", $params="{}", $jsCallback=NULL,$attr="id",$jqueryDone="html",$ajaxTransition=null) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Performs a get to $url on the event $event on $element |
||
| 312 | * and display it in $responseElement |
||
| 313 | * @param string $event |
||
| 314 | * @param string $element |
||
| 315 | * @param string $url The url of the request |
||
| 316 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
| 317 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html") |
||
| 318 | */ |
||
| 319 | public function getOn($event, $element, $url, $responseElement="", $parameters=array()) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Performs a get to $url on the click event on $element |
||
| 335 | * and display it in $responseElement |
||
| 336 | * @param string $element |
||
| 337 | * @param string $url The url of the request |
||
| 338 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
| 339 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html") |
||
| 340 | */ |
||
| 341 | public function getOnClick($element, $url, $responseElement="", $parameters=array()) { |
||
| 344 | |||
| 345 | private function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Makes an ajax post |
||
| 351 | * @param string $url the request url |
||
| 352 | * @param string $params JSON parameters |
||
| 353 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 354 | * @param string $jsCallback javascript code to execute after the request |
||
| 355 | * @param boolean $hasLoader true for showing ajax loader. default : true |
||
| 356 | * @param string $jqueryDone the jquery function call on ajax data. default:html |
||
| 357 | * @param string|callable $ajaxTransition |
||
| 358 | */ |
||
| 359 | public function post($url, $responseElement="", $params="{}", $jsCallback=NULL,$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Prepares a delayed ajax POST |
||
| 365 | * to use on an event |
||
| 366 | * @param string $url the request url |
||
| 367 | * @param string $params JSON parameters |
||
| 368 | * @param string $attr the html attribute added to the request |
||
| 369 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 370 | * @param string $jsCallback javascript code to execute after the request |
||
| 371 | * @param boolean $hasLoader true for showing ajax loader. default : true |
||
| 372 | * @param string $jqueryDone the jquery function call on ajax data. default:html |
||
| 373 | * @param string|callable $ajaxTransition |
||
| 374 | */ |
||
| 375 | public function postDeferred($url, $responseElement="", $params="{}", $jsCallback=NULL, $attr="id",$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Performs a post to $url on the event $event fired on $element and pass the parameters $params |
||
| 381 | * Display the result in $responseElement |
||
| 382 | * @param string $event |
||
| 383 | * @param string $element |
||
| 384 | * @param string $url The url of the request |
||
| 385 | * @param string $params The parameters to send |
||
| 386 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 387 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null) |
||
| 388 | */ |
||
| 389 | View Code Duplication | public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Performs a post to $url on the click event fired on $element and pass the parameters $params |
||
| 404 | * Display the result in $responseElement |
||
| 405 | * @param string $element |
||
| 406 | * @param string $url The url of the request |
||
| 407 | * @param string $params The parameters to send |
||
| 408 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 409 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null) |
||
| 410 | */ |
||
| 411 | public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 414 | |||
| 415 | private function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Performs a post form with ajax |
||
| 440 | * @param string $url The url of the request |
||
| 441 | * @param string $form The form HTML id |
||
| 442 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 443 | * @param string $jsCallback javascript code to execute after the request |
||
| 444 | * @param boolean $hasLoader true for showing ajax loader. default : true |
||
| 445 | * @param string $jqueryDone the jquery function call on ajax data. default:html |
||
| 446 | * @param string|callable $ajaxTransition |
||
| 447 | */ |
||
| 448 | public function postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL,$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Performs a delayed post form with ajax |
||
| 454 | * For use on an event |
||
| 455 | * @param string $url The url of the request |
||
| 456 | * @param string $form The form HTML id |
||
| 457 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 458 | * @param string $jsCallback javascript code to execute after the request |
||
| 459 | * @param string $attr the html attribute added to the request |
||
| 460 | * @param boolean $hasLoader true for showing ajax loader. default : true |
||
| 461 | * @param string $jqueryDone the jquery function call on ajax data. default:html |
||
| 462 | * @param string|callable $ajaxTransition |
||
| 463 | */ |
||
| 464 | public function postFormDeferred($url, $form, $responseElement, $validation=false, $jsCallback=NULL,$attr="id",$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Performs a post form with ajax in response to an event $event on $element |
||
| 470 | * display the result in $responseElement |
||
| 471 | * @param string $event |
||
| 472 | * @param string $element |
||
| 473 | * @param string $url |
||
| 474 | * @param string $form |
||
| 475 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 476 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null) |
||
| 477 | */ |
||
| 478 | public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Performs a post form with ajax in response to the click event on $element |
||
| 494 | * display the result in $responseElement |
||
| 495 | * @param string $element |
||
| 496 | * @param string $url |
||
| 497 | * @param string $form |
||
| 498 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 499 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null) |
||
| 500 | */ |
||
| 501 | public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) { |
||
| 504 | } |
||
| 505 |
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: