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 |
||
| 19 | class SitemapParser |
||
| 20 | { |
||
| 21 | use UrlParser; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Default User-Agent |
||
| 25 | */ |
||
| 26 | const DEFAULT_USER_AGENT = 'SitemapParser-VIPnytt/1.1 (+https://github.com/VIPnytt/SitemapParser/blob/master/README.md)'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Default encoding |
||
| 30 | */ |
||
| 31 | const ENCODING = 'UTF-8'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * XML file extension |
||
| 35 | */ |
||
| 36 | const XML_EXTENSION = 'xml'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Compressed XML file extension |
||
| 40 | */ |
||
| 41 | const XML_EXTENSION_COMPRESSED = 'xml.gz'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * XML Sitemap tag |
||
| 45 | */ |
||
| 46 | const XML_TAG_SITEMAP = 'sitemap'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * XML URL tag |
||
| 50 | */ |
||
| 51 | const XML_TAG_URL = 'url'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Robots.txt path |
||
| 55 | */ |
||
| 56 | const ROBOTSTXT_PATH = '/robots.txt'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * User-Agent to send with every HTTP(S) request |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $userAgent; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Configuration options |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $config = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Sitemaps discovered |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $sitemaps = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * URLs discovered |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $urls = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Sitemap URLs discovered but not yet parsed |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $queue = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Parsed URLs history |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $history = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Current URL being parsed |
||
| 96 | * @var null|string |
||
| 97 | */ |
||
| 98 | protected $currentURL; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Constructor |
||
| 102 | * |
||
| 103 | * @param string $userAgent User-Agent to send with every HTTP(S) request |
||
| 104 | * @param array $config Configuration options |
||
| 105 | * @throws Exceptions\SitemapParserException |
||
| 106 | */ |
||
| 107 | public function __construct($userAgent = self::DEFAULT_USER_AGENT, array $config = []) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Parse Recursive |
||
| 119 | * |
||
| 120 | * @param string $url |
||
| 121 | * @return void |
||
| 122 | * @throws Exceptions\SitemapParserException |
||
| 123 | */ |
||
| 124 | public function parseRecursive($url) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Add an array of URLs to the parser queue |
||
| 143 | * |
||
| 144 | * @param array $urlArray |
||
| 145 | */ |
||
| 146 | public function addToQueue(array $urlArray) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Sitemap URLs discovered but not yet parsed |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | public function getQueue() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Parse |
||
| 169 | * |
||
| 170 | * @param string $url URL to parse |
||
| 171 | * @param string|null $urlContent URL body content (provide to skip download) |
||
| 172 | * @return void |
||
| 173 | * @throws Exceptions\TransferException |
||
| 174 | * @throws Exceptions\SitemapParserException |
||
| 175 | */ |
||
| 176 | public function parse($url, $urlContent = null) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Cleanup between each parse |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | protected function clean() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Request the body content of an URL |
||
| 215 | * |
||
| 216 | * @return string Raw body content |
||
| 217 | * @throws Exceptions\TransferException |
||
| 218 | * @throws Exceptions\SitemapParserException |
||
| 219 | */ |
||
| 220 | protected function getContent() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Search for sitemaps in the robots.txt content |
||
| 242 | * |
||
| 243 | * @param string $robotstxt |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | protected function parseRobotstxt($robotstxt) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Validate URL arrays and add them to their corresponding arrays |
||
| 274 | * |
||
| 275 | * @param string $type sitemap|url |
||
| 276 | * @param array $array Tag array |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | protected function addArray($type, array $array) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Check for missing values and set them to null |
||
| 300 | * |
||
| 301 | * @param array $tags Tags check if exists |
||
| 302 | * @param array $array Array to check |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | protected function fixMissingTags(array $tags, array $array) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Generate the \SimpleXMLElement object if the XML is valid |
||
| 317 | * |
||
| 318 | * @param string $xml |
||
| 319 | * @return \SimpleXMLElement|false |
||
| 320 | */ |
||
| 321 | protected function generateXMLObject($xml) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Parse line separated text string |
||
| 337 | * |
||
| 338 | * @param string $string |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | protected function parseString($string) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Check if the URL may contain an Sitemap |
||
| 360 | * |
||
| 361 | * @param string $url |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | protected function isSitemapURL($url) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Parse Json object |
||
| 375 | * |
||
| 376 | * @param string $type Sitemap or URL |
||
| 377 | * @param \SimpleXMLElement $json object |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | protected function parseJson($type, \SimpleXMLElement $json) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Sitemaps discovered |
||
| 393 | * |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | public function getSitemaps() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * URLs discovered |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function getURLs() |
||
| 410 | } |
||
| 411 |