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 URI_PATTERN = "url(data:%s;base64,%s)"; |
||
20 | |||
21 | const HTTP_SEARCH_PATTERN = "%url\\(['\" ]*((?!data:)[^'\" ]+)['\" ]*\\)%U"; |
||
22 | const HTTP_ENABLED = 1; |
||
23 | const HTTP_DEFAULT_HTTPS = 2; |
||
24 | const HTTP_URL_ON_ERROR = 4; |
||
25 | const HTTP_EMBED_FONTS = 8; |
||
26 | const HTTP_EMBED_SVG = 16; |
||
27 | const HTTP_EMBED_SCHEME = 32; |
||
28 | const HTTP_EMBED_URL_ONLY = 64; |
||
29 | |||
30 | protected $root_dir; |
||
31 | |||
32 | /** @var integer the http flags */ |
||
33 | protected $http_flags = 0; |
||
34 | |||
35 | /** |
||
36 | * @param $root_dir |
||
37 | */ |
||
38 | public function setRootDir($root_dir) |
||
42 | |||
43 | /** |
||
44 | * Allow assets referenced over HTTP to be embedded, or assets in a css |
||
45 | * file loaded over HTTP. Flags: |
||
46 | * |
||
47 | * - CssEmbed::HTTP_ENABLED: enable embedding over http; |
||
48 | * - CssEmbed::HTTP_DEFAULT_HTTPS: for URLs with no scheme, use https to |
||
49 | * instead of http |
||
50 | * - CssEmbed::HTTP_URL_ON_ERROR: if there is an error fetching a remote |
||
51 | * asset, embed the URL instead of throwing an exception |
||
52 | * - CssEmbed::HTTP_EMBED_FONTS: embedding fonts will usually break them |
||
53 | * in most browsers. Enable this flag to force the embed. **WARNING** |
||
54 | * this flag is currently not tested, but seems to work. |
||
55 | * - CssEmbed::HTTP_EMBED_SVG: SVG is often used as a font face; however |
||
56 | * including these in a stylesheet will cause it to bloat for browsers |
||
57 | * that don't use it. By default SVGs will be replaced with the URL |
||
58 | * to the asset; set this flag to force the embed of SVG files. |
||
59 | * - CssEmbed::HTTP_EMBED_SCHEME: By default, assets that are converted |
||
60 | * to URLs instead of data urls have no scheme (eg, "//example.com"). |
||
61 | * This is better for stylesheets that are maybe served over http or |
||
62 | * https, but it will break stylesheets served from a local HTML file. |
||
63 | * Set this option to force the schema (eg, "http://example.com"). |
||
64 | * - CssEmbed::HTTP_EMBED_URL_ONLY: do not convert assets to data URLs, |
||
65 | * only the fully qualified URL. |
||
66 | * |
||
67 | * |
||
68 | * @param integer $flags |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | public function enableHttp( |
||
77 | |||
78 | /** |
||
79 | * Set a single http option flag. See `enableHttp` for a description of |
||
80 | * available flags. |
||
81 | * |
||
82 | * @param integer $flag |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | public function setHttpFlag($flag) |
||
90 | |||
91 | /** |
||
92 | * unset a single http option flag. See `enableHttp` for a description of |
||
93 | * available flags. |
||
94 | * |
||
95 | * @param integer $flag |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function unsetHttpFlag($flag) |
||
103 | |||
104 | /** |
||
105 | * @param $css_file |
||
106 | * @return null|string |
||
107 | * @throws \InvalidArgumentException |
||
108 | */ |
||
109 | public function embedCss($css_file) |
||
124 | |||
125 | /** |
||
126 | * @param $content |
||
127 | * @return mixed |
||
128 | */ |
||
129 | public function embedString($content) |
||
140 | |||
141 | |||
142 | /** |
||
143 | * @param $matches |
||
144 | * @return string |
||
145 | */ |
||
146 | protected function replace($matches) |
||
150 | |||
151 | /** |
||
152 | * @param $file |
||
153 | * @return string |
||
154 | */ |
||
155 | protected function embedFile($file) |
||
159 | |||
160 | /** |
||
161 | * @param $file |
||
162 | * @return string |
||
163 | */ |
||
164 | protected function mimeType($file) |
||
176 | |||
177 | /** |
||
178 | * @param $file |
||
179 | * @return string |
||
180 | * @throws \InvalidArgumentException |
||
181 | */ |
||
182 | protected function base64($file) |
||
190 | |||
191 | /** |
||
192 | * @param $matches |
||
193 | * @return string |
||
194 | */ |
||
195 | protected function httpEnabledReplace($matches) |
||
212 | |||
213 | /** |
||
214 | * Get the contents of a URL and return it as a data uri within url() |
||
215 | * |
||
216 | * @param string $url the URL to the file to embed |
||
217 | * @return string|bool the string for the CSS url property, or FALSE if the |
||
218 | * url could not/should not be embedded. |
||
219 | */ |
||
220 | protected function embedHttpAsset($url) |
||
260 | |||
261 | /** |
||
262 | * For URLs that could not/should not be embedded, embed the resolved URL |
||
263 | * instead. |
||
264 | * |
||
265 | * @param string $url |
||
266 | * @return string |
||
267 | */ |
||
268 | protected function embedHttpAssetUrl($url) |
||
275 | |||
276 | /** |
||
277 | * Check if an asset is remote or local |
||
278 | * |
||
279 | * @param string $path the path specified in the CSS file |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | protected function isHttpAsset($path) |
||
305 | |||
306 | /** |
||
307 | * Resolve the URL to an http asset |
||
308 | * |
||
309 | * @param string $root_url the root URL |
||
310 | * @param string |
||
311 | */ |
||
312 | protected function resolveHttpAssetUrl($root_url, $path) |
||
367 | |||
368 | /** |
||
369 | * Throw an exception if HTTP_URL_ON_ERROR is not set |
||
370 | * |
||
371 | * @param string $msg the message |
||
372 | * @param string $interpolations... strings to interpolate in the error message |
||
373 | * @throws \InvalidArgmumentException |
||
374 | * @return void |
||
375 | */ |
||
376 | protected function httpError($msg, $interpolations) |
||
384 | } |
||
385 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.