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 | |||
9 | protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>'; |
||
10 | |||
11 | public abstract function _prep_value($value); |
||
24 | |||
25 | public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
31 | |||
32 | protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
47 | |||
48 | protected function _getAjaxUrl($url,$attr){ |
||
49 | $url=$this->_correctAjaxUrl($url); |
||
50 | $retour="url='".$url."';\n"; |
||
51 | $slash="/"; |
||
52 | if(PhalconUtils::endsWith($url, "/")===true) |
||
53 | $slash=""; |
||
54 | if(JString::isNotNull($attr)){ |
||
55 | if ($attr=="value") |
||
56 | $retour.="url=url+'".$slash."'+$(this).val();\n"; |
||
57 | else if($attr!=null && $attr!=="") |
||
58 | $retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n"; |
||
59 | } |
||
60 | return $retour; |
||
61 | } |
||
62 | |||
63 | protected function _getOnAjaxDone($responseElement,$jsCallback){ |
||
71 | |||
72 | protected function _getResponseElement($responseElement){ |
||
78 | |||
79 | protected function _correctAjaxUrl($url) { |
||
80 | if ($url!=="/" && JString::endsWith($url, "/")===true) |
||
81 | $url=substr($url, 0, strlen($url)-1); |
||
82 | if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) { |
||
83 | $url=$this->jsUtils->getUrl($url); |
||
84 | } |
||
85 | return $url; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
90 | * @param string $url the request address |
||
91 | * @param string $params Paramètres passés au format JSON |
||
92 | * @param string $method Method use |
||
93 | * @param string $jsCallback javascript code to execute after the request |
||
94 | * @param boolean $immediatly |
||
95 | */ |
||
96 | public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) { |
||
109 | |||
110 | /** |
||
111 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
112 | * @param string $element |
||
113 | * @param string $event |
||
114 | * @param string $url the request address |
||
115 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
116 | */ |
||
117 | View Code Duplication | public function _jsonOn($event,$element, $url,$parameters=array()) { |
|
129 | |||
130 | /** |
||
131 | * Makes an ajax request and receives a JSON array data types by copying and assigning them to the DOM elements with the same name |
||
132 | * @param string $url the request address |
||
133 | * @param string $params Paramètres passés au format JSON |
||
134 | * @param string $method Method use |
||
135 | * @param string $jsCallback javascript code to execute after the request |
||
136 | * @param string $context jquery DOM element, array container. |
||
137 | * @param boolean $immediatly |
||
138 | */ |
||
139 | public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null,$immediatly=false) { |
||
159 | /** |
||
160 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
161 | * @param string $element |
||
162 | * @param string $event |
||
163 | * @param string $url the request address |
||
164 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get", "context"=>null) |
||
165 | */ |
||
166 | View Code Duplication | public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=array()) { |
|
178 | |||
179 | public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
||
201 | |||
202 | /** |
||
203 | * Effectue un get vers $url sur l'évènement $event de $element en passant les paramètres $params |
||
204 | * puis affiche le résultat dans $responseElement |
||
205 | * @param string $element |
||
206 | * @param string $event |
||
207 | * @param string $url |
||
208 | * @param string $params queryString parameters (JSON format). default : {} |
||
209 | * @param string $responseElement |
||
210 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true) |
||
211 | */ |
||
212 | View Code Duplication | public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
222 | |||
223 | /** |
||
224 | * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres $params |
||
225 | * puis affiche le résultat dans $responseElement |
||
226 | * @param string $element |
||
227 | * @param string $event |
||
228 | * @param string $url |
||
229 | * @param string $params queryString parameters (JSON format). default : {} |
||
230 | * @param string $responseElement |
||
231 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true) |
||
232 | */ |
||
233 | View Code Duplication | public function _postOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
243 | |||
244 | /** |
||
245 | * Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres du formulaire $form |
||
246 | * puis affiche le résultat dans $responseElement |
||
247 | * @param string $element |
||
248 | * @param string $event |
||
249 | * @param string $url |
||
250 | * @param string $form |
||
251 | * @param string $responseElement |
||
252 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true) |
||
253 | */ |
||
254 | View Code Duplication | public function _postFormOn($event,$element, $url, $form, $responseElement="", $parameters=array()) { |
|
265 | } |
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: