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 |
||
| 36 | class Assets |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var boolean |
||
| 40 | */ |
||
| 41 | private $debug = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array of default filter strings - may be overridden by prefs |
||
| 45 | */ |
||
| 46 | private $default_filters = array( |
||
| 47 | 'css' => 'cssimport,cssembed,?cssmin', |
||
| 48 | 'js' => '?jsqueeze', |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array of output locations in assets directory |
||
| 53 | */ |
||
| 54 | private $default_output = array( |
||
| 55 | 'css' => 'css/*.css', |
||
| 56 | 'js' => 'js/*.js', |
||
| 57 | ); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array of asset reference definitions - may be overridden by prefs |
||
| 61 | */ |
||
| 62 | private $default_asset_refs = array( |
||
| 63 | array( |
||
| 64 | 'name' => 'jquery', |
||
| 65 | 'assets' => array('media/jquery/jquery.js'), |
||
| 66 | 'filters' => null, |
||
| 67 | ), |
||
| 68 | array( |
||
| 69 | 'name' => 'jqueryui', |
||
| 70 | 'assets' => array('media/jquery/ui/jquery-ui.js'), |
||
| 71 | 'filters' => null, |
||
| 72 | ), |
||
| 73 | array( |
||
| 74 | 'name' => 'jgrowl', |
||
| 75 | 'assets' => array('media/jquery/plugins/jquery.jgrowl.js'), |
||
| 76 | 'filters' => null, |
||
| 77 | ), |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var AssetManager |
||
| 82 | */ |
||
| 83 | private $assetManager = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var string config file with assets prefs |
||
| 87 | */ |
||
| 88 | private $assetsPrefsFilename = 'var/configs/system_assets_prefs.yml'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string config cache key |
||
| 92 | */ |
||
| 93 | private $assetsPrefsCacheKey = 'system/assets/prefs'; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var string string to identify Assetic filters using instanceof |
||
| 97 | */ |
||
| 98 | |||
| 99 | private $filterInterface = '\Assetic\Filter\FilterInterface'; |
||
| 100 | /** |
||
| 101 | * __construct |
||
| 102 | */ |
||
| 103 | 3 | public function __construct() |
|
|
|
|||
| 104 | { |
||
| 105 | 3 | $this->assetManager = new AssetManager(); |
|
| 106 | 3 | if (isset($_REQUEST['ASSET_DEBUG'])) { |
|
| 107 | $this->setDebug(); |
||
| 108 | } |
||
| 109 | 3 | $this->readAssetsPrefs(); |
|
| 110 | // register any asset references |
||
| 111 | 3 | foreach ($this->default_asset_refs as $ref) { |
|
| 112 | 3 | $this->registerAssetReference($ref['name'], $ref['assets'], $ref['filters']); |
|
| 113 | 3 | } |
|
| 114 | 3 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * readAssetsPrefs - read configured asset preferences |
||
| 118 | * |
||
| 119 | * @return array of assets preferences |
||
| 120 | */ |
||
| 121 | 3 | protected function readAssetsPrefs() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * saveAssetsPrefs - record array of assets preferences in config file, and |
||
| 167 | * update cache |
||
| 168 | * |
||
| 169 | * @param array $assets_prefs array of asset preferences to save |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | 1 | View Code Duplication | protected function saveAssetsPrefs($assets_prefs) |
| 174 | { |
||
| 175 | 1 | if (is_array($assets_prefs)) { |
|
| 176 | 1 | $xoops = \Xoops::getInstance(); |
|
| 177 | try { |
||
| 178 | 1 | Yaml::save($assets_prefs, $xoops->path($this->assetsPrefsFilename)); |
|
| 179 | 1 | $xoops->cache()->write($this->assetsPrefsCacheKey, $assets_prefs); |
|
| 180 | 1 | } catch (\Exception $e) { |
|
| 181 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 182 | } |
||
| 183 | 1 | } |
|
| 184 | 1 | } |
|
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * getUrlToAssets |
||
| 189 | * |
||
| 190 | * Create an asset file from a list of assets |
||
| 191 | * |
||
| 192 | * @param string $type type of asset, css or js |
||
| 193 | * @param array $assets list of source files to process |
||
| 194 | * @param string|array $filters either a comma separated list of known namsed filters |
||
| 195 | * or an array of named filters and/or filter object |
||
| 196 | * @param string $target target path, will default to assets directory |
||
| 197 | * |
||
| 198 | * @return string URL to asset file |
||
| 199 | */ |
||
| 200 | public function getUrlToAssets($type, $assets, $filters = 'default', $target = null) |
||
| 201 | { |
||
| 202 | if (is_scalar($assets)) { |
||
| 203 | $assets = array($assets); // just a single path name |
||
| 204 | } |
||
| 205 | |||
| 206 | if ($filters==='default') { |
||
| 207 | if (isset($this->default_filters[$type])) { |
||
| 208 | $filters = $this->default_filters[$type]; |
||
| 209 | } else { |
||
| 210 | $filters = ''; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | View Code Duplication | if (!is_array($filters)) { |
|
| 215 | if (empty($filters)) { |
||
| 216 | $filters = array(); |
||
| 217 | } else { |
||
| 218 | $filters = explode(',', str_replace(' ', '', $filters)); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | if (isset($this->default_output[$type])) { |
||
| 223 | $output = $this->default_output[$type]; |
||
| 224 | } else { |
||
| 225 | $output = ''; |
||
| 226 | } |
||
| 227 | |||
| 228 | $xoops = \Xoops::getInstance(); |
||
| 229 | |||
| 230 | if (isset($target)) { |
||
| 231 | $target_path = $target; |
||
| 232 | } else { |
||
| 233 | $target_path = $xoops->path('assets'); |
||
| 234 | } |
||
| 235 | |||
| 236 | try { |
||
| 237 | $am = $this->assetManager; |
||
| 238 | $fm = new FilterManager(); |
||
| 239 | |||
| 240 | foreach ($filters as $filter) { |
||
| 241 | if (is_object($filter) && $filter instanceof $this->filterInterface) { |
||
| 242 | $filterArray[] = $filter; |
||
| 243 | } else { |
||
| 244 | switch (ltrim($filter, '?')) { |
||
| 245 | case 'cssembed': |
||
| 246 | $fm->set('cssembed', new Filter\PhpCssEmbedFilter()); |
||
| 247 | break; |
||
| 248 | case 'cssmin': |
||
| 249 | $fm->set('cssmin', new Filter\CssMinFilter()); |
||
| 250 | break; |
||
| 251 | case 'cssimport': |
||
| 252 | $fm->set('cssimport', new Filter\CssImportFilter()); |
||
| 253 | break; |
||
| 254 | case 'cssrewrite': |
||
| 255 | $fm->set('cssrewrite', new Filter\CssRewriteFilter()); |
||
| 256 | break; |
||
| 257 | case 'lessphp': |
||
| 258 | $fm->set('lessphp', new Filter\LessphpFilter()); |
||
| 259 | break; |
||
| 260 | case 'scssphp': |
||
| 261 | $fm->set('scssphp', new Filter\ScssphpFilter()); |
||
| 262 | break; |
||
| 263 | case 'jsmin': |
||
| 264 | $fm->set('jsmin', new Filter\JSMinFilter()); |
||
| 265 | break; |
||
| 266 | case 'jsqueeze': |
||
| 267 | $fm->set('jsqueeze', new Filter\JSqueezeFilter()); |
||
| 268 | break; |
||
| 269 | default: |
||
| 270 | throw new \Exception(sprintf('%s filter not implemented.', $filter)); |
||
| 271 | break; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | // Factory setup |
||
| 277 | $factory = new AssetFactory($target_path); |
||
| 278 | $factory->setAssetManager($am); |
||
| 279 | $factory->setFilterManager($fm); |
||
| 280 | $factory->setDefaultOutput($output); |
||
| 281 | $factory->setDebug($this->debug); |
||
| 282 | $factory->addWorker(new CacheBustingWorker()); |
||
| 283 | |||
| 284 | // Prepare the assets writer |
||
| 285 | $writer = new AssetWriter($target_path); |
||
| 286 | |||
| 287 | // Translate asset paths, remove duplicates |
||
| 288 | $translated_assets = array(); |
||
| 289 | foreach ($assets as $k => $v) { |
||
| 290 | // translate path if not a reference or absolute path |
||
| 291 | if (0 == preg_match("/^\\/|^\\\\|^[a-zA-Z]:|^@/", $v)) { |
||
| 292 | $v = $xoops->path($v); |
||
| 293 | } |
||
| 294 | if (!in_array($v, $translated_assets)) { |
||
| 295 | $translated_assets[] = $v; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | // Create the asset |
||
| 300 | $asset = $factory->createAsset( |
||
| 301 | $translated_assets, |
||
| 302 | $filters |
||
| 303 | ); |
||
| 304 | $asset_path = $asset->getTargetPath(); |
||
| 305 | if (!is_readable($target_path . $asset_path)) { |
||
| 306 | $assetKey = 'Asset '.$asset_path; |
||
| 307 | $xoops->events()->triggerEvent('debug.timer.start', $assetKey); |
||
| 308 | $oldumask = umask(0002); |
||
| 309 | $writer->writeAsset($asset); |
||
| 310 | umask($oldumask); |
||
| 311 | $xoops->events()->triggerEvent('debug.timer.stop', $assetKey); |
||
| 312 | } |
||
| 313 | |||
| 314 | return $xoops->url('assets/' . $asset_path); |
||
| 315 | |||
| 316 | } catch (\Exception $e) { |
||
| 317 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 318 | return null; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * setDebug enable debug mode, will skip filters prefixed with '?' |
||
| 325 | * |
||
| 326 | * @return true |
||
| 327 | */ |
||
| 328 | 1 | public function setDebug() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Add an asset reference to the asset manager |
||
| 336 | * |
||
| 337 | * @param string $name the name of the reference to be added |
||
| 338 | * @param mixed $assets a string asset path, or an array of asset paths, |
||
| 339 | * may include wildcard |
||
| 340 | * @param string|array $filters either a comma separated list of known named filters |
||
| 341 | * or an array of named filters and/or filter object |
||
| 342 | * |
||
| 343 | * @return boolean true if asset registers, false on error |
||
| 344 | */ |
||
| 345 | 3 | public function registerAssetReference($name, $assets, $filters = null) |
|
| 346 | { |
||
| 347 | 3 | $xoops = \Xoops::getInstance(); |
|
| 348 | |||
| 349 | 3 | $assetArray = array(); |
|
| 350 | 3 | $filterArray = array(); |
|
| 351 | |||
| 352 | try { |
||
| 353 | 3 | if (is_scalar($assets)) { |
|
| 354 | 1 | $assets = array($assets); // just a single path name |
|
| 355 | 1 | } |
|
| 356 | 3 | foreach ($assets as $a) { |
|
| 357 | // translate path if not a reference or absolute path |
||
| 358 | 3 | if ((substr_compare($a, '@', 0, 1) != 0) |
|
| 359 | 3 | && (substr_compare($a, '/', 0, 1) != 0)) { |
|
| 360 | 3 | $a = $xoops->path($a); |
|
| 361 | 3 | } |
|
| 362 | 3 | if (false===strpos($a, '*')) { |
|
| 363 | 3 | $assetArray[] = new FileAsset($a); // single file |
|
| 364 | 3 | } else { |
|
| 365 | $assetArray[] = new GlobAsset($a); // wild card match |
||
| 366 | } |
||
| 367 | 3 | } |
|
| 368 | |||
| 369 | 3 | View Code Duplication | if (!is_array($filters)) { |
| 370 | 3 | if (empty($filters)) { |
|
| 371 | 3 | $filters = array(); |
|
| 372 | 3 | } else { |
|
| 373 | $filters = explode(',', str_replace(' ', '', $filters)); |
||
| 374 | } |
||
| 375 | 3 | } |
|
| 376 | 3 | foreach ($filters as $filter) { |
|
| 377 | if (is_object($filter) && $filter instanceof $this->filterInterface) { |
||
| 378 | $filterArray[] = $filter; |
||
| 379 | } else { |
||
| 380 | switch (ltrim($filter, '?')) { |
||
| 381 | case 'cssembed': |
||
| 382 | $filterArray[] = new Filter\PhpCssEmbedFilter(); |
||
| 383 | break; |
||
| 384 | case 'cssmin': |
||
| 385 | $filterArray[] = new Filter\CssMinFilter(); |
||
| 386 | break; |
||
| 387 | case 'cssimport': |
||
| 388 | $filterArray[] = new Filter\CssImportFilter(); |
||
| 389 | break; |
||
| 390 | case 'cssrewrite': |
||
| 391 | $filterArray[] = new Filter\CssRewriteFilter(); |
||
| 392 | break; |
||
| 393 | case 'lessphp': |
||
| 394 | $filterArray[] = new Filter\LessphpFilter(); |
||
| 395 | break; |
||
| 396 | case 'scssphp': |
||
| 397 | $filterArray[] = new Filter\ScssphpFilter(); |
||
| 398 | break; |
||
| 399 | case 'jsmin': |
||
| 400 | $filterArray[] = new Filter\JSMinFilter(); |
||
| 401 | break; |
||
| 402 | case 'jsqueeze': |
||
| 403 | $filterArray[] = new Filter\JSqueezeFilter(); |
||
| 404 | break; |
||
| 405 | default: |
||
| 406 | throw new \Exception(sprintf('%s filter not implemented.', $filter)); |
||
| 407 | break; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | 3 | } |
|
| 411 | |||
| 412 | 3 | $collection = new AssetCollection($assetArray, $filterArray); |
|
| 413 | 3 | $this->assetManager->set($name, $collection); |
|
| 414 | |||
| 415 | 3 | return true; |
|
| 416 | } catch (\Exception $e) { |
||
| 417 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 418 | return false; |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * copyFileAssets - copy files to the appropriate asset directory. |
||
| 424 | * |
||
| 425 | * Copying is normally only needed for fonts or images when they are referenced by a |
||
| 426 | * relative url in stylesheet, or are located outside of the web root. |
||
| 427 | * |
||
| 428 | * @param string $from_path path to files to copy |
||
| 429 | * @param string $pattern glob pattern to match files to be copied |
||
| 430 | * @param string $output output type (css, fonts, images, js) |
||
| 431 | * |
||
| 432 | * @return mixed boolean false if target directory is not writable, otherwise |
||
| 433 | * integer count of files copied |
||
| 434 | */ |
||
| 435 | 1 | public function copyFileAssets($from_path, $pattern, $output) |
|
| 469 | } |
||
| 470 |
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: