Complex classes like TxtParser 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 TxtParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class TxtParser |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Robots.txt max length in bytes |
||
| 15 | */ |
||
| 16 | const DEFAULT_BYTE_LIMIT = 500000; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Max rule length |
||
| 20 | */ |
||
| 21 | const RULE_MAX_LENGTH = 500; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Directives |
||
| 25 | */ |
||
| 26 | const DIRECTIVE_ALLOW = 'allow'; |
||
| 27 | const DIRECTIVE_CACHE_DELAY = 'cache-delay'; // unofficial |
||
| 28 | const DIRECTIVE_CLEAN_PARAM = 'clean-param'; // Yandex only |
||
| 29 | const DIRECTIVE_CRAWL_DELAY = 'crawl-delay'; |
||
| 30 | const DIRECTIVE_DISALLOW = 'disallow'; |
||
| 31 | const DIRECTIVE_HOST = 'host'; // Yandex only |
||
| 32 | const DIRECTIVE_SITEMAP = 'sitemap'; |
||
| 33 | const DIRECTIVE_USER_AGENT = 'user-agent'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Default User-Agent |
||
| 37 | */ |
||
| 38 | const FALLBACK_USER_AGENT = '*'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * RAW robots.txt content |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $raw = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Rule array |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $rules = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * User-Agents |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | private $userAgents = [self::FALLBACK_USER_AGENT]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Current line |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $line = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Previous directive |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $previous; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Current Directive |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $directive; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Current Rule |
||
| 78 | * @var array|string |
||
| 79 | */ |
||
| 80 | private $rule; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Constructor |
||
| 84 | * |
||
| 85 | * @param string $content - file content |
||
| 86 | * @param string|null $encoding - character encoding |
||
| 87 | * @param int|null $byteLimit - maximum of bytes to parse |
||
| 88 | * @throws TxtParserException |
||
| 89 | */ |
||
| 90 | public function __construct($content, $encoding = null, $byteLimit = self::DEFAULT_BYTE_LIMIT) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Parse robots.txt |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | private function parseTxt() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Generate Directive:Rule pair |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | private function generateRulePair() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Directives and sub directives |
||
| 154 | * |
||
| 155 | * @param string|null $parent |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | private function directives($parent = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Parse line |
||
| 189 | * |
||
| 190 | * @param string|null $parent |
||
| 191 | * @return array|false |
||
| 192 | */ |
||
| 193 | private function parseLine($parent = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Add value to directive |
||
| 215 | * |
||
| 216 | * @return array|false |
||
| 217 | */ |
||
| 218 | private function add() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Add an Allow or Disallow rule |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | private function addDisAllow() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Add float value |
||
| 264 | * |
||
| 265 | * @return array|false |
||
| 266 | */ |
||
| 267 | private function addFloat() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Add Clean-Param record |
||
| 279 | * |
||
| 280 | * @return array|false |
||
| 281 | */ |
||
| 282 | private function addCleanParam() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Explode Clean-Param rule |
||
| 297 | * |
||
| 298 | * @param string $rule |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | private function explodeCleanParamRule($rule) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * URL encoder according to RFC 3986 |
||
| 317 | * Returns a string containing the encoded URL with disallowed characters converted to their percentage encodings. |
||
| 318 | * @link http://publicmind.in/blog/url-encoding/ |
||
| 319 | * |
||
| 320 | * @param string $url |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | private function urlEncode($url) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Add Host |
||
| 351 | * |
||
| 352 | * @return array|false |
||
| 353 | */ |
||
| 354 | private function addHost() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Validate host name |
||
| 380 | * |
||
| 381 | * @link http://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php |
||
| 382 | * |
||
| 383 | * @param string $host |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | private static function urlValidateHost($host) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Validate URL scheme |
||
| 398 | * |
||
| 399 | * @param string $scheme |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | private static function urlValidateScheme($scheme) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Add Sitemap |
||
| 413 | * |
||
| 414 | * @return array|false |
||
| 415 | */ |
||
| 416 | private function addSitemap() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Validate URL |
||
| 433 | * |
||
| 434 | * @param string $url |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | public function urlValidate($url) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Set User-Agent(s) |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | private function setUserAgent() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Assign User-Agent dependent rules to the User-Agent arrays |
||
| 468 | * |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | private function assignUserAgent() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Get rules |
||
| 485 | */ |
||
| 486 | public function getRules() |
||
| 490 | } |
||
| 491 |