Complex classes like CSS 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 CSS, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class CSS extends MinifyComponent |
||
| 17 | { |
||
| 18 | |||
| 19 | 9 | public function export() |
|
| 20 | { |
||
| 21 | 9 | $cssFiles = $this->view->cssFiles; |
|
| 22 | |||
| 23 | 9 | $this->view->cssFiles = []; |
|
| 24 | |||
| 25 | 9 | $toMinify = []; |
|
| 26 | |||
| 27 | 9 | foreach ($cssFiles as $file => $html) { |
|
| 28 | 8 | if ($this->thisFileNeedMinify($file, $html)) { |
|
| 29 | 8 | if ($this->view->concatCss) { |
|
| 30 | 8 | $toMinify[$file] = $html; |
|
| 31 | 8 | } else { |
|
| 32 | $this->process([$file => $html]); |
||
| 33 | } |
||
| 34 | 8 | } else { |
|
| 35 | 2 | if (!empty($toMinify)) { |
|
| 36 | 2 | $this->process($toMinify); |
|
| 37 | |||
| 38 | 2 | $toMinify = []; |
|
| 39 | 2 | } |
|
| 40 | |||
| 41 | 2 | $this->view->cssFiles[$file] = $html; |
|
| 42 | } |
||
| 43 | 9 | } |
|
| 44 | |||
| 45 | 9 | if (!empty($toMinify)) { |
|
| 46 | 6 | $this->process($toMinify); |
|
| 47 | 6 | } |
|
| 48 | |||
| 49 | 9 | unset($toMinify); |
|
| 50 | 9 | } |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $files |
||
| 54 | */ |
||
| 55 | 8 | protected function process(array $files) |
|
| 56 | { |
||
| 57 | 8 | $minifyPath = $this->view->minifyPath; |
|
| 58 | 8 | $hash = $this->_getSummaryFilesHash($files); |
|
| 59 | |||
| 60 | 8 | $resultFile = $minifyPath . DIRECTORY_SEPARATOR . $hash . '.css'; |
|
| 61 | |||
| 62 | 8 | if (!file_exists($resultFile)) { |
|
| 63 | 8 | $css = ''; |
|
| 64 | |||
| 65 | 8 | foreach ($files as $file => $html) { |
|
| 66 | 8 | $cacheKey = $this->buildCacheKey($file); |
|
| 67 | |||
| 68 | 8 | $content = $this->getFromCache($cacheKey); |
|
| 69 | |||
| 70 | 8 | if (false !== $content) { |
|
| 71 | $css .= $content; |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | |||
| 75 | 8 | $path = dirname($file); |
|
| 76 | 8 | $file = $this->getAbsoluteFilePath($file); |
|
| 77 | |||
| 78 | 8 | $content = ''; |
|
| 79 | |||
| 80 | 8 | if (!file_exists($file)) { |
|
| 81 | \Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__); |
||
| 82 | 8 | } elseif (!is_readable($file)) { |
|
| 83 | \Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__); |
||
| 84 | } else { |
||
| 85 | 8 | $content = file_get_contents($file); |
|
| 86 | } |
||
| 87 | |||
| 88 | 8 | $result = []; |
|
| 89 | |||
| 90 | 8 | preg_match_all('|url\(([^)]+)\)|i', $content, $m); |
|
| 91 | |||
| 92 | 8 | if (isset($m[0])) { |
|
| 93 | 8 | foreach ((array)$m[0] as $k => $v) { |
|
| 94 | 6 | if (in_array(strpos($m[1][$k], 'data:'), [0, 1], true)) { |
|
| 95 | 6 | continue; |
|
| 96 | } |
||
| 97 | |||
| 98 | 6 | $url = str_replace(['\'', '"'], '', $m[1][$k]); |
|
| 99 | |||
| 100 | 6 | if ($this->isUrl($url)) { |
|
| 101 | 6 | $result[$m[1][$k]] = $url; |
|
| 102 | 6 | } else { |
|
| 103 | 6 | $result[$m[1][$k]] = $path . '/' . $url; |
|
| 104 | } |
||
| 105 | 8 | } |
|
| 106 | |||
| 107 | 8 | $content = strtr($content, $result); |
|
| 108 | 8 | } |
|
| 109 | |||
| 110 | 8 | $this->expandImports($content); |
|
| 111 | |||
| 112 | 8 | $this->convertMediaTypeAttributeToMediaQuery($html, $content); |
|
| 113 | |||
| 114 | 8 | if ($this->view->minifyCss) { |
|
| 115 | 8 | $content = \Minify_CSSmin::minify($content); |
|
| 116 | 8 | } |
|
| 117 | |||
| 118 | 8 | $this->saveToCache($cacheKey, $content); |
|
| 119 | |||
| 120 | 8 | $css .= $content; |
|
| 121 | 8 | } |
|
| 122 | |||
| 123 | 8 | $charsets = $this->collectCharsets($css); |
|
| 124 | 8 | $imports = $this->collectImports($css); |
|
| 125 | 8 | $fonts = $this->collectFonts($css); |
|
| 126 | |||
| 127 | 8 | if (false !== $this->view->forceCharset) { |
|
| 128 | 7 | $charsets = '@charset "' . (string)$this->view->forceCharset . '";' . "\n"; |
|
| 129 | 7 | } |
|
| 130 | |||
| 131 | 8 | file_put_contents($resultFile, $charsets . $imports . $fonts . $css); |
|
| 132 | |||
| 133 | 8 | if (false !== $this->view->fileMode) { |
|
| 134 | 8 | @chmod($resultFile, $this->view->fileMode); |
|
| 135 | 8 | } |
|
| 136 | 8 | } |
|
| 137 | |||
| 138 | 8 | $file = $this->prepareResultFile($resultFile); |
|
| 139 | |||
| 140 | 8 | $this->view->cssFiles[$file] = Html::cssFile($file); |
|
| 141 | 8 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $code |
||
| 145 | */ |
||
| 146 | 8 | protected function expandImports(&$code) |
|
| 147 | { |
||
| 148 | 8 | if (true !== $this->view->expandImports) { |
|
| 149 | 1 | return; |
|
| 150 | } |
||
| 151 | |||
| 152 | 7 | preg_match_all('|\@import\s([^;]+);|i', str_replace('&', '&', $code), $m); |
|
| 153 | |||
| 154 | 7 | if (!isset($m[0])) { |
|
| 155 | return; |
||
| 156 | } |
||
| 157 | |||
| 158 | 7 | foreach ((array)$m[0] as $k => $v) { |
|
| 159 | 5 | $importUrl = $m[1][$k]; |
|
| 160 | |||
| 161 | 5 | if (empty($importUrl)) { |
|
| 162 | continue; |
||
| 163 | } |
||
| 164 | |||
| 165 | 5 | $importContent = $this->_getImportContent($importUrl); |
|
| 166 | |||
| 167 | 5 | if (null === $importContent) { |
|
| 168 | continue; |
||
| 169 | } |
||
| 170 | |||
| 171 | 5 | $code = str_replace($m[0][$k], $importContent, $code); |
|
| 172 | 7 | } |
|
| 173 | 7 | } |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $code |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 8 | protected function collectCharsets(&$code) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $code |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | 8 | protected function collectImports(&$code) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $code |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | protected function collectFonts(&$code) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $code |
||
| 210 | * @param string $pattern |
||
| 211 | * @param callable $handler |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 8 | protected function _collect(&$code, $pattern, $handler) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param string|null $url |
||
| 232 | * @return null|string |
||
| 233 | */ |
||
| 234 | 5 | protected function _getImportContent($url) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $url |
||
| 268 | * @return bool|string |
||
| 269 | */ |
||
| 270 | 5 | protected function _fetchImportFileContent($url) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * If the <link> tag has a media="type" attribute, wrap the content in an equivalent media query |
||
| 284 | * @param string $html HTML of the link tag |
||
| 285 | * @param string $content CSS content |
||
| 286 | * @return string $content CSS content wrapped with media query, if applicable |
||
| 287 | */ |
||
| 288 | 8 | protected function convertMediaTypeAttributeToMediaQuery($html, &$content) |
|
| 294 | } |
||
| 295 |