Complex classes like Assets_processing 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_processing, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Assets_processing { |
||
| 18 | protected static $extension_to_mime = [ |
||
| 19 | 'jpeg' => 'image/jpg', |
||
| 20 | 'jpe' => 'image/jpg', |
||
| 21 | 'jpg' => 'image/jpg', |
||
| 22 | 'gif' => 'image/gif', |
||
| 23 | 'png' => 'image/png', |
||
| 24 | 'svg' => 'image/svg+xml', |
||
| 25 | 'svgz' => 'image/svg+xml', |
||
| 26 | 'woff2' => 'application/font-woff2' |
||
| 27 | ]; |
||
| 28 | /** |
||
| 29 | * Analyses file for images, fonts and css links and include they content into single resulting css file. |
||
| 30 | * |
||
| 31 | * Supports next file extensions for possible assets: |
||
| 32 | * jpeg, jpe, jpg, gif, png, ttf, ttc, svg, svgz, woff, css |
||
| 33 | * |
||
| 34 | * @param string $data Content of processed file |
||
| 35 | * @param string $file Path to file, that contains specified in previous parameter content |
||
| 36 | * @param string $target_directory_path Target directory for resulting combined files |
||
| 37 | * @param string[] $not_embedded_resources Some resources like images and fonts might not be embedded into resulting CSS because of their size |
||
| 38 | * |
||
| 39 | * @return string $data |
||
| 40 | */ |
||
| 41 | 10 | public static function css ($data, $file, $target_directory_path = PUBLIC_CACHE, &$not_embedded_resources = []) { |
|
| 42 | 10 | $dir = dirname($file); |
|
| 43 | /** |
||
| 44 | * Remove comments, tabs and new lines |
||
| 45 | */ |
||
| 46 | 10 | $data = preg_replace('#(/\*.*?\*/)|\t|\n|\r#s', ' ', $data); |
|
| 47 | /** |
||
| 48 | * Remove unnecessary spaces |
||
| 49 | */ |
||
| 50 | 10 | $data = preg_replace('/\s*([,;>{}(])\s*/', '$1', $data); |
|
| 51 | 10 | $data = preg_replace('/\s+/', ' ', $data); |
|
| 52 | /** |
||
| 53 | * Return spaces required in media queries |
||
| 54 | */ |
||
| 55 | 10 | $data = preg_replace('/\s(and|or)\(/', ' $1 (', $data); |
|
| 56 | /** |
||
| 57 | * Duplicated semicolons |
||
| 58 | */ |
||
| 59 | 10 | $data = preg_replace('/;+/m', ';', $data); |
|
| 60 | /** |
||
| 61 | * Minify rgb colors declarations |
||
| 62 | */ |
||
| 63 | 10 | $data = preg_replace_callback( |
|
| 64 | 10 | '/rgb\(([0-9,.]+)\)/i', |
|
| 65 | function ($rgb) { |
||
| 66 | 6 | $rgb = explode(',', $rgb[1]); |
|
| 67 | return |
||
| 68 | '#'. |
||
| 69 | 6 | str_pad(dechex($rgb[0]), 2, 0, STR_PAD_LEFT). |
|
| 70 | 6 | str_pad(dechex($rgb[1]), 2, 0, STR_PAD_LEFT). |
|
| 71 | 6 | str_pad(dechex($rgb[2]), 2, 0, STR_PAD_LEFT); |
|
| 72 | 10 | }, |
|
| 73 | $data |
||
| 74 | ); |
||
| 75 | /** |
||
| 76 | * Minify repeated colors declarations |
||
| 77 | */ |
||
| 78 | 10 | $data = preg_replace('/#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3/i', '#$1$2$3', $data); |
|
| 79 | /** |
||
| 80 | * Remove unnecessary zeros |
||
| 81 | */ |
||
| 82 | 10 | $data = preg_replace('/(\D)0\.(\d+)/i', '$1.$2', $data); |
|
| 83 | /** |
||
| 84 | * Unnecessary spaces around colons (should have whitespace character after, otherwise `.class :disabled` will be handled incorrectly) |
||
| 85 | */ |
||
| 86 | 10 | $data = preg_replace('/\s*:\s+/', ':', $data); |
|
| 87 | /** |
||
| 88 | * Assets processing |
||
| 89 | */ |
||
| 90 | // TODO: replace by loop, track duplicated stuff that are subject to inlining and if they appear more than once, don't inline them |
||
| 91 | 10 | $data = preg_replace_callback( |
|
| 92 | 10 | '/url\((.*)\)|@import\s*(?:url\()?\s*([\'"].*[\'"])\s*\)??(.*);/U', |
|
| 93 | function ($match) use ($dir, $target_directory_path, &$not_embedded_resources) { |
||
| 94 | 10 | $path_matched = $match[2] ?? $match[1]; |
|
| 95 | 10 | $path = trim($path_matched, '\'" '); |
|
| 96 | 10 | $link = explode('?', $path, 2)[0]; |
|
| 97 | 10 | if (!static::is_relative_path_and_exists($link, $dir)) { |
|
| 98 | 4 | return $match[0]; |
|
| 99 | } |
||
| 100 | 10 | $extension = file_extension($link); |
|
| 101 | 10 | $absolute_path = static::absolute_path($link, $dir); |
|
| 102 | 10 | $content = file_get_contents($absolute_path); |
|
| 103 | 10 | if ($extension == 'css' && @$match[2]) { |
|
| 104 | /** |
||
| 105 | * Only inline CSS imports without media queries, imports with media queries will be placed as separate files |
||
| 106 | */ |
||
| 107 | 6 | if (!trim(@$match[3])) { |
|
| 108 | 6 | return static::css($content, $absolute_path, $target_directory_path, $not_embedded_resources); |
|
| 109 | } |
||
| 110 | 6 | $filename = static::file_put_contents_with_hash( |
|
| 111 | $target_directory_path, |
||
| 112 | $extension, |
||
| 113 | 6 | static::css($content, $absolute_path, $target_directory_path) |
|
| 114 | ); |
||
| 115 | 6 | return str_replace($path_matched, "'./$filename'", $match[0]); |
|
| 116 | } |
||
| 117 | 10 | if (!isset(static::$extension_to_mime[$extension])) { |
|
| 118 | $filename = static::file_put_contents_with_hash($target_directory_path, $extension, $content); |
||
| 119 | return str_replace($path_matched, "'./$filename'", $match[0]); |
||
| 120 | } |
||
| 121 | 10 | $filename = md5_file($absolute_path).'.'.$extension; |
|
| 122 | 10 | copy($absolute_path, "$target_directory_path/$filename"); |
|
| 123 | 10 | if (strpos($path, '?') === false) { |
|
| 124 | 6 | $not_embedded_resources[] = str_replace(getcwd(), '', "$target_directory_path/$filename"); |
|
| 125 | } |
||
| 126 | 10 | return str_replace($path_matched, "'./$filename'", $match[0]); |
|
| 127 | 10 | }, |
|
| 128 | $data |
||
| 129 | ); |
||
| 130 | 10 | return trim($data); |
|
| 131 | } |
||
| 132 | /** |
||
| 133 | * Put `$content` into `$dir` where filename is `md5($content)` with specified extension |
||
| 134 | * |
||
| 135 | * @param string $dir |
||
| 136 | * @param string $extension |
||
| 137 | * @param string $content |
||
| 138 | * |
||
| 139 | * @return string Filename (without full path) |
||
| 140 | */ |
||
| 141 | 6 | protected static function file_put_contents_with_hash ($dir, $extension, $content) { |
|
| 146 | /** |
||
| 147 | * Simple and fast JS minification |
||
| 148 | * |
||
| 149 | * @param string $data |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 10 | public static function js ($data) { |
|
| 236 | /** |
||
| 237 | * @param string $current_line |
||
| 238 | * @param string $next_line |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | 10 | protected static function new_line_needed ($current_line, $next_line) { |
|
| 254 | /** |
||
| 255 | * Analyses file for scripts and styles, combines them into resulting files in order to optimize loading process |
||
| 256 | * (files with combined scripts and styles will be created) |
||
| 257 | * |
||
| 258 | * @param string $data Content of processed file |
||
| 259 | * @param string $file Path to file, that contains specified in previous parameter content |
||
| 260 | * @param string $target_directory_path Target directory for resulting combined files |
||
| 261 | * @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
||
| 262 | * @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 8 | public static function html ($data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources = []) { |
|
| 279 | /** |
||
| 280 | * @param string $data Content of processed file |
||
| 281 | * @param string $file Path to file, that contains specified in previous parameter content |
||
| 282 | * @param string $target_directory_path Target directory for resulting combined files |
||
| 283 | * @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
||
| 284 | * @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
||
| 285 | */ |
||
| 286 | 8 | protected static function html_process_scripts (&$data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources) { |
|
| 326 | /** |
||
| 327 | * @param string $data Content of processed file |
||
| 328 | * @param string $file Path to file, that contains specified in previous parameter content |
||
| 329 | * @param string $target_directory_path Target directory for resulting combined files |
||
| 330 | * @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
||
| 331 | * @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
||
| 332 | */ |
||
| 333 | 8 | protected static function html_process_links_and_styles (&$data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources) { |
|
| 397 | /** |
||
| 398 | * @param string $link |
||
| 399 | * @param string $url |
||
| 400 | * @param string $dir |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | 8 | protected static function has_relative_href ($link, &$url, $dir) { |
|
| 414 | /** |
||
| 415 | * @param string $path |
||
| 416 | * @param string $dir |
||
| 417 | * |
||
| 418 | * @return bool |
||
| 419 | */ |
||
| 420 | 10 | protected static function is_relative_path_and_exists ($path, $dir) { |
|
| 423 | /** |
||
| 424 | * @param string $path |
||
| 425 | * @param string $dir |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | 10 | protected static function absolute_path ($path, $dir) { |
|
| 435 | } |
||
| 436 |