Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Parser implements RobotsTxtInterface |
||
| 12 | { |
||
| 13 | use UrlToolbox; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Max length for each rule |
||
| 17 | */ |
||
| 18 | protected $maxRuleLength = self::MAX_LENGTH_RULE; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * RAW robots.txt content |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $raw = ''; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Rule array |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $rules = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * User-Agents |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $userAgents = [self::USER_AGENT]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Current line |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $line = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Previous directive |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $previous; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Current Directive |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $directive; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Current Rule |
||
| 58 | * @var array|string |
||
| 59 | */ |
||
| 60 | private $rule; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Constructor |
||
| 64 | * |
||
| 65 | * @param string $content - file content |
||
| 66 | * @param string $encoding - character encoding |
||
| 67 | * @param integer|null $byteLimit - maximum of bytes to parse |
||
| 68 | * @param integer|null $maxRuleLength - max length of each rule |
||
| 69 | * @throws Exceptions\ParserException |
||
| 70 | */ |
||
| 71 | public function __construct($content, $encoding = self::ENCODING, $byteLimit = self::BYTE_LIMIT, $maxRuleLength = self::MAX_LENGTH_RULE) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Parse robots.txt |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | private function parseTxt() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Generate Directive:Rule pair |
||
| 113 | * |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | private function generateRulePair() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Directives and sub directives |
||
| 136 | * |
||
| 137 | * @param string|null $parent |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | private function directives($parent = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Parse line |
||
| 171 | * |
||
| 172 | * @param string|null $parent |
||
| 173 | * @return array|false |
||
| 174 | */ |
||
| 175 | private function parseLine($parent = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Add value to directive |
||
| 197 | * |
||
| 198 | * @return array|false |
||
| 199 | */ |
||
| 200 | private function add() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Add an Allow or Disallow rule |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | private function addDisAllow() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Add float value |
||
| 246 | * |
||
| 247 | * @return array|false |
||
| 248 | */ |
||
| 249 | private function addFloat() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Add Clean-Param record |
||
| 261 | * |
||
| 262 | * @return array|false |
||
| 263 | */ |
||
| 264 | private function addCleanParam() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Explode Clean-Param rule |
||
| 279 | * |
||
| 280 | * @param string $rule |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | private function explodeCleanParamRule($rule) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Add Host |
||
| 299 | * |
||
| 300 | * @return array|false |
||
| 301 | */ |
||
| 302 | private function addHost() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Add Sitemap |
||
| 329 | * |
||
| 330 | * @return array|false |
||
| 331 | */ |
||
| 332 | private function addSitemap() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Set User-Agent(s) |
||
| 349 | * |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | private function setUserAgent() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Assign User-Agent dependent rules to the User-Agent arrays |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | private function assignUserAgent() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get rules |
||
| 385 | */ |
||
| 386 | public function export() |
||
| 390 | } |
||
| 391 |