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 DATA_URI_PATTERN = "url(data:%s;base64,%s)"; |
||
| 20 | const URL_URI_PATTERN = "url('%s')"; |
||
| 21 | const MIME_MAGIC_URL = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'; |
||
| 22 | const EMBED_FONTS = 1; |
||
| 23 | const EMBED_SVG = 2; |
||
| 24 | const URL_ON_ERROR = 4; |
||
| 25 | const HTTP_DEFAULT_HTTPS = 1; |
||
| 26 | const HTTP_EMBED_SCHEME = 2; |
||
| 27 | const HTTP_EMBED_URL_ONLY = 4; |
||
| 28 | |||
| 29 | /** @var string the root directory for finding assets */ |
||
| 30 | protected $root_dir; |
||
| 31 | |||
| 32 | /** @var string the path to the local mime.magic database */ |
||
| 33 | protected $mime_magic_path = null; |
||
| 34 | |||
| 35 | /** @var integer flags that modify behavior, embed SVG by default for BC */ |
||
| 36 | protected $flags = 2; |
||
| 37 | |||
| 38 | /** @var bool enable HTTP asset fetching */ |
||
| 39 | protected $http_enabled = false; |
||
| 40 | |||
| 41 | /** @var integer flags that modify behavior in HTTP only */ |
||
| 42 | protected $http_flags = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $root_dir |
||
| 46 | */ |
||
| 47 | public function setRootDir($root_dir) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Set embedding options. Flags: |
||
| 54 | * |
||
| 55 | * - CssEmbed::EMBED_FONTS: embedding fonts will usually break them |
||
| 56 | * in most browsers. Enable this flag to force the embed. WARNING: |
||
| 57 | * this flag is currently not unit tested, but seems to work. |
||
| 58 | * - CssEmbed::EMBED_SVG: SVG is often used as a font face; however |
||
| 59 | * including these in a stylesheet will cause it to bloat for browsers |
||
| 60 | * that don't use it. SVGs will be embedded by default. |
||
| 61 | * - CssEmbed::URL_ON_ERROR: if there is an error fetching an asset, |
||
| 62 | * embed a URL (or best guess at URL) instead of throwing an exception |
||
| 63 | * |
||
| 64 | * @param integer $flags |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function setOptions($flags) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Enable embedding assets over HTTP, or processing stylesheets from HTTP |
||
| 75 | * locations. Available flags: |
||
| 76 | * |
||
| 77 | * - CssEmbed::HTTP_DEFAULT_HTTPS: when HTTP assets are enabled, use |
||
| 78 | * HTTPS for URLs with no scheme |
||
| 79 | * - CssEmbed::HTTP_EMBED_SCHEME: By default, assets that are converted |
||
| 80 | * to URLs instead of data urls have no scheme (eg, "//example.com"). |
||
| 81 | * This is better for stylesheets that are maybe served over http or |
||
| 82 | * https, but it will break stylesheets served from a local HTML file. |
||
| 83 | * Set this option to force the schema (eg, "http://example.com"). |
||
| 84 | * - CssEmbed::HTTP_EMBED_URL_ONLY: do not convert assets to data URLs, |
||
| 85 | * only the fully qualified URL. |
||
| 86 | * |
||
| 87 | * @note this method will turn the options URL_ON_ERROR on and EMBED_SVG |
||
| 88 | * off. You will need to use setOptions() after this method to change that. |
||
| 89 | * |
||
| 90 | * @param bool $enable |
||
| 91 | * @param int $flags flags that modify HTTP behaviour |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function enableHttp($enable = true, $flags = 0) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Enable the functionality to compare mimes against a custom mime.types file. |
||
| 104 | * |
||
| 105 | * @param string $path the path to the mime types file |
||
| 106 | * @param bool $create download and save the Apache mime types file if the |
||
| 107 | * specified path does not exist |
||
| 108 | * @throws \InvalidArgumentException if the mime file does not exist and |
||
| 109 | * cannot be created. |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function enableEnhancedMimeTypes( |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $css_file |
||
| 130 | * @return null|string |
||
| 131 | * @throws \InvalidArgumentException |
||
| 132 | */ |
||
| 133 | public function embedCss($css_file) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $content |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function embedString($content) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * preg_replace_callback callback for embedString. |
||
| 158 | * |
||
| 159 | * @param array $matches |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | protected function replace($matches) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Fetch an asset |
||
| 181 | * |
||
| 182 | * @param string $path the asset path |
||
| 183 | * @return array|false an array with keys 'content' for the file content |
||
| 184 | * and 'mime' for the mime type, or FALSE on error |
||
| 185 | */ |
||
| 186 | protected function fetchAsset($path) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the URL to an asset as it would be embedded in a stylesheet |
||
| 203 | * |
||
| 204 | * @param string $path the path to the asset as it appears in the stylesheet |
||
| 205 | * @return string $url the URL to the asset |
||
| 206 | */ |
||
| 207 | protected function fetchAssetUrl($path) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Fetch an asset stored locally in the filesystem |
||
| 221 | * |
||
| 222 | * @param string $absolute_path the absolute path to the asset |
||
| 223 | * @return array same as fetchAsset |
||
| 224 | */ |
||
| 225 | protected function fetchLocalAsset($absolute_path) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Find the mime for a file on the local file system |
||
| 240 | * |
||
| 241 | * @param string $absolute_path |
||
| 242 | * @return false|string the mime type, or false if not found |
||
| 243 | */ |
||
| 244 | protected function getLocalAssetMime($absolute_path) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Fetch an asset stored remotely over HTTP |
||
| 263 | * |
||
| 264 | * @param string $url the url to the asset |
||
| 265 | * @return array same as fetchAsset |
||
| 266 | */ |
||
| 267 | protected function fetchHttpAsset($url) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Extract the content type header from the headers returned with the |
||
| 285 | * file_get_contents http call |
||
| 286 | * |
||
| 287 | * @param array $headers the `$http_response_headers` created by `file_get_contents` |
||
| 288 | * @return false|string the mime type, or false if not found |
||
| 289 | */ |
||
| 290 | protected function getHttpAssetMime($headers) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Check if a successfully fetched an asset is of a type that can be |
||
| 303 | * embedded given the current options. |
||
| 304 | * |
||
| 305 | * @param array $asset the return value of fetchAsset |
||
| 306 | * @return boolean |
||
| 307 | */ |
||
| 308 | protected function assetIsEmbeddable(array $asset) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Check if an asset is remote or local |
||
| 327 | * |
||
| 328 | * @param string $path the path specified in the CSS file |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | protected function isHttpAsset($path) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Resolve the absolute path to a local asset |
||
| 357 | * |
||
| 358 | * @param string $path the path to the asset, relative to root_dir |
||
| 359 | * @return false|string the absolute path, or false if not found |
||
| 360 | */ |
||
| 361 | protected function resolveAssetPath($path) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Resolve the URL to an http asset |
||
| 371 | * |
||
| 372 | * @param string $path |
||
| 373 | * @return false|string the url, or false if not resolvable |
||
| 374 | */ |
||
| 375 | protected function resolveAssetUrl($path) |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * Resolve the URL to an http asset |
||
| 388 | * |
||
| 389 | * @param string $path |
||
| 390 | * @return false|string the url, or false if not resolvable |
||
| 391 | */ |
||
| 392 | protected function buildAssetUrl($path) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Remove directory traversals from a path. Exists because file_get_contents |
||
| 427 | * seems to choke on http://example.com/path/to/dir/../other-dir/file.txt |
||
| 428 | * |
||
| 429 | * @param string $path |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | protected function removePathTraversals($path) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Check the file mime type against the mime.types file |
||
| 448 | * |
||
| 449 | * @param string $path the path to the file |
||
| 450 | * @return string the mime, or false if it could not be identified |
||
| 451 | */ |
||
| 452 | protected function detectMime($path) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Compare an extention against the a line in the mime.types |
||
| 472 | * |
||
| 473 | * @param string $ext the file extension |
||
| 474 | * @param string $line the line from the mime.types file |
||
| 475 | * @return string|bool the mime type if there is a match, false if not |
||
| 476 | */ |
||
| 477 | protected function compareMime($ext, $line) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Download the Apache mimes.types file and save it locally |
||
| 490 | * |
||
| 491 | * @param string $path the path to save the file to |
||
| 492 | * @return void |
||
| 493 | */ |
||
| 494 | protected function createMimesFile($path) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Throw an exception if URL_ON_ERROR is not set |
||
| 510 | * |
||
| 511 | * This method accepts an unlimited number of arguments. They will be passed |
||
| 512 | * to sprintf to generate the error message in the exception. For example: |
||
| 513 | * |
||
| 514 | * $this->error('My exception about %d %s', 4, 'cats'); |
||
| 515 | * |
||
| 516 | * would throw an exception with with the message "My error about 4 cats". |
||
| 517 | * |
||
| 518 | * @throws \InvalidArgmumentException |
||
| 519 | * @return void |
||
| 520 | */ |
||
| 521 | protected function error() |
||
| 536 | } |
||
| 537 |