Complex classes like IconService 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 IconService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class IconService { |
||
| 28 | // URL types |
||
| 29 | const URL_TYPE_ABSOLUTE = 1; |
||
| 30 | const URL_TYPE_ABSOLUTE_SCHEME = 2; |
||
| 31 | const URL_TYPE_ABSOLUTE_PATH = 3; |
||
| 32 | const URL_TYPE_RELATIVE = 4; |
||
| 33 | const URL_TYPE_EMBED_BASE64 = 5; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string Page URL |
||
| 37 | */ |
||
| 38 | public $url; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string Page URL, after prospective redirects |
||
| 42 | */ |
||
| 43 | public $pageUrl; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string Site root URL (homepage), based on $pageUrl |
||
| 47 | */ |
||
| 48 | public $siteUrl; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string full URI to favicon |
||
| 52 | */ |
||
| 53 | public $icoUrl; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string favicon type (file extension, ex: ico|gif|png) |
||
| 57 | */ |
||
| 58 | public $icoType; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string favicon url determination method (default /favicon.ico or found in head>link tag) |
||
| 62 | */ |
||
| 63 | public $findMethod; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string details, in case of failure |
||
| 67 | */ |
||
| 68 | public $error; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var bool tell if the favicon exists (set after calling IconService) |
||
| 72 | */ |
||
| 73 | public $icoExists; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string md5 of $icoData |
||
| 77 | */ |
||
| 78 | public $icoMd5; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string favicon binary data |
||
| 82 | */ |
||
| 83 | public $icoData; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array Additional debug info |
||
| 87 | */ |
||
| 88 | public $debugInfo; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string HTTP proxy (ex: localhost:8888) |
||
| 92 | */ |
||
| 93 | protected $httpProxy; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var bool SSL verify peer (default: true) |
||
| 97 | */ |
||
| 98 | protected $sslVerify; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Create a new IconService object, search & download favicon if $auto is true |
||
| 102 | * |
||
| 103 | * @param string $url Page URL |
||
| 104 | * @param array $options Optional settings |
||
| 105 | * @param bool $auto Search & download favicon on instantiation |
||
| 106 | */ |
||
| 107 | public function __construct($url, $options = null, $auto = true) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Download page and search html to find favicon URL. Returns favicon URL. |
||
| 125 | * HTML parsing is achieved using regular expressions (http://blog.codinghorror.com/parsing-html-the-cthulhu-way/) |
||
| 126 | * to get it work on all kinds of web documents (including non w3c compliance), which an XML parser can't do. |
||
| 127 | */ |
||
| 128 | public function getFaviconUrl() { |
||
| 182 | |||
| 183 | |||
| 184 | private function parseLinkElement($htmlHead, $pageUrlInfo, $base_href){ |
||
| 185 | if (preg_match('#<\s*link[^>]*(rel=(["\'])[^>\2]*icon[^>\2]*\2)[^>]*>#i', $htmlHead, $matches)) { |
||
| 186 | $link_tag = $matches[0]; |
||
| 187 | $this->debugInfo['link_tag'] = $link_tag; |
||
| 188 | |||
| 189 | // HTML <link> icon tag href analysis |
||
| 190 | if (preg_match('#href\s*=\s*(["\'])(.*?)\1#i', $link_tag, $matches)) { |
||
| 191 | $ico_href = trim($matches[2]); |
||
| 192 | $this->debugInfo['ico_href'] = $ico_href; |
||
| 193 | $this->findMethod = 'head'; |
||
| 194 | |||
| 195 | // Building full absolute URL |
||
| 196 | $urlType = self::urlType($ico_href); |
||
| 197 | switch ($urlType) { |
||
| 198 | case self::URL_TYPE_ABSOLUTE: |
||
| 199 | $this->findMethod .= ' absolute'; |
||
| 200 | $this->icoUrl = $ico_href; |
||
| 201 | $this->icoType = self::getExtension($this->icoUrl); |
||
| 202 | break; |
||
| 203 | case self::URL_TYPE_ABSOLUTE_SCHEME: |
||
| 204 | $this->findMethod .= ' absolute_scheme'; |
||
| 205 | $this->icoUrl = $pageUrlInfo['scheme'] . ':' . $ico_href; |
||
| 206 | $this->icoType = self::getExtension($this->icoUrl); |
||
| 207 | break; |
||
| 208 | case self::URL_TYPE_ABSOLUTE_PATH: |
||
| 209 | $this->findMethod .= ' absolute_path'; |
||
| 210 | $this->icoUrl = rtrim($this->siteUrl, '/') . '/' . ltrim($ico_href, '/'); |
||
| 211 | $this->findMethod .= ' without base href'; |
||
| 212 | if (isset($base_href)) { |
||
| 213 | $baseHrefType = self::urlType($base_href); |
||
| 214 | if ($baseHrefType != self::URL_TYPE_ABSOLUTE) { |
||
| 215 | throw new \Exception("Base href is not an absolute URL"); |
||
| 216 | } |
||
| 217 | $baseUrlInfo = parse_url($base_href); |
||
| 218 | $this->icoUrl = $baseUrlInfo['scheme'] . '://' . $baseUrlInfo['host'] . $ico_href; |
||
| 219 | $this->findMethod .= ' with base href'; |
||
| 220 | } |
||
| 221 | $this->icoType = self::getExtension($this->icoUrl); |
||
| 222 | break; |
||
| 223 | case self::URL_TYPE_RELATIVE: |
||
| 224 | $this->findMethod .= ' relative'; |
||
| 225 | $path = preg_replace('#/[^/]+?$#i', '/', $pageUrlInfo['path']); |
||
| 226 | $this->icoUrl = $pageUrlInfo['scheme'] . '://' . $pageUrlInfo['host'] . $path . $ico_href; |
||
| 227 | $this->findMethod .= ' without base href'; |
||
| 228 | if (isset($base_href)) { |
||
| 229 | $this->icoUrl = $base_href . $ico_href; |
||
| 230 | $this->findMethod .= ' with base href'; |
||
| 231 | } |
||
| 232 | $this->icoType = self::getExtension($this->icoUrl); |
||
| 233 | break; |
||
| 234 | case self::URL_TYPE_EMBED_BASE64: |
||
| 235 | $this->findMethod .= ' base64'; |
||
| 236 | $this->icoUrl = $ico_href; |
||
| 237 | break; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Download the favicon if available |
||
| 245 | */ |
||
| 246 | public function downloadFavicon() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Download URL as Firefox with cURL |
||
| 317 | * Details available in $info if provided |
||
| 318 | * |
||
| 319 | * @param string $url URL to download |
||
| 320 | * @param array $info Download metadata |
||
| 321 | * @return bool|mixed |
||
| 322 | */ |
||
| 323 | public function downloadAs($url, &$info = null) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Return file extension from an URL or a file path |
||
| 359 | * |
||
| 360 | * @param string $url |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public static function getExtension($url) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Return file extension from MIME type |
||
| 374 | * |
||
| 375 | * @param string $mimeType |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public static function getExtensionFromMimeType($mimeType) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Return URL type, either : |
||
| 395 | * - URL_TYPE_ABSOLUTE ex: http://www.domain.com/images/fav.ico |
||
| 396 | * - URL_TYPE_ABSOLUTE_SCHEME ex: //www.domain.com/images/fav.ico |
||
| 397 | * - URL_TYPE_ABSOLUTE_PATH ex: /images/fav.ico |
||
| 398 | * - URL_TYPE_RELATIVE ex: ../images/fav.ico |
||
| 399 | * - URL_TYPE_EMBED_BASE64 ex: data:image/x-icon;base64,AAABAA... |
||
| 400 | * |
||
| 401 | * @return int |
||
| 402 | */ |
||
| 403 | public static function urlType($url) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Show object printable properties, or return it if $return is true |
||
| 420 | * |
||
| 421 | * @param boolean $return |
||
| 422 | * @return IconService |
||
| 423 | */ |
||
| 424 | public function debug($return = false) { |
||
| 434 | } |