Complex classes like SitemapParser 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 SitemapParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class SitemapParser |
||
| 19 | { |
||
| 20 | use UrlParser; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Default User-Agent |
||
| 24 | */ |
||
| 25 | const DEFAULT_USER_AGENT = 'SitemapParser'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Default encoding |
||
| 29 | */ |
||
| 30 | const ENCODING = 'UTF-8'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * XML file extension |
||
| 34 | */ |
||
| 35 | const XML_EXTENSION = 'xml'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Compressed XML file extension |
||
| 39 | */ |
||
| 40 | const XML_EXTENSION_COMPRESSED = 'xml.gz'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * XML Sitemap tag |
||
| 44 | */ |
||
| 45 | const XML_TAG_SITEMAP = 'sitemap'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * XML URL tag |
||
| 49 | */ |
||
| 50 | const XML_TAG_URL = 'url'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Robots.txt path |
||
| 54 | */ |
||
| 55 | const ROBOTSTXT_PATH = '/robots.txt'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * User-Agent to send with every HTTP(S) request |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $userAgent = self::DEFAULT_USER_AGENT; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Configuration options |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $config = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Sitemaps discovered |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $sitemaps = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * URLs discovered |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $urls = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Sitemap URLs discovered but not yet parsed |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $queue = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Parsed URLs history |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $history = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Current URL being parsed |
||
| 95 | * @var null|string |
||
| 96 | */ |
||
| 97 | protected $currentURL; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Constructor |
||
| 101 | * |
||
| 102 | * @param string $userAgent User-Agent to send with every HTTP(S) request |
||
| 103 | * @param array $config Configuration options |
||
| 104 | * @throws SitemapParserException |
||
| 105 | */ |
||
| 106 | public function __construct($userAgent = self::DEFAULT_USER_AGENT, array $config = []) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Parse Recursive |
||
| 118 | * |
||
| 119 | * @param string $url |
||
| 120 | * @return void |
||
| 121 | * @throws SitemapParserException |
||
| 122 | */ |
||
| 123 | public function parseRecursive($url) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add an array of URLs to the parser queue |
||
| 137 | * |
||
| 138 | * @param array $urlArray |
||
| 139 | */ |
||
| 140 | public function addToQueue(array $urlArray) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Sitemap URLs discovered but not yet parsed |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function getQueue() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Parse |
||
| 160 | * |
||
| 161 | * @param string $url URL to parse |
||
| 162 | * @param string|null $urlContent URL body content (provide to skip download) |
||
| 163 | * @return void |
||
| 164 | * @throws SitemapParserException |
||
| 165 | */ |
||
| 166 | public function parse($url, $urlContent = null) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Cleanup between each parse |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | protected function clean() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Request the body content of an URL |
||
| 202 | * |
||
| 203 | * @return string Raw body content |
||
| 204 | * @throws SitemapParserException |
||
| 205 | */ |
||
| 206 | protected function getContent() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Search for sitemaps in the robots.txt content |
||
| 226 | * |
||
| 227 | * @param string $robotstxt |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | protected function parseRobotstxt($robotstxt) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Validate URL arrays and add them to their corresponding arrays |
||
| 258 | * |
||
| 259 | * @param string $type sitemap|url |
||
| 260 | * @param array $array Tag array |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | protected function addArray($type, array $array) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Check for missing values and set them to null |
||
| 284 | * |
||
| 285 | * @param array $tags Tags check if exists |
||
| 286 | * @param array $array Array to check |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | protected function fixMissingTags(array $tags, array $array) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Generate the \SimpleXMLElement object if the XML is valid |
||
| 301 | * |
||
| 302 | * @param string $xml |
||
| 303 | * @return \SimpleXMLElement|false |
||
| 304 | */ |
||
| 305 | protected function generateXMLObject($xml) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Parse line separated text string |
||
| 317 | * |
||
| 318 | * @param string $string |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | protected function parseString($string) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Check if the URL may contain an Sitemap |
||
| 340 | * |
||
| 341 | * @param string $url |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | protected function isSitemapURL($url) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Parse Json object |
||
| 355 | * |
||
| 356 | * @param string $type Sitemap or URL |
||
| 357 | * @param \SimpleXMLElement $json object |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | protected function parseJson($type, \SimpleXMLElement $json) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Sitemaps discovered |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public function getSitemaps() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * URLs discovered |
||
| 383 | * |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | public function getURLs() |
||
| 390 | } |
||
| 391 |