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 JsUtils 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 JsUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class JsUtils{ |
||
| 25 | use JsUtilsEventsTrait,JsUtilsActionsTrait,JsUtilsAjaxTrait; |
||
| 26 | |||
| 27 | protected $js; |
||
| 28 | protected $cdns; |
||
| 29 | /** |
||
| 30 | * |
||
| 31 | * @var JqueryUI |
||
| 32 | */ |
||
| 33 | protected $_ui; |
||
| 34 | /** |
||
| 35 | * |
||
| 36 | * @var Bootstrap |
||
| 37 | */ |
||
| 38 | protected $_bootstrap; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * @var Semantic |
||
| 43 | */ |
||
| 44 | protected $_semantic; |
||
| 45 | /** |
||
| 46 | * |
||
| 47 | * @var Config |
||
| 48 | */ |
||
| 49 | protected $config; |
||
| 50 | |||
| 51 | protected function _setDi($di) { |
||
| 55 | |||
| 56 | public abstract function getUrl($url); |
||
| 58 | /** |
||
| 59 | * render the content of $controller::$action and set the response to the modal content |
||
| 60 | * @param Controller $initialController |
||
| 61 | * @param string $controller a Phalcon controller |
||
| 62 | * @param string $action a Phalcon action |
||
| 63 | */ |
||
| 64 | public abstract function forward($initialController,$controller,$action); |
||
| 65 | /** |
||
| 66 | * render the content of an existing view : $viewName and set the response to the modal content |
||
| 67 | * @param Controller $initialControllerInstance |
||
| 68 | * @param View $viewName |
||
| 69 | * @param $params The parameters to pass to the view |
||
| 70 | */ |
||
| 71 | public abstract function renderContent($initialControllerInstance,$viewName, $params=NULL); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Collect url parts from the request dispatcher : controllerName, actionName, parameters |
||
| 75 | * @param mixed $dispatcher |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public abstract function fromDispatcher($dispatcher); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * |
||
| 82 | * @param JqueryUI $ui |
||
| 83 | * @return \Ajax\JqueryUI |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function ui($ui=NULL) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * |
||
| 102 | * @param Bootstrap $bootstrap |
||
| 103 | * @return \Ajax\Bootstrap |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function bootstrap($bootstrap=NULL) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * |
||
| 122 | * @param Semantic $semantic |
||
| 123 | * @return \Ajax\Semantic |
||
| 124 | */ |
||
| 125 | public function semantic($semantic=NULL) { |
||
| 139 | |||
| 140 | protected function conflict() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * |
||
| 146 | * @param \Ajax\config\Config $config |
||
| 147 | * @return \Ajax\config\Config |
||
| 148 | */ |
||
| 149 | public function config($config=NULL) { |
||
| 161 | |||
| 162 | public function __construct($params=array()) { |
||
| 176 | |||
| 177 | public function __set($property, $value){ |
||
| 192 | |||
| 193 | public function addToCompile($jsScript) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Outputs the called javascript to the screen |
||
| 199 | * |
||
| 200 | * @param string $js code to output |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function output($js) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Document ready method |
||
| 209 | * |
||
| 210 | * @param string $js code to execute |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function ready($js) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * gather together all script needing to be output |
||
| 219 | * |
||
| 220 | * @param View $view |
||
| 221 | * @param $view_var |
||
| 222 | * @param $script_tags |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function compile($view=NULL, $view_var='script_foot', $script_tags=TRUE) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Clears any previous javascript collected for output |
||
| 239 | * |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function clear_compile() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Outputs a <script> tag |
||
| 248 | * |
||
| 249 | * @param string $script |
||
| 250 | * @param boolean $cdata If a CDATA section should be added |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function inline($script, $cdata=TRUE) { |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Outputs an opening <script> |
||
| 262 | * |
||
| 263 | * @param string $src |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | private function _open_script($src='') { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Outputs an closing </script> |
||
| 274 | * |
||
| 275 | * @param string $extra |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | private function _close_script($extra="\n") { |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Can be passed a database result or associative array and returns a JSON formatted string |
||
| 285 | * |
||
| 286 | * @param mixed $result result set or array |
||
| 287 | * @param bool $match_array_type match array types (defaults to objects) |
||
| 288 | * @return string json formatted string |
||
| 289 | */ |
||
| 290 | public function generate_json($result=NULL, $match_array_type=FALSE) { |
||
| 306 | |||
| 307 | private function _create_json($json_result, $match_array_type) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Checks for an associative array |
||
| 328 | * |
||
| 329 | * @param type |
||
| 330 | * @return type |
||
| 331 | */ |
||
| 332 | public function _is_associative_array($arr) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Ensures a standard json value and escapes values |
||
| 343 | * |
||
| 344 | * @param type |
||
| 345 | * @return type |
||
| 346 | */ |
||
| 347 | public function _prep_args($result, $is_key=FALSE) { |
||
| 362 | |||
| 363 | public function getCDNs() { |
||
| 366 | |||
| 367 | public function setCDNs($cdns) { |
||
| 375 | |||
| 376 | public function genCDNs($template=NULL) { |
||
| 417 | } |
||
| 418 |