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 | protected $params; |
||
| 30 | protected $injected; |
||
| 31 | /** |
||
| 32 | * |
||
| 33 | * @var JqueryUI |
||
| 34 | */ |
||
| 35 | protected $_ui; |
||
| 36 | /** |
||
| 37 | * |
||
| 38 | * @var Bootstrap |
||
| 39 | */ |
||
| 40 | protected $_bootstrap; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * |
||
| 44 | * @var Semantic |
||
| 45 | */ |
||
| 46 | protected $_semantic; |
||
| 47 | /** |
||
| 48 | * |
||
| 49 | * @var Config |
||
| 50 | */ |
||
| 51 | protected $config; |
||
| 52 | |||
| 53 | protected function _setDi($di) { |
||
| 57 | |||
| 58 | public abstract function getUrl($url); |
||
| 61 | /** |
||
| 62 | * render the content of $controller::$action and set the response to the modal content |
||
| 63 | * @param Controller $initialController |
||
| 64 | * @param string $controller a Phalcon controller |
||
| 65 | * @param string $action a Phalcon action |
||
| 66 | * @param array $params |
||
| 67 | */ |
||
| 68 | public abstract function forward($initialController,$controller,$action,$params); |
||
| 69 | /** |
||
| 70 | * render the content of an existing view : $viewName and set the response to the modal content |
||
| 71 | * @param Controller $initialControllerInstance |
||
| 72 | * @param View $viewName |
||
| 73 | * @param $params The parameters to pass to the view |
||
| 74 | */ |
||
| 75 | public abstract function renderContent($initialControllerInstance,$viewName, $params=NULL); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Collect url parts from the request dispatcher : controllerName, actionName, parameters |
||
| 79 | * @param mixed $dispatcher |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public abstract function fromDispatcher($dispatcher); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | * @param JqueryUI $ui |
||
| 87 | * @return \Ajax\JqueryUI |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function ui($ui=NULL) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * |
||
| 106 | * @param Bootstrap $bootstrap |
||
| 107 | * @return \Ajax\Bootstrap |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function bootstrap($bootstrap=NULL) { |
|
| 123 | |||
| 124 | /** |
||
| 125 | * |
||
| 126 | * @param Semantic $semantic |
||
| 127 | * @return \Ajax\Semantic |
||
| 128 | */ |
||
| 129 | public function semantic($semantic=NULL) { |
||
| 143 | |||
| 144 | protected function conflict() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * |
||
| 150 | * @param \Ajax\config\Config $config |
||
| 151 | * @return \Ajax\config\Config |
||
| 152 | */ |
||
| 153 | public function config($config=NULL) { |
||
| 165 | |||
| 166 | public function __construct($params=array(),$injected=NULL) { |
||
| 185 | |||
| 186 | public function __set($property, $value){ |
||
| 201 | |||
| 202 | public function getParam($key){ |
||
| 205 | |||
| 206 | public function addToCompile($jsScript) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Outputs the called javascript to the screen |
||
| 212 | * |
||
| 213 | * @param string $js code to output |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function output($js) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Document ready method |
||
| 222 | * |
||
| 223 | * @param string $js code to execute |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function ready($js) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * gather together all script needing to be output |
||
| 232 | * |
||
| 233 | * @param View $view |
||
| 234 | * @param $view_var |
||
| 235 | * @param $script_tags |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Clears any previous javascript collected for output |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | public function clear_compile() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Outputs a <script> tag |
||
| 261 | * |
||
| 262 | * @param string $script |
||
| 263 | * @param boolean $cdata If a CDATA section should be added |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function inline($script, $cdata=TRUE) { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Outputs an opening <script> |
||
| 275 | * |
||
| 276 | * @param string $src |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | private function _open_script($src='') { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Outputs an closing </script> |
||
| 287 | * |
||
| 288 | * @param string $extra |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | private function _close_script($extra="\n") { |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Can be passed a database result or associative array and returns a JSON formatted string |
||
| 298 | * |
||
| 299 | * @param mixed $result result set or array |
||
| 300 | * @param bool $match_array_type match array types (defaults to objects) |
||
| 301 | * @return string json formatted string |
||
| 302 | */ |
||
| 303 | public function generate_json($result=NULL, $match_array_type=FALSE) { |
||
| 319 | |||
| 320 | private function _create_json($json_result, $match_array_type) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Checks for an associative array |
||
| 341 | * |
||
| 342 | * @param type |
||
| 343 | * @return type |
||
| 344 | */ |
||
| 345 | public function _is_associative_array($arr) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Ensures a standard json value and escapes values |
||
| 356 | * |
||
| 357 | * @param type |
||
| 358 | * @return type |
||
| 359 | */ |
||
| 360 | public function _prep_args($result, $is_key=FALSE) { |
||
| 375 | |||
| 376 | public function getCDNs() { |
||
| 379 | |||
| 380 | public function setCDNs($cdns) { |
||
| 388 | |||
| 389 | public function genCDNs($template=NULL) { |
||
| 430 | |||
| 431 | public function getInjected() { |
||
| 434 | |||
| 435 | } |
||
| 436 |