| Total Complexity | 40 |
| Total Lines | 267 |
| Duplicated Lines | 0 % |
| Changes | 18 | ||
| Bugs | 2 | Features | 0 |
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.
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 |
||
| 20 | class Asset implements \ArrayAccess |
||
| 21 | { |
||
| 22 | /** @var Builder */ |
||
| 23 | protected $builder; |
||
| 24 | /** @var Config */ |
||
| 25 | protected $config; |
||
| 26 | /** @var bool */ |
||
| 27 | /** @var array */ |
||
| 28 | protected $data = []; |
||
| 29 | protected $versioned = false; |
||
| 30 | /** @var bool */ |
||
| 31 | protected $compiled = false; |
||
| 32 | /** @var bool */ |
||
| 33 | protected $minified = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Creates an Asset from file. |
||
| 37 | * |
||
| 38 | * $options[ |
||
| 39 | * 'minify' => true, |
||
| 40 | * 'version' => true, |
||
| 41 | * 'attributes' => ['title' => 'Titre'], |
||
| 42 | * ]; |
||
| 43 | * |
||
| 44 | * @param Builder $builder |
||
| 45 | * @param string $path |
||
| 46 | * @param array|null $options |
||
| 47 | */ |
||
| 48 | public function __construct(Builder $builder, string $path, array $options = null) |
||
| 49 | { |
||
| 50 | $this->builder = $builder; |
||
| 51 | $this->config = $builder->getConfig(); |
||
| 52 | $path = '/'.ltrim($path, '/'); |
||
| 53 | |||
| 54 | if (false === $filePath = $this->findFile($path)) { |
||
| 55 | throw new Exception(sprintf('Asset file "%s" doesn\'t exist.', $path)); |
||
| 56 | } |
||
| 57 | |||
| 58 | $pathinfo = pathinfo($path); |
||
| 59 | |||
| 60 | // handles options |
||
| 61 | $minify = (bool) $this->config->get('assets.minify.auto'); |
||
| 62 | $version = (bool) $this->config->get('assets.version.auto'); |
||
| 63 | $attributes = null; |
||
| 64 | extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
||
| 65 | |||
| 66 | // set data |
||
| 67 | $this->data['file'] = $filePath; |
||
| 68 | $this->data['path'] = $path; |
||
| 69 | $this->data['ext'] = $pathinfo['extension']; |
||
| 70 | $this->data['type'] = explode('/', mime_content_type($filePath))[0]; |
||
| 71 | $this->data['source'] = file_get_contents($filePath); |
||
| 72 | $this->data['content'] = $this->data['source']; |
||
| 73 | $this->data['attributes'] = $attributes; |
||
| 74 | |||
| 75 | // versionning |
||
| 76 | if ($version) { |
||
| 77 | $this->version(); |
||
| 78 | } |
||
| 79 | // compiling |
||
| 80 | if ((bool) $this->config->get('assets.sass.auto')) { |
||
| 81 | $this->compile(); |
||
| 82 | } |
||
| 83 | // minifying |
||
| 84 | if ($minify) { |
||
| 85 | $this->minify(); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns Asset path. |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function __toString(): string |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Versions a file. |
||
| 101 | * |
||
| 102 | * @return self |
||
| 103 | */ |
||
| 104 | public function version(): self |
||
| 105 | { |
||
| 106 | if ($this->versioned) { |
||
| 107 | return $this; |
||
| 108 | } |
||
| 109 | |||
| 110 | switch ($this->config->get('assets.version.strategy')) { |
||
| 111 | case 'static': |
||
| 112 | $version = $this->config->get('assets.version.value'); |
||
| 113 | break; |
||
| 114 | case 'buildtime': |
||
| 115 | $version = $this->builder->time; |
||
| 116 | break; |
||
| 117 | case 'today': |
||
| 118 | default: |
||
| 119 | $version = date('Ymd'); |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($this->config->get('assets.version.strategy') == 'static') { |
||
| 124 | $version = $this->config->get('assets.version.value'); |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->data['path'] = preg_replace('/'.$this->data['ext'].'$/m', "$version.".$this->data['ext'], $this->data['path']); |
||
| 128 | |||
| 129 | $this->versioned = true; |
||
| 130 | |||
| 131 | return $this; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Compiles a SCSS. |
||
| 136 | * |
||
| 137 | * @return self |
||
| 138 | */ |
||
| 139 | public function compile(): self |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Minifying a CSS or a JS. |
||
| 178 | * |
||
| 179 | * @return self |
||
| 180 | */ |
||
| 181 | public function minify(): self |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Implements \ArrayAccess. |
||
| 219 | */ |
||
| 220 | public function offsetSet($offset, $value) |
||
| 221 | { |
||
| 222 | if (!is_null($offset)) { |
||
| 223 | $this->data[$offset] = $value; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Implements \ArrayAccess. |
||
| 229 | */ |
||
| 230 | public function offsetExists($offset) |
||
| 231 | { |
||
| 232 | return isset($this->data[$offset]); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Implements \ArrayAccess. |
||
| 237 | */ |
||
| 238 | public function offsetUnset($offset) |
||
| 239 | { |
||
| 240 | unset($this->data[$offset]); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Implements \ArrayAccess. |
||
| 245 | */ |
||
| 246 | public function offsetGet($offset) |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Saves file. |
||
| 253 | * |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | public function save(): void |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Try to find a static file (in site or theme(s)) if exists or returns false. |
||
| 266 | * |
||
| 267 | * @param string $path |
||
| 268 | * |
||
| 269 | * @return string|false |
||
| 270 | */ |
||
| 271 | private function findFile(string $path) |
||
| 289 |