Complex classes like CssEmbed 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 CssEmbed, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class CssEmbed |
||
16 | { |
||
17 | |||
18 | // const SEARCH_PATTERN = "%url\\(['\" ]*((?!data:|//)[^'\"#\?: ]+)['\" ]*\\)%U"; |
||
|
|||
19 | const SEARCH_PATTERN = "%url\\(['\" ]*((?!data:)[^'\" ]+)['\" ]*\\)%U"; |
||
20 | const DATA_URI_PATTERN = "url(data:%s;base64,%s)"; |
||
21 | const URL_URI_PATTERN = "url('%s')"; |
||
22 | const MIME_MAGIC_URL = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'; |
||
23 | const EMBED_FONTS = 1; |
||
24 | const EMBED_SVG = 2; |
||
25 | const URL_ON_ERROR = 4; |
||
26 | const HTTP_DEFAULT_HTTPS = 1; |
||
27 | const HTTP_EMBED_SCHEME = 2; |
||
28 | const HTTP_EMBED_URL_ONLY = 4; |
||
29 | |||
30 | /** @var string the root directory for finding assets */ |
||
31 | protected $root_dir; |
||
32 | |||
33 | /** @var string the path to the local mime.magic database */ |
||
34 | protected $mime_magic_path = null; |
||
35 | |||
36 | /** @var integer flags that modify behavior, embed SVG by default for BC */ |
||
37 | protected $flags = 2; |
||
38 | |||
39 | /** @var bool enable HTTP asset fetching */ |
||
40 | protected $http_enabled = false; |
||
41 | |||
42 | /** @var integer flags that modify behavior in HTTP only */ |
||
43 | protected $http_flags = 0; |
||
44 | |||
45 | /** |
||
46 | * @param $root_dir |
||
47 | */ |
||
48 | public function setRootDir($root_dir) |
||
52 | |||
53 | /** |
||
54 | * Set embedding options. Flags: |
||
55 | * |
||
56 | * - CssEmbed::EMBED_FONTS: embedding fonts will usually break them |
||
57 | * in most browsers. Enable this flag to force the embed. WARNING: |
||
58 | * this flag is currently not unit tested, but seems to work. |
||
59 | * - CssEmbed::EMBED_SVG: SVG is often used as a font face; however |
||
60 | * including these in a stylesheet will cause it to bloat for browsers |
||
61 | * that don't use it. SVGs will be embedded by default. |
||
62 | * - CssEmbed::URL_ON_ERROR: if there is an error fetching an asset, |
||
63 | * embed a URL (or best guess at URL) instead of throwing an exception |
||
64 | * |
||
65 | * @param integer $flags |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | public function setOptions($flags) |
||
73 | |||
74 | /** |
||
75 | * Enable embedding assets over HTTP, or processing stylesheets from HTTP |
||
76 | * locations. Available flags: |
||
77 | * |
||
78 | * - CssEmbed::HTTP_DEFAULT_HTTPS: when HTTP assets are enabled, use |
||
79 | * HTTPS for URLs with no scheme |
||
80 | * - CssEmbed::HTTP_EMBED_SCHEME: By default, assets that are converted |
||
81 | * to URLs instead of data urls have no scheme (eg, "//example.com"). |
||
82 | * This is better for stylesheets that are maybe served over http or |
||
83 | * https, but it will break stylesheets served from a local HTML file. |
||
84 | * Set this option to force the schema (eg, "http://example.com"). |
||
85 | * - CssEmbed::HTTP_EMBED_URL_ONLY: do not convert assets to data URLs, |
||
86 | * only the fully qualified URL. |
||
87 | * |
||
88 | * @note this method will turn the options URL_ON_ERROR on and EMBED_SVG |
||
89 | * off. You will need to use setOptions() after this method to change that. |
||
90 | * |
||
91 | * @param bool $enable |
||
92 | * @param int $http_flags flags that modify HTTP behaviour |
||
93 | * @return void |
||
94 | */ |
||
95 | public function enableHttp($enable = true, $flags = 0) |
||
102 | |||
103 | /** |
||
104 | * Enable the functionality to compare mimes against a custom mime.types file. |
||
105 | * |
||
106 | * @param string $path the path to the mime types file |
||
107 | * @param bool $create download and save the Apache mime types file if the |
||
108 | * specified path does not exist |
||
109 | * @throws \InvalidArgumentException if the mime file does not exist and |
||
110 | * cannot be created. |
||
111 | * @return void |
||
112 | */ |
||
113 | public function enableEnhancedMimeTypes( |
||
137 | |||
138 | /** |
||
139 | * @param $css_file |
||
140 | * @return null|string |
||
141 | * @throws \InvalidArgumentException |
||
142 | */ |
||
143 | public function embedCss($css_file) |
||
152 | |||
153 | /** |
||
154 | * @param $content |
||
155 | * @return mixed |
||
156 | */ |
||
157 | public function embedString($content) |
||
165 | |||
166 | /** |
||
167 | * preg_replace_callback callback for embedString. |
||
168 | * |
||
169 | * @param array $matches |
||
170 | * @return string |
||
171 | */ |
||
172 | protected function replace($matches) |
||
188 | |||
189 | /** |
||
190 | * Fetch an asset |
||
191 | * |
||
192 | * @param string $path the asset path |
||
193 | * @return array|false an array with keys 'content' for the file content |
||
194 | * and 'mime' for the mime type, or FALSE on error |
||
195 | */ |
||
196 | protected function fetchAsset($path) |
||
210 | |||
211 | /** |
||
212 | * Get the URL to an asset as it would be embedded in a stylesheet |
||
213 | * |
||
214 | * @param string $path the path to the asset as it appears in the stylesheet |
||
215 | * @return string $url the URL to the asset |
||
216 | */ |
||
217 | protected function fetchAssetUrl($path) |
||
228 | |||
229 | /** |
||
230 | * Fetch an asset stored locally in the filesystem |
||
231 | * |
||
232 | * @param string $absolute_path the absolute path to the asset |
||
233 | * @return array same as fetchAsset |
||
234 | */ |
||
235 | protected function fetchLocalAsset($absolute_path) |
||
259 | |||
260 | /** |
||
261 | * Fetch an asset stored remotely over HTTP |
||
262 | * |
||
263 | * @param string $url the url to the asset |
||
264 | * @return array same as fetchAsset |
||
265 | */ |
||
266 | protected function fetchHttpAsset($url) |
||
289 | |||
290 | /** |
||
291 | * Check if a successfully fetched an asset is of a type that can be |
||
292 | * embedded given the current options. |
||
293 | * |
||
294 | * @param array $asset the return value of fetchAsset |
||
295 | * @return boolean |
||
296 | */ |
||
297 | protected function assetIsEmbeddable(array $asset) |
||
313 | |||
314 | /** |
||
315 | * Check if an asset is remote or local |
||
316 | * |
||
317 | * @param string $path the path specified in the CSS file |
||
318 | * |
||
319 | * @return bool |
||
320 | */ |
||
321 | protected function isHttpAsset($path) |
||
343 | |||
344 | /** |
||
345 | * Resolve the absolute path to a local asset |
||
346 | * |
||
347 | * @param string $path the path to the asset, relative to root_dir |
||
348 | * @return string|boolean the absolute path, or false if not found |
||
349 | */ |
||
350 | protected function resolveAssetPath($path) |
||
357 | |||
358 | /** |
||
359 | * Resolve the URL to an http asset |
||
360 | * |
||
361 | * @param string $root_url the root URL |
||
362 | * @param string |
||
363 | */ |
||
364 | protected function resolveAssetUrl($path) |
||
418 | |||
419 | /** |
||
420 | * Check the file mime type against the mime.types file |
||
421 | * |
||
422 | * @param string $path the path to the file |
||
423 | * @return string the mime, or false if it could not be identified |
||
424 | */ |
||
425 | protected function detectMime($path) |
||
442 | |||
443 | /** |
||
444 | * Compare an extention against the a line in the mime.types |
||
445 | * |
||
446 | * @param string $ext the file extension |
||
447 | * @param string $line the line from the mime.types file |
||
448 | * @return string|bool the mime type if there is a match, false if not |
||
449 | */ |
||
450 | protected function compareMime($ext, $line) |
||
460 | |||
461 | /** |
||
462 | * Throw an exception if URL_ON_ERROR is not set |
||
463 | * |
||
464 | * @param string $message OPTIONAL the message |
||
465 | * @param string $interpolations,... unlimited OPTIONAL strings to |
||
466 | * interpolate in the error message |
||
467 | * @throws \InvalidArgmumentException |
||
468 | * @return void |
||
469 | */ |
||
470 | protected function error() |
||
485 | } |
||
486 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.