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 | ||
| 25 | abstract class JsUtils{ | ||
| 26 | use JsUtilsEventsTrait,JsUtilsActionsTrait,JsUtilsAjaxTrait; | ||
| 27 | |||
| 28 | protected $js; | ||
| 29 | protected $cdns; | ||
| 30 | /** | ||
| 31 | * | ||
| 32 | * @var JqueryUI | ||
| 33 | */ | ||
| 34 | protected $_ui; | ||
| 35 | /** | ||
| 36 | * | ||
| 37 | * @var Bootstrap | ||
| 38 | */ | ||
| 39 | protected $_bootstrap; | ||
| 40 | |||
| 41 | /** | ||
| 42 | * | ||
| 43 | * @var Semantic | ||
| 44 | */ | ||
| 45 | protected $_semantic; | ||
| 46 | /** | ||
| 47 | * | ||
| 48 | * @var Config | ||
| 49 | */ | ||
| 50 | protected $config; | ||
| 51 | |||
| 52 | 	protected function _setDi($di) { | ||
| 56 | |||
| 57 | public abstract function getUrl($url); | ||
| 59 | /** | ||
| 60 | * render the content of $controller::$action and set the response to the modal content | ||
| 61 | * @param Controller $initialController | ||
| 62 | * @param string $controller a Phalcon controller | ||
| 63 | * @param string $action a Phalcon action | ||
| 64 | */ | ||
| 65 | public abstract function forward($initialController,$controller,$action); | ||
| 66 | /** | ||
| 67 | * render the content of an existing view : $controller/$action and set the response to the modal content | ||
| 68 | * @param View $view | ||
| 69 | * @param string $controller a Phalcon controller | ||
| 70 | * @param string $action a Phalcon action | ||
| 71 | * @param $params The parameters to pass to the view | ||
| 72 | */ | ||
| 73 | public abstract function renderContent($view, $controller, $action, $params=NULL); | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Collect url parts from the request dispatcher : controllerName, actionName, parameters | ||
| 77 | * @param mixed $dispatcher | ||
| 78 | * @return array | ||
| 79 | */ | ||
| 80 | public abstract function fromDispatcher($dispatcher); | ||
| 81 | |||
| 82 | /** | ||
| 83 | * | ||
| 84 | * @param JqueryUI $ui | ||
| 85 | * @return \Ajax\JqueryUI | ||
| 86 | */ | ||
| 87 | View Code Duplication | 	public function ui($ui=NULL) { | |
| 101 | |||
| 102 | /** | ||
| 103 | * | ||
| 104 | * @param Bootstrap $bootstrap | ||
| 105 | * @return \Ajax\Bootstrap | ||
| 106 | */ | ||
| 107 | View Code Duplication | 	public function bootstrap($bootstrap=NULL) { | |
| 121 | |||
| 122 | /** | ||
| 123 | * | ||
| 124 | * @param Semantic $semantic | ||
| 125 | * @return \Ajax\Semantic | ||
| 126 | */ | ||
| 127 | 	public function semantic($semantic=NULL) { | ||
| 141 | |||
| 142 | 	protected function conflict() { | ||
| 145 | |||
| 146 | /** | ||
| 147 | * | ||
| 148 | * @param \Ajax\config\Config $config | ||
| 149 | * @return \Ajax\config\Config | ||
| 150 | */ | ||
| 151 | 	public function config($config=NULL) { | ||
| 163 | |||
| 164 | 	public function __construct($params=array()) { | ||
| 178 | |||
| 179 | 	public function addToCompile($jsScript) { | ||
| 182 | |||
| 183 | /** | ||
| 184 | * Outputs the called javascript to the screen | ||
| 185 | * | ||
| 186 | * @param string $js code to output | ||
| 187 | * @return string | ||
| 188 | */ | ||
| 189 | 	public function output($js) { | ||
| 192 | |||
| 193 | /** | ||
| 194 | * Document ready method | ||
| 195 | * | ||
| 196 | * @param string $js code to execute | ||
| 197 | * @return string | ||
| 198 | */ | ||
| 199 | 	public function ready($js) { | ||
| 202 | |||
| 203 | /** | ||
| 204 | * gather together all script needing to be output | ||
| 205 | * | ||
| 206 | * @param View $view | ||
| 207 | * @param $view_var | ||
| 208 | * @param $script_tags | ||
| 209 | * @return string | ||
| 210 | */ | ||
| 211 | 	public function compile($view=NULL, $view_var='script_foot', $script_tags=TRUE) { | ||
| 222 | |||
| 223 | /** | ||
| 224 | * Clears any previous javascript collected for output | ||
| 225 | * | ||
| 226 | * @return void | ||
| 227 | */ | ||
| 228 | 	public function clear_compile() { | ||
| 231 | |||
| 232 | /** | ||
| 233 | * Outputs a <script> tag | ||
| 234 | * | ||
| 235 | * @param string $script | ||
| 236 | * @param boolean $cdata If a CDATA section should be added | ||
| 237 | * @return string | ||
| 238 | */ | ||
| 239 | View Code Duplication | 	public function inline($script, $cdata=TRUE) { | |
| 245 | |||
| 246 | /** | ||
| 247 | * Outputs an opening <script> | ||
| 248 | * | ||
| 249 | * @param string $src | ||
| 250 | * @return string | ||
| 251 | */ | ||
| 252 | 	private function _open_script($src='') { | ||
| 257 | |||
| 258 | /** | ||
| 259 | * Outputs an closing </script> | ||
| 260 | * | ||
| 261 | * @param string $extra | ||
| 262 | * @return string | ||
| 263 | */ | ||
| 264 | 	private function _close_script($extra="\n") { | ||
| 267 | |||
| 268 | |||
| 269 | /** | ||
| 270 | * Can be passed a database result or associative array and returns a JSON formatted string | ||
| 271 | * | ||
| 272 | * @param mixed $result result set or array | ||
| 273 | * @param bool $match_array_type match array types (defaults to objects) | ||
| 274 | * @return string json formatted string | ||
| 275 | */ | ||
| 276 | 	public function generate_json($result=NULL, $match_array_type=FALSE) { | ||
| 292 | |||
| 293 | 	private function _create_json($json_result, $match_array_type) { | ||
| 311 | |||
| 312 | /** | ||
| 313 | * Checks for an associative array | ||
| 314 | * | ||
| 315 | * @param type | ||
| 316 | * @return type | ||
| 317 | */ | ||
| 318 | 	public function _is_associative_array($arr) { | ||
| 326 | |||
| 327 | /** | ||
| 328 | * Ensures a standard json value and escapes values | ||
| 329 | * | ||
| 330 | * @param type | ||
| 331 | * @return type | ||
| 332 | */ | ||
| 333 | 	public function _prep_args($result, $is_key=FALSE) { | ||
| 348 | |||
| 349 | 	public function getCDNs() { | ||
| 352 | |||
| 353 | 	public function setCDNs($cdns) { | ||
| 361 | |||
| 362 | 	public function genCDNs($template=NULL) { | ||
| 403 | } | ||
| 404 |