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 JqueryAjaxTrait 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 JqueryAjaxTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | trait JqueryAjaxTrait { |
||
17 | protected $ajaxTransition; |
||
18 | protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>'; |
||
19 | |||
20 | abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true); |
||
32 | |||
33 | public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
||
39 | |||
40 | protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
||
55 | |||
56 | protected function setAjaxDataCall($params){ |
||
57 | $result=null; |
||
58 | if(!\is_callable($params)){ |
||
59 | $result=function ($responseElement,$jqueryDone="html") use($params){ |
||
60 | return AjaxTransition::{$params}($responseElement,$jqueryDone); |
||
61 | }; |
||
62 | } |
||
63 | return $result; |
||
64 | } |
||
65 | |||
66 | protected function _getAjaxUrl($url,$attr){ |
||
82 | |||
83 | protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback){ |
||
99 | |||
100 | protected function _getResponseElement($responseElement){ |
||
106 | |||
107 | protected function _correctAjaxUrl($url) { |
||
115 | |||
116 | /** |
||
117 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
118 | * @param string $url the request address |
||
119 | * @param string $params Paramètres passés au format JSON |
||
120 | * @param string $method Method use |
||
121 | * @param string $jsCallback javascript code to execute after the request |
||
122 | * @param boolean $immediatly |
||
123 | */ |
||
124 | public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) { |
||
137 | |||
138 | /** |
||
139 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
140 | * @param string $element |
||
141 | * @param string $event |
||
142 | * @param string $url the request address |
||
143 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
144 | */ |
||
145 | View Code Duplication | public function _jsonOn($event,$element, $url,$parameters=array()) { |
|
157 | |||
158 | /** |
||
159 | * Makes an ajax request and receives a JSON array data types by copying and assigning them to the DOM elements with the same name |
||
160 | * @param string $url the request address |
||
161 | * @param string $params Paramètres passés au format JSON |
||
162 | * @param string $method Method use |
||
163 | * @param string $jsCallback javascript code to execute after the request |
||
164 | * @param string $context jquery DOM element, array container. |
||
165 | * @param boolean $immediatly |
||
166 | */ |
||
167 | public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null,$immediatly=false) { |
||
190 | /** |
||
191 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
192 | * @param string $element |
||
193 | * @param string $event |
||
194 | * @param string $url the request address |
||
195 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get", "context"=>null) |
||
196 | */ |
||
197 | View Code Duplication | public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=array()) { |
|
209 | |||
210 | public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
||
232 | |||
233 | /** |
||
234 | * Effectue un get vers $url sur l'évènement $event de $element en passant les paramètres $params |
||
235 | * puis affiche le résultat dans $responseElement |
||
236 | * @param string $element |
||
237 | * @param string $event |
||
238 | * @param string $url |
||
239 | * @param string $params queryString parameters (JSON format). default : {} |
||
240 | * @param string $responseElement |
||
241 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxTransition"=>null,"jqueryDone"=>"html") |
||
242 | */ |
||
243 | View Code Duplication | public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
255 | |||
256 | /** |
||
257 | * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres $params |
||
258 | * puis affiche le résultat dans $responseElement |
||
259 | * @param string $element |
||
260 | * @param string $event |
||
261 | * @param string $url |
||
262 | * @param string $params queryString parameters (JSON format). default : {} |
||
263 | * @param string $responseElement |
||
264 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxTransition"=>null) |
||
265 | */ |
||
266 | View Code Duplication | public function _postOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
278 | |||
279 | /** |
||
280 | * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres du formulaire $form |
||
281 | * puis affiche le résultat dans $responseElement |
||
282 | * @param string $element |
||
283 | * @param string $event |
||
284 | * @param string $url |
||
285 | * @param string $form |
||
286 | * @param string $responseElement |
||
287 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxTransition"=>null,"immediatly"=>true) |
||
288 | */ |
||
289 | public function _postFormOn($event,$element, $url, $form, $responseElement="", $parameters=array()) { |
||
302 | } |
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.