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 Assets 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 Assets, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Assets |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | private $debug = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array of default filter strings - may be overridden by prefs |
||
| 46 | */ |
||
| 47 | private $default_filters = array( |
||
| 48 | 'css' => 'cssimport,cssembed,?cssmin', |
||
| 49 | 'js' => '?jsqueeze', |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array of output locations in assets directory |
||
| 54 | */ |
||
| 55 | private $default_output = array( |
||
| 56 | 'css' => 'css/*.css', |
||
| 57 | 'js' => 'js/*.js', |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array of asset reference definitions - may be overridden by prefs |
||
| 62 | */ |
||
| 63 | private $default_asset_refs = array( |
||
| 64 | array( |
||
| 65 | 'name' => 'jquery', |
||
| 66 | 'assets' => array('media/jquery/jquery.js'), |
||
| 67 | 'filters' => null, |
||
| 68 | ), |
||
| 69 | array( |
||
| 70 | 'name' => 'jqueryui', |
||
| 71 | 'assets' => array('media/jquery/ui/jquery-ui.js'), |
||
| 72 | 'filters' => null, |
||
| 73 | ), |
||
| 74 | array( |
||
| 75 | 'name' => 'jgrowl', |
||
| 76 | 'assets' => array('media/jquery/plugins/jquery.jgrowl.js'), |
||
| 77 | 'filters' => null, |
||
| 78 | ), |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var AssetManager |
||
| 83 | */ |
||
| 84 | private $assetManager = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string config file with assets prefs |
||
| 88 | */ |
||
| 89 | private $assetsPrefsFilename = 'var/configs/system_assets_prefs.yml'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string config cache key |
||
| 93 | */ |
||
| 94 | private $assetsPrefsCacheKey = 'system/assets/prefs'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string string to identify Assetic filters using instanceof |
||
| 98 | */ |
||
| 99 | |||
| 100 | private $filterInterface = '\Assetic\Filter\FilterInterface'; |
||
| 101 | /** |
||
| 102 | * __construct |
||
| 103 | */ |
||
| 104 | 3 | public function __construct() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * readAssetsPrefs - read configured asset preferences |
||
| 119 | * |
||
| 120 | * @return array of assets preferences |
||
| 121 | */ |
||
| 122 | 3 | protected function readAssetsPrefs() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * saveAssetsPrefs - record array of assets preferences in config file, and |
||
| 168 | * update cache |
||
| 169 | * |
||
| 170 | * @param array $assets_prefs array of asset preferences to save |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | 1 | View Code Duplication | protected function saveAssetsPrefs($assets_prefs) |
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * getUrlToAssets |
||
| 190 | * |
||
| 191 | * Create an asset file from a list of assets |
||
| 192 | * |
||
| 193 | * @param string $type type of asset, css or js |
||
| 194 | * @param array $assets list of source files to process |
||
| 195 | * @param string|array $filters either a comma separated list of known namsed filters |
||
| 196 | * or an array of named filters and/or filter object |
||
| 197 | * @param string $target target path, will default to assets directory |
||
| 198 | * |
||
| 199 | * @return string URL to asset file |
||
| 200 | */ |
||
| 201 | 1 | public function getUrlToAssets($type, $assets, $filters = 'default', $target = null) |
|
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * setDebug enable debug mode, will skip filters prefixed with '?' |
||
| 326 | * |
||
| 327 | * @return true |
||
| 328 | */ |
||
| 329 | 1 | public function setDebug() |
|
| 330 | { |
||
| 331 | 1 | $this->debug = true; |
|
| 332 | 1 | return true; |
|
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Add an asset reference to the asset manager |
||
| 337 | * |
||
| 338 | * @param string $name the name of the reference to be added |
||
| 339 | * @param mixed $assets a string asset path, or an array of asset paths, |
||
| 340 | * may include wildcard |
||
| 341 | * @param string|array $filters either a comma separated list of known named filters |
||
| 342 | * or an array of named filters and/or filter object |
||
| 343 | * |
||
| 344 | * @return boolean true if asset registers, false on error |
||
| 345 | */ |
||
| 346 | 3 | public function registerAssetReference($name, $assets, $filters = null) |
|
| 347 | { |
||
| 348 | 3 | $xoops = \Xoops::getInstance(); |
|
| 349 | |||
| 350 | 3 | $assetArray = array(); |
|
| 351 | 3 | $filterArray = array(); |
|
| 352 | |||
| 353 | try { |
||
| 354 | 3 | if (is_scalar($assets)) { |
|
| 355 | 1 | $assets = array($assets); // just a single path name |
|
| 356 | } |
||
| 357 | 3 | foreach ($assets as $a) { |
|
| 358 | // translate path if not a reference or absolute path |
||
| 359 | 3 | if ((substr_compare($a, '@', 0, 1) != 0) |
|
| 360 | 3 | && (substr_compare($a, '/', 0, 1) != 0)) { |
|
| 361 | 3 | $a = $xoops->path($a); |
|
| 362 | } |
||
| 363 | 3 | if (false===strpos($a, '*')) { |
|
| 364 | 3 | $assetArray[] = new FileAsset($a); // single file |
|
| 365 | } else { |
||
| 366 | 3 | $assetArray[] = new GlobAsset($a); // wild card match |
|
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | 3 | View Code Duplication | if (!is_array($filters)) { |
| 371 | 3 | if (empty($filters)) { |
|
| 372 | 3 | $filters = array(); |
|
| 373 | } else { |
||
| 374 | $filters = explode(',', str_replace(' ', '', $filters)); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | 3 | foreach ($filters as $filter) { |
|
| 378 | if (is_object($filter) && $filter instanceof $this->filterInterface) { |
||
| 379 | $filterArray[] = $filter; |
||
| 380 | } else { |
||
| 381 | switch (ltrim($filter, '?')) { |
||
| 382 | case 'cssembed': |
||
| 383 | $filterArray[] = new Filter\PhpCssEmbedFilter(); |
||
| 384 | break; |
||
| 385 | case 'cssmin': |
||
| 386 | $filterArray[] = new Filter\CssMinFilter(); |
||
| 387 | break; |
||
| 388 | case 'cssimport': |
||
| 389 | $filterArray[] = new Filter\CssImportFilter(); |
||
| 390 | break; |
||
| 391 | case 'cssrewrite': |
||
| 392 | $filterArray[] = new Filter\CssRewriteFilter(); |
||
| 393 | break; |
||
| 394 | case 'lessphp': |
||
| 395 | $filterArray[] = new Filter\LessphpFilter(); |
||
| 396 | break; |
||
| 397 | case 'scssphp': |
||
| 398 | $filterArray[] = new Filter\ScssphpFilter(); |
||
| 399 | break; |
||
| 400 | case 'jsmin': |
||
| 401 | $filterArray[] = new Filter\JSMinFilter(); |
||
| 402 | break; |
||
| 403 | case 'jsqueeze': |
||
| 404 | $filterArray[] = new Filter\JSqueezeFilter(); |
||
| 405 | break; |
||
| 406 | default: |
||
| 407 | throw new \Exception(sprintf('%s filter not implemented.', $filter)); |
||
| 408 | break; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | 3 | $collection = new AssetCollection($assetArray, $filterArray); |
|
| 414 | 3 | $this->assetManager->set($name, $collection); |
|
| 415 | |||
| 416 | 3 | return true; |
|
| 417 | } catch (\Exception $e) { |
||
| 418 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 419 | return false; |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * copyFileAssets - copy files to the appropriate asset directory. |
||
| 425 | * |
||
| 426 | * Copying is normally only needed for fonts or images when they are referenced by a |
||
| 427 | * relative url in stylesheet, or are located outside of the web root. |
||
| 428 | * |
||
| 429 | * @param string $from_path path to files to copy |
||
| 430 | * @param string $pattern glob pattern to match files to be copied |
||
| 431 | * @param string $output output type (css, fonts, images, js) |
||
| 432 | * |
||
| 433 | * @return mixed boolean false if target directory is not writable, otherwise |
||
| 434 | * integer count of files copied |
||
| 435 | */ |
||
| 436 | 1 | public function copyFileAssets($from_path, $pattern, $output) |
|
| 470 | } |
||
| 471 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: