Complex classes like Includes_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 Includes_processing, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Includes_processing { |
||
19 | /** |
||
20 | * Do not inline files bigger than 4 KiB |
||
21 | */ |
||
22 | const MAX_EMBEDDING_SIZE = 4096; |
||
23 | protected static $extension_to_mime = [ |
||
24 | 'jpeg' => 'image/jpg', |
||
25 | 'jpe' => 'image/jpg', |
||
26 | 'jpg' => 'image/jpg', |
||
27 | 'gif' => 'image/gif', |
||
28 | 'png' => 'image/png', |
||
29 | 'svg' => 'image/svg+xml', |
||
30 | 'svgz' => 'image/svg+xml', |
||
31 | 'woff' => 'application/font-woff', |
||
32 | //'woff2' => 'application/font-woff2', |
||
33 | 'css' => 'text/css' |
||
34 | ]; |
||
35 | /** |
||
36 | * Analyses file for images, fonts and css links and include they content into single resulting css file. |
||
37 | * |
||
38 | * Supports next file extensions for possible includes: |
||
39 | * jpeg, jpe, jpg, gif, png, ttf, ttc, svg, svgz, woff, eot, css |
||
40 | * |
||
41 | * @param string $data Content of processed file |
||
42 | * @param string $file Path to file, that includes specified in previous parameter content |
||
43 | * @param string[] $not_embedded_resources Some resources like images and fonts might not be embedded into resulting CSS because of their size |
||
44 | * |
||
45 | * @return string $data |
||
46 | */ |
||
47 | 10 | static function css ($data, $file, &$not_embedded_resources = []) { |
|
48 | 10 | $dir = dirname($file); |
|
49 | /** |
||
50 | * Remove comments, tabs and new lines |
||
51 | */ |
||
52 | 10 | $data = preg_replace('#(/\*.*?\*/)|\t|\n|\r#s', ' ', $data); |
|
53 | /** |
||
54 | * Remove unnecessary spaces |
||
55 | */ |
||
56 | 10 | $data = preg_replace('/\s*([,;>{}(])\s*/', '$1', $data); |
|
57 | 10 | $data = preg_replace('/\s+/', ' ', $data); |
|
58 | /** |
||
59 | * Return spaces required in media queries |
||
60 | */ |
||
61 | 10 | $data = preg_replace('/\s(and|or)\(/', ' $1 (', $data); |
|
62 | /** |
||
63 | * Duplicated semicolons |
||
64 | */ |
||
65 | 10 | $data = preg_replace('/;+/m', ';', $data); |
|
66 | /** |
||
67 | * Minify rgb colors declarations |
||
68 | */ |
||
69 | 10 | $data = preg_replace_callback( |
|
70 | 10 | '/rgb\(([0-9,.]+)\)/i', |
|
71 | function ($rgb) { |
||
72 | 6 | $rgb = explode(',', $rgb[1]); |
|
73 | return |
||
74 | '#'. |
||
75 | 6 | str_pad(dechex($rgb[0]), 2, 0, STR_PAD_LEFT). |
|
76 | 6 | str_pad(dechex($rgb[1]), 2, 0, STR_PAD_LEFT). |
|
77 | 6 | str_pad(dechex($rgb[2]), 2, 0, STR_PAD_LEFT); |
|
78 | 10 | }, |
|
79 | $data |
||
80 | ); |
||
81 | /** |
||
82 | * Minify repeated colors declarations |
||
83 | */ |
||
84 | 10 | $data = preg_replace('/#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3/i', '#$1$2$3', $data); |
|
85 | /** |
||
86 | * Remove unnecessary zeros |
||
87 | */ |
||
88 | 10 | $data = preg_replace('/(\D)0\.(\d+)/i', '$1.$2', $data); |
|
89 | /** |
||
90 | * Unnecessary spaces around colons (should have whitespace character after, otherwise might be `.c :disabled` and will be handled incorrectly) |
||
91 | */ |
||
92 | 10 | $data = preg_replace('/\s*:\s+/', ':', $data); |
|
93 | /** |
||
94 | * Includes processing |
||
95 | */ |
||
96 | 10 | $data = preg_replace_callback( |
|
97 | 10 | '/url\((.*?)\)|@import\s*[\'"](.*?)[\'"]\s*;/', |
|
98 | function ($match) use ($dir, &$not_embedded_resources) { |
||
99 | 10 | $path = @$match[2] ?: $match[1]; |
|
100 | 10 | $path = trim($path, '\'" '); |
|
101 | 10 | $link = explode('?', $path, 2)[0]; |
|
102 | 10 | if (!static::is_relative_path_and_exists($link, $dir)) { |
|
103 | 10 | return $match[0]; |
|
104 | } |
||
105 | 10 | $extension = file_extension($link); |
|
106 | 10 | if ($extension == 'css') { |
|
107 | /** |
||
108 | * For recursive includes processing, if CSS file includes others CSS files |
||
109 | * TODO: Support for `@import` with media queries |
||
110 | */ |
||
111 | 6 | return static::css(file_get_contents("$dir/$link"), "$dir/$link", $not_embedded_resources); |
|
112 | } |
||
113 | 10 | $content = file_get_contents("$dir/$link"); |
|
114 | 10 | if (!isset(static::$extension_to_mime[$extension]) || filesize("$dir/$link") > static::MAX_EMBEDDING_SIZE) { |
|
115 | 10 | $path_relatively_to_the_root = str_replace(getcwd(), '', realpath("$dir/$link")); |
|
116 | 10 | $path_relatively_to_the_root .= '?'.substr(md5($content), 0, 5); |
|
117 | 10 | if (strpos($path, '?') === false) { |
|
118 | 6 | $not_embedded_resources[] = $path_relatively_to_the_root; |
|
119 | } |
||
120 | 10 | return str_replace($path, "'".str_replace("'", "\\'", $path_relatively_to_the_root)."'", $match[0]); |
|
121 | } |
||
122 | 6 | $mime_type = static::$extension_to_mime[$extension]; |
|
123 | 6 | $content = base64_encode($content); |
|
124 | 6 | return str_replace($path, "data:$mime_type;charset=utf-8;base64,$content", $match[0]); |
|
125 | 10 | }, |
|
126 | $data |
||
127 | ); |
||
128 | 10 | return trim($data); |
|
129 | } |
||
130 | /** |
||
131 | * Simple and fast JS minification |
||
132 | * |
||
133 | * @param string $data |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 10 | static function js ($data) { |
|
207 | /** |
||
208 | * Analyses file for scripts and styles, combines them into resulting files in order to optimize loading process |
||
209 | * (files with combined scripts and styles will be created) |
||
210 | * |
||
211 | * @param string $data Content of processed file |
||
212 | * @param string $file Path to file, that includes specified in previous parameter content |
||
213 | * @param string $base_target_file_path Base filename for resulting combined files |
||
214 | * @param bool $vulcanization Whether to put combined files separately or to make includes built-in (vulcanization) |
||
215 | * @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 |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 8 | static function html ($data, $file, $base_target_file_path, $vulcanization, &$not_embedded_resources = []) { |
|
232 | /** |
||
233 | * @param string $data Content of processed file |
||
234 | * @param string $file Path to file, that includes specified in previous parameter content |
||
235 | * @param string $base_target_file_path Base filename for resulting combined files |
||
236 | * @param bool $vulcanization Whether to put combined files separately or to make includes built-in (vulcanization) |
||
237 | * @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 |
||
238 | * |
||
239 | * @return string |
||
|
|||
240 | */ |
||
241 | 8 | protected static function html_process_scripts (&$data, $file, $base_target_file_path, $vulcanization, &$not_embedded_resources) { |
|
286 | /** |
||
287 | * @param string $data Content of processed file |
||
288 | * @param string $file Path to file, that includes specified in previous parameter content |
||
289 | * @param string $base_target_file_path Base filename for resulting combined files |
||
290 | * @param bool $vulcanization Whether to put combined files separately or to make includes built-in (vulcanization) |
||
291 | * @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 |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | 8 | protected static function html_process_links_and_styles (&$data, $file, $base_target_file_path, $vulcanization, &$not_embedded_resources) { |
|
361 | /** |
||
362 | * @param string $link |
||
363 | * @param string $url |
||
364 | * @param string $dir |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | 8 | protected static function has_relative_href ($link, &$url, $dir) { |
|
378 | /** |
||
379 | * Simple check for http[s], ftp and absolute links |
||
380 | * |
||
381 | * @param string $path |
||
382 | * @param string $dir |
||
383 | * |
||
384 | * @return bool |
||
385 | */ |
||
386 | 10 | protected static function is_relative_path_and_exists ($path, $dir) { |
|
389 | } |
||
390 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.