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 Asset 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 Asset, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Anomaly\Streams\Platform\Asset; |
||
| 37 | class Asset |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The public base directory. |
||
| 42 | * |
||
| 43 | * @var null |
||
| 44 | */ |
||
| 45 | protected $directory = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Groups of assets. Groups can |
||
| 49 | * be single files as well. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $collections = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The URL generator. |
||
| 57 | * |
||
| 58 | * @var UrlGenerator |
||
| 59 | */ |
||
| 60 | protected $url; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The HTML utility. |
||
| 64 | * |
||
| 65 | * @var HtmlBuilder |
||
| 66 | */ |
||
| 67 | protected $html; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The files system. |
||
| 71 | * |
||
| 72 | * @var Filesystem |
||
| 73 | */ |
||
| 74 | protected $files; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Asset path hints by namespace. |
||
| 78 | * |
||
| 79 | * 'module.users' => 'the/resources/path' |
||
| 80 | * |
||
| 81 | * @var AssetPaths |
||
| 82 | */ |
||
| 83 | protected $paths; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The asset parser utility. |
||
| 87 | * |
||
| 88 | * @var AssetParser |
||
| 89 | */ |
||
| 90 | protected $parser; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The theme collection. |
||
| 94 | * |
||
| 95 | * @var ThemeCollection |
||
| 96 | */ |
||
| 97 | protected $themes; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The mount manager. |
||
| 101 | * |
||
| 102 | * @var MountManager |
||
| 103 | */ |
||
| 104 | protected $manager; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The HTTP request. |
||
| 108 | * |
||
| 109 | * @var Request |
||
| 110 | */ |
||
| 111 | protected $request; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The stream application. |
||
| 115 | * |
||
| 116 | * @var Application |
||
| 117 | */ |
||
| 118 | protected $application; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The config repository. |
||
| 122 | * |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | protected $config; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Create a new Application instance. |
||
| 129 | * |
||
| 130 | * @param Application $application |
||
| 131 | * @param ThemeCollection $themes |
||
| 132 | * @param MountManager $manager |
||
| 133 | * @param AssetParser $parser |
||
| 134 | * @param Repository $config |
||
| 135 | * @param Filesystem $files |
||
| 136 | * @param AssetPaths $paths |
||
| 137 | * @param Request $request |
||
| 138 | * @param HtmlBuilder $html |
||
| 139 | * @param UrlGenerator $url |
||
| 140 | */ |
||
| 141 | public function __construct( |
||
| 142 | Application $application, |
||
| 143 | ThemeCollection $themes, |
||
| 144 | MountManager $manager, |
||
| 145 | AssetParser $parser, |
||
| 146 | Repository $config, |
||
| 147 | Filesystem $files, |
||
| 148 | AssetPaths $paths, |
||
| 149 | Request $request, |
||
|
|
|||
| 150 | HtmlBuilder $html, |
||
| 151 | UrlGenerator $url |
||
| 152 | ) { |
||
| 153 | $this->url = $url; |
||
| 154 | $this->html = $html; |
||
| 155 | $this->files = $files; |
||
| 156 | $this->paths = $paths; |
||
| 157 | $this->config = $config; |
||
| 158 | $this->themes = $themes; |
||
| 159 | $this->parser = $parser; |
||
| 160 | $this->manager = $manager; |
||
| 161 | $this->request = $request; |
||
| 162 | $this->application = $application; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add an asset or glob pattern to an asset collection. |
||
| 167 | * |
||
| 168 | * This should support the asset being the collection |
||
| 169 | * and the asset (for single files) internally |
||
| 170 | * so asset.links / asset.scripts will work. |
||
| 171 | * |
||
| 172 | * @param $collection |
||
| 173 | * @param $file |
||
| 174 | * @param array $filters |
||
| 175 | * @return $this |
||
| 176 | * @throws \Exception |
||
| 177 | */ |
||
| 178 | public function add($collection, $file, array $filters = []) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Download a file and return it's path. |
||
| 217 | * |
||
| 218 | * @param $url |
||
| 219 | * @param null $path |
||
| 220 | * @return null|string |
||
| 221 | */ |
||
| 222 | public function download($url, $path = null) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return the contents of a collection. |
||
| 240 | * |
||
| 241 | * @param $collection |
||
| 242 | * @param array $filters |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function inline($collection, array $filters = []) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Return the URL to a compiled asset collection. |
||
| 252 | * |
||
| 253 | * @param $collection |
||
| 254 | * @param array $filters |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function url($collection, array $filters = [], array $parameters = [], $secure = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return the path to a compiled asset collection. |
||
| 272 | * |
||
| 273 | * @param $collection |
||
| 274 | * @param array $filters |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function path($collection, array $filters = []) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Return the script tag for a collection. |
||
| 288 | * |
||
| 289 | * @param $collection |
||
| 290 | * @param array $filters |
||
| 291 | * @param array $attributes |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function script($collection, array $filters = [], array $attributes = []) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Return the style tag for a collection. |
||
| 303 | * |
||
| 304 | * @param $collection |
||
| 305 | * @param array $filters |
||
| 306 | * @param array $attributes |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | View Code Duplication | public function style($collection, array $filters = [], array $attributes = []) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Return an array of script tags. |
||
| 322 | * |
||
| 323 | * @param $collection |
||
| 324 | * @param array $filters |
||
| 325 | * @param array $attributes |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public function scripts($collection, array $filters = [], array $attributes = []) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Return an array of style tags. |
||
| 343 | * |
||
| 344 | * @param $collection |
||
| 345 | * @param array $filters |
||
| 346 | * @param array $attributes |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | View Code Duplication | public function styles($collection, array $filters = [], array $attributes = []) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Return an array of paths to an asset collection. |
||
| 368 | * |
||
| 369 | * This instead of combining the collection contents |
||
| 370 | * just returns an array of individual processed |
||
| 371 | * paths instead. |
||
| 372 | * |
||
| 373 | * @param $collection |
||
| 374 | * @param array $additionalFilters |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | public function paths($collection, array $additionalFilters = []) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Return an array of style URLs. |
||
| 399 | * |
||
| 400 | * @param $collection |
||
| 401 | * @param array $filters |
||
| 402 | * @param array $attributes |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | public function urls($collection, array $filters = [], array $attributes = []) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param $collection |
||
| 417 | * @param $filters |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | protected function getPath($collection, $filters) |
||
| 421 | { |
||
| 422 | /** |
||
| 423 | * If the asset is remote just return it. |
||
| 424 | */ |
||
| 425 | if (starts_with($collection, ['http', '//'])) { |
||
| 426 | return $collection; |
||
| 427 | } |
||
| 428 | |||
| 429 | $path = $this->paths->outputPath($collection); |
||
| 430 | |||
| 431 | if ($this->shouldPublish($path, $collection, $filters)) { |
||
| 432 | $this->publish($path, $collection, $filters); |
||
| 433 | } |
||
| 434 | |||
| 435 | /*if (file_exists($path) && filesize($path) == 0) { |
||
| 436 | return null; |
||
| 437 | }*/ |
||
| 438 | |||
| 439 | return $this->paths->prefix() . $path; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Return the collection path. This |
||
| 444 | * is primarily used to determine paths |
||
| 445 | * to single assets. |
||
| 446 | * |
||
| 447 | * @param $collection |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | public function getCollectionPath($collection) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Publish the collection of assets to the path. |
||
| 460 | * |
||
| 461 | * @param $path |
||
| 462 | * @param $collection |
||
| 463 | * @param $additionalFilters |
||
| 464 | */ |
||
| 465 | protected function publish($path, $collection, $additionalFilters) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Transform an array of filters to |
||
| 488 | * an array of Assetic filters. |
||
| 489 | * |
||
| 490 | * @param $filters |
||
| 491 | * @param $hint |
||
| 492 | * @return mixed |
||
| 493 | */ |
||
| 494 | protected function transformFilters($filters, $hint) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Add filters that we can assume based |
||
| 623 | * on the asset's file name. |
||
| 624 | * |
||
| 625 | * @param $file |
||
| 626 | * @param $filters |
||
| 627 | * @return array |
||
| 628 | */ |
||
| 629 | protected function addConvenientFilters($file, $filters) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Decide whether we need to publish the file |
||
| 652 | * to the path or not. |
||
| 653 | * |
||
| 654 | * @param $path |
||
| 655 | * @param $collection |
||
| 656 | * @param array $filters |
||
| 657 | * @return bool |
||
| 658 | */ |
||
| 659 | protected function shouldPublish($path, $collection, array $filters = []) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Add a namespace path hint. |
||
| 708 | * |
||
| 709 | * @param $namespace |
||
| 710 | * @param $path |
||
| 711 | * @return $this |
||
| 712 | */ |
||
| 713 | public function addPath($namespace, $path) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Set the public base directory. |
||
| 722 | * |
||
| 723 | * @param $directory |
||
| 724 | * @return $this |
||
| 725 | */ |
||
| 726 | public function setDirectory($directory) |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Create asset collection from collection array |
||
| 735 | * |
||
| 736 | * @param $collection |
||
| 737 | * @param array $additionalFilters |
||
| 738 | * @return AssetCollection |
||
| 739 | */ |
||
| 740 | private function getAssetCollection($collection, $additionalFilters = array()) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Return the filters used in a collection. |
||
| 769 | * |
||
| 770 | * @param $collection |
||
| 771 | * @param array $filters |
||
| 772 | * @return array |
||
| 773 | */ |
||
| 774 | protected function collectionFilters($collection, array $filters = []) |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Return nothing. |
||
| 783 | * |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | function __toString() |
||
| 790 | } |
||
| 791 |