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 assetManager 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 assetManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class assetManager |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var assetManager |
||
| 23 | */ |
||
| 24 | protected static $_BehaviorInstance; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $callMapp; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | * @var MY_Controller |
||
| 34 | */ |
||
| 35 | protected $ci; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $module_js = 'jsLangs'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * |
||
| 44 | * @var Template |
||
| 45 | */ |
||
| 46 | protected $template; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $useCompress = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * assetManager constructor. |
||
| 55 | */ |
||
| 56 | private function __construct() { |
||
| 59 | |||
| 60 | private function __clone() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string|array $item |
||
| 66 | * @param string|integer|float $value |
||
| 67 | * @return assetManager |
||
| 68 | * @access public |
||
| 69 | * @copyright ImageCMS (c) 2013, Roman <[email protected]> |
||
| 70 | */ |
||
| 71 | public function appendData($item, $value) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string|array $item |
||
| 78 | * @param string|integer|float|array|boolean $value |
||
| 79 | * @return assetManager |
||
| 80 | * @access public |
||
| 81 | * @author Kaero |
||
| 82 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 83 | */ |
||
| 84 | public function setData($item, $value = null) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return assetManager |
||
| 96 | * @access public |
||
| 97 | * @author Kaero |
||
| 98 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 99 | */ |
||
| 100 | public static function create() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * fetch admin view |
||
| 108 | * @param string $tpl Template file name |
||
| 109 | * @param boolean $fetchLangsTpl |
||
| 110 | * @return string |
||
| 111 | * @access public |
||
| 112 | * @author Kaero |
||
| 113 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 114 | */ |
||
| 115 | public function fetchAdminTemplate($tpl, $fetchLangsTpl = TRUE) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * fetch public view |
||
| 137 | * @param string $tpl Template file name |
||
| 138 | * @param string $moduleName |
||
| 139 | * @return string |
||
| 140 | * @access public |
||
| 141 | * @author Kaero |
||
| 142 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function fetchTemplate($tpl, $moduleName = null) { |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $item |
||
| 156 | * @return string|integer|float|array|boolean |
||
| 157 | * @access public |
||
| 158 | * @author |
||
| 159 | * @copyright |
||
| 160 | */ |
||
| 161 | public function getData($item) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * |
||
| 167 | * @param string $path |
||
| 168 | * @param string $position |
||
| 169 | * @return assetManager |
||
| 170 | */ |
||
| 171 | public function registerJsFullpath($path, $position = 'after') { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $message |
||
| 178 | * @param string $title |
||
| 179 | * @param string $class |
||
| 180 | */ |
||
| 181 | public function registerJsMessage($message, $title, $class = '') { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @access public |
||
| 188 | * @author a.gula |
||
| 189 | * @param string $script |
||
| 190 | * @param boolean $useCompress |
||
| 191 | * @param string $position after|before |
||
| 192 | * @param string $type |
||
| 193 | * @return assetManager |
||
| 194 | * @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
||
| 195 | */ |
||
| 196 | public function registerJsScript($script, $useCompress = FALSE, $position = 'after', $type = 'text/javascript') { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return assetManager |
||
| 209 | * @access public |
||
| 210 | * @author Kaero |
||
| 211 | * @param string $name |
||
| 212 | * @param boolean $useCompress |
||
| 213 | * @param string $position |
||
| 214 | * @return assetManager |
||
| 215 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 216 | */ |
||
| 217 | public function registerScript($name, $useCompress = FALSE, $position = 'after') { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $js |
||
| 230 | * @return string |
||
| 231 | * @todo compress and cache |
||
| 232 | */ |
||
| 233 | private function compressJs($js) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Return formated path for JS - script files |
||
| 239 | * @param string $fileName |
||
| 240 | * @return string |
||
| 241 | * @access private |
||
| 242 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 243 | */ |
||
| 244 | View Code Duplication | private function buildScriptPath($fileName) { |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $list |
||
| 266 | * @return array |
||
| 267 | * @access public |
||
| 268 | * @author cutter |
||
| 269 | */ |
||
| 270 | private function getTrace($list = 'first_file') { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Checks if file exists in any of modules dirs. If exists returns its path |
||
| 291 | * @param string|array $files example: ['menu/assets/css/style.css'] |
||
| 292 | * @param bool $noExt |
||
| 293 | * @return bool|string returns file path or FALSE |
||
| 294 | */ |
||
| 295 | private function getModuleFilePath($files, $noExt = true) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $name |
||
| 320 | */ |
||
| 321 | public function registerScriptWithoutTemplate($name) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return assetManager |
||
| 328 | * @access public |
||
| 329 | * @author Kaero |
||
| 330 | * @param string $name |
||
| 331 | * @param boolean $useCompress |
||
| 332 | * @return assetManager |
||
| 333 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 334 | */ |
||
| 335 | public function registerStyle($name, $useCompress = FALSE) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Compressing css file |
||
| 354 | * @param string $css text of css file |
||
| 355 | * @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | private function compressCss($css) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Put css string into template |
||
| 366 | * @return assetManager |
||
| 367 | * @access public |
||
| 368 | * @author a.gula |
||
| 369 | * @param string $css |
||
| 370 | * @param boolean $useCompress |
||
| 371 | * @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
||
| 372 | */ |
||
| 373 | public function registerStyleStr($css, $useCompress = FALSE) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $name |
||
| 386 | */ |
||
| 387 | public function registerStyleWithoutTemplate($name) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Return formated path for css |
||
| 394 | * @param string $fileName |
||
| 395 | * @return string |
||
| 396 | * @access private |
||
| 397 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 398 | */ |
||
| 399 | View Code Duplication | private function buildStylePath($fileName) { |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Render public view |
||
| 422 | * @param string $tpl Template file name |
||
| 423 | * @param bool $ignoreWrap |
||
| 424 | * @param bool $fetchJsTpl |
||
| 425 | * @access public |
||
| 426 | * @author Kaero |
||
| 427 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 428 | */ |
||
| 429 | public function render($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Render Admin view |
||
| 435 | * @param string $tpl Template file name |
||
| 436 | * @param bool $ignoreWrap |
||
| 437 | * @param bool $fetchJsTpl |
||
| 438 | * @access public |
||
| 439 | * @author Kaero |
||
| 440 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 441 | */ |
||
| 442 | public function renderAdmin($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * |
||
| 448 | * @param string $tpl |
||
| 449 | * @param boolean $ignoreWrap |
||
| 450 | * @param boolean $fetchJsTpl |
||
| 451 | * @param boolean $admin |
||
| 452 | */ |
||
| 453 | private function _render($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE, $admin = FALSE) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Return formatted path |
||
| 496 | * @access private |
||
| 497 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 498 | * @param string $tpl |
||
| 499 | * @param string $moduleName |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | private function buildTemplatePath($tpl, $moduleName = null) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * |
||
| 524 | * @param string $path |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | protected function makePath($path) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * |
||
| 549 | * @param string $tpl |
||
| 550 | * @param string $moduleName |
||
| 551 | * @param boolean $admin |
||
| 552 | * @return string |
||
| 553 | * @throws Exception |
||
| 554 | */ |
||
| 555 | private function _buildTemplatePath($tpl, $moduleName = null, $admin = FALSE) { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Return formatted path |
||
| 570 | * @param string $fileName |
||
| 571 | * @return string |
||
| 572 | * @access private |
||
| 573 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 574 | */ |
||
| 575 | private function buildAdminTemplatePath($fileName) { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Changing main layout file |
||
| 587 | * @param string $mainLayout |
||
| 588 | * @return assetManager |
||
| 589 | */ |
||
| 590 | View Code Duplication | public function setMainLayout($mainLayout) { |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Changing main layout file by full path |
||
| 602 | * @param string $mainLayout |
||
| 603 | * @return assetManager |
||
| 604 | */ |
||
| 605 | View Code Duplication | public function setMainLayoutByFullPath($mainLayout) { |
|
| 614 | |||
| 615 | } |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: