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 |
||
8 | trait JqueryAjaxTrait { |
||
9 | |||
10 | protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>'; |
||
11 | |||
12 | public abstract function _prep_value($value); |
||
25 | |||
26 | public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
32 | |||
33 | protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
48 | |||
49 | protected function _getAjaxUrl($url,$attr){ |
||
63 | |||
64 | protected function _getOnAjaxDone($responseElement,$jsCallback){ |
||
72 | |||
73 | protected function _getResponseElement($responseElement){ |
||
79 | |||
80 | protected function _correctAjaxUrl($url) { |
||
88 | |||
89 | /** |
||
90 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
91 | * @param string $url the request address |
||
92 | * @param string $params Paramètres passés au format JSON |
||
93 | * @param string $method Method use |
||
94 | * @param string $jsCallback javascript code to execute after the request |
||
95 | * @param boolean $immediatly |
||
96 | */ |
||
97 | public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) { |
||
110 | |||
111 | /** |
||
112 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
113 | * @param string $element |
||
114 | * @param string $event |
||
115 | * @param string $url the request address |
||
116 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
117 | */ |
||
118 | View Code Duplication | public function _jsonOn($event,$element, $url,$parameters=array()) { |
|
130 | |||
131 | /** |
||
132 | * Makes an ajax request and receives a JSON array data types by copying and assigning them to the DOM elements with the same name |
||
133 | * @param string $url the request address |
||
134 | * @param string $params Paramètres passés au format JSON |
||
135 | * @param string $method Method use |
||
136 | * @param string $jsCallback javascript code to execute after the request |
||
137 | * @param string $context jquery DOM element, array container. |
||
138 | * @param boolean $immediatly |
||
139 | */ |
||
140 | public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null,$immediatly=false) { |
||
160 | /** |
||
161 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
162 | * @param string $element |
||
163 | * @param string $event |
||
164 | * @param string $url the request address |
||
165 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get", "context"=>null) |
||
166 | */ |
||
167 | View Code Duplication | public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=array()) { |
|
179 | |||
180 | public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
202 | |||
203 | /** |
||
204 | * Effectue un get vers $url sur l'évènement $event de $element en passant les paramètres $params |
||
205 | * puis affiche le résultat dans $responseElement |
||
206 | * @param string $element |
||
207 | * @param string $event |
||
208 | * @param string $url |
||
209 | * @param string $params queryString parameters (JSON format). default : {} |
||
210 | * @param string $responseElement |
||
211 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true) |
||
212 | */ |
||
213 | View Code Duplication | public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
223 | |||
224 | /** |
||
225 | * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres $params |
||
226 | * puis affiche le résultat dans $responseElement |
||
227 | * @param string $element |
||
228 | * @param string $event |
||
229 | * @param string $url |
||
230 | * @param string $params queryString parameters (JSON format). default : {} |
||
231 | * @param string $responseElement |
||
232 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true) |
||
233 | */ |
||
234 | View Code Duplication | public function _postOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
244 | |||
245 | /** |
||
246 | * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres du formulaire $form |
||
247 | * puis affiche le résultat dans $responseElement |
||
248 | * @param string $element |
||
249 | * @param string $event |
||
250 | * @param string $url |
||
251 | * @param string $form |
||
252 | * @param string $responseElement |
||
253 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true) |
||
254 | */ |
||
255 | View Code Duplication | public function _postFormOn($event,$element, $url, $form, $responseElement="", $parameters=array()) { |
|
266 | } |
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: