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 |
||
7 | trait JqueryAjaxTrait { |
||
8 | protected function addLoading(&$retour, $responseElement) { |
||
19 | |||
20 | public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
26 | |||
27 | protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
42 | |||
43 | protected function _getAjaxUrl($url,$attr){ |
||
57 | |||
58 | protected function _getOnAjaxDone($responseElement,$jsCallback){ |
||
66 | |||
67 | protected function _getResponseElement($responseElement){ |
||
73 | |||
74 | protected function _correctAjaxUrl($url) { |
||
82 | |||
83 | /** |
||
84 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
85 | * @param string $url the request address |
||
86 | * @param string $params Paramètres passés au format JSON |
||
87 | * @param string $method Method use |
||
88 | * @param string $jsCallback javascript code to execute after the request |
||
89 | * @param boolean $immediatly |
||
90 | */ |
||
91 | public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) { |
||
104 | |||
105 | /** |
||
106 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
107 | * @param string $element |
||
108 | * @param string $event |
||
109 | * @param string $url the request address |
||
110 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
111 | */ |
||
112 | View Code Duplication | public function _jsonOn($event,$element, $url,$parameters=array()) { |
|
124 | |||
125 | /** |
||
126 | * Makes an ajax request and receives a JSON array data types by copying and assigning them to the DOM elements with the same name |
||
127 | * @param string $url the request address |
||
128 | * @param string $params Paramètres passés au format JSON |
||
129 | * @param string $method Method use |
||
130 | * @param string $jsCallback javascript code to execute after the request |
||
131 | * @param string $context jquery DOM element, array container. |
||
132 | * @param boolean $immediatly |
||
133 | */ |
||
134 | public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null,$immediatly=false) { |
||
154 | /** |
||
155 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
156 | * @param string $element |
||
157 | * @param string $event |
||
158 | * @param string $url the request address |
||
159 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get", "context"=>null) |
||
160 | */ |
||
161 | View Code Duplication | public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=array()) { |
|
173 | |||
174 | public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
196 | |||
197 | /** |
||
198 | * Effectue un get vers $url sur l'évènement $event de $element en passant les paramètres $params |
||
199 | * puis affiche le résultat dans $responseElement |
||
200 | * @param string $element |
||
201 | * @param string $event |
||
202 | * @param string $url |
||
203 | * @param string $params queryString parameters (JSON format). default : {} |
||
204 | * @param string $responseElement |
||
205 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true) |
||
206 | */ |
||
207 | View Code Duplication | public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
217 | |||
218 | /** |
||
219 | * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres $params |
||
220 | * puis affiche le résultat dans $responseElement |
||
221 | * @param string $element |
||
222 | * @param string $event |
||
223 | * @param string $url |
||
224 | * @param string $params queryString parameters (JSON format). default : {} |
||
225 | * @param string $responseElement |
||
226 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true) |
||
227 | */ |
||
228 | View Code Duplication | public function _postOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
238 | |||
239 | /** |
||
240 | * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres du formulaire $form |
||
241 | * puis affiche le résultat dans $responseElement |
||
242 | * @param string $element |
||
243 | * @param string $event |
||
244 | * @param string $url |
||
245 | * @param string $form |
||
246 | * @param string $responseElement |
||
247 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true) |
||
248 | */ |
||
249 | View Code Duplication | public function _postFormOn($event,$element, $url, $form, $responseElement="", $parameters=array()) { |
|
260 | } |
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: