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 |
||
| 17 | class SitemapParser |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Default encoding |
||
| 21 | */ |
||
| 22 | const ENCODING = 'UTF-8'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * XML file extension |
||
| 26 | */ |
||
| 27 | const XML_EXTENSION = '.xml'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Compressed XML file extension |
||
| 31 | */ |
||
| 32 | const XML_EXTENSION_COMPRESSED = '.xml.gz'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * XML Sitemap tag |
||
| 36 | */ |
||
| 37 | const XML_TAG_SITEMAP = 'sitemap'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * XML URL tag |
||
| 41 | */ |
||
| 42 | const XML_TAG_URL = 'url'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Robots.txt path |
||
| 46 | */ |
||
| 47 | const ROBOTSTXT_PATH = '/robots.txt'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Robots.txt sitemap prefix |
||
| 51 | */ |
||
| 52 | const ROBOTSTXT_PREFIX = 'Sitemap:'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * User-Agent to send with every HTTP(S) request |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $userAgent; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Configuration options |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $config = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Sitemaps discovered |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $sitemaps = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * URLs discovered |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $urls = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Sitemap URLs discovered but not yet parsed |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $queue = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Parsed URLs history |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $history = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Current URL being parsed |
||
| 92 | * @var null|string |
||
| 93 | */ |
||
| 94 | protected $currentURL; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Constructor |
||
| 98 | * |
||
| 99 | * @param string $userAgent User-Agent to send with every HTTP(S) request |
||
| 100 | * @param array $config Configuration options |
||
| 101 | * @throws SitemapParserException |
||
| 102 | */ |
||
| 103 | public function __construct($userAgent = 'SitemapParser', $config = []) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Parse Recursive |
||
| 121 | * |
||
| 122 | * @param string $url |
||
| 123 | * @return void |
||
| 124 | * @throws SitemapParserException |
||
| 125 | */ |
||
| 126 | public function parseRecursive($url) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Add an array of URLs to the parser queue |
||
| 140 | * |
||
| 141 | * @param array $urlArray |
||
| 142 | */ |
||
| 143 | public function addToQueue($urlArray) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Sitemap URLs discovered but not yet parsed |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function getQueue() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Parse |
||
| 163 | * |
||
| 164 | * @param string $url URL to parse |
||
| 165 | * @param string|null $urlContent URL body content (skip download) |
||
| 166 | * @return void |
||
| 167 | * @throws SitemapParserException |
||
| 168 | */ |
||
| 169 | public function parse($url, $urlContent = null) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Cleanup between each parse |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | protected function clean() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Request the body content of an URL |
||
| 205 | * |
||
| 206 | * @return string Raw body content |
||
| 207 | * @throws SitemapParserException |
||
| 208 | */ |
||
| 209 | protected function getContent() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Search for sitemaps in the robots.txt content |
||
| 228 | * |
||
| 229 | * @param string $robotstxt |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | protected function parseRobotstxt($robotstxt) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Validate URL arrays and add them to their corresponding arrays |
||
| 250 | * |
||
| 251 | * @param string $type sitemap|url |
||
| 252 | * @param array $array Tag array |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | protected function addArray($type, $array) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Generate the \SimpleXMLElement object if the XML is valid |
||
| 272 | * |
||
| 273 | * @param string $xml |
||
| 274 | * @return \SimpleXMLElement|false |
||
| 275 | */ |
||
| 276 | protected function generateXMLObject($xml) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Parse line separated text string |
||
| 288 | * |
||
| 289 | * @param string $string |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | protected function parseString($string) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Check if the URL may contain an Sitemap |
||
| 311 | * |
||
| 312 | * @param string $url |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | protected function isSitemapURL($url) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Parse Json object |
||
| 326 | * |
||
| 327 | * @param string $type Sitemap or URL |
||
| 328 | * @param \SimpleXMLElement $json object |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | protected function parseJson($type, $json) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Sitemaps discovered |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | public function getSitemaps() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * URLs discovered |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | public function getURLs() |
||
| 361 | } |
||
| 362 |