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 | * Default User-Agent |
||
15 | */ |
||
16 | const USERAGENT_DEFAULT = '*'; |
||
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'; |
||
28 | const DIRECTIVE_CLEAN_PARAM = 'clean-param'; |
||
29 | const DIRECTIVE_CRAWL_DELAY = 'crawl-delay'; |
||
30 | const DIRECTIVE_DISALLOW = 'disallow'; |
||
31 | const DIRECTIVE_HOST = 'host'; |
||
32 | const DIRECTIVE_SITEMAP = 'sitemap'; |
||
33 | const DIRECTIVE_USERAGENT = 'user-agent'; |
||
34 | |||
35 | /** |
||
36 | * User-Agent dependent directives |
||
37 | */ |
||
38 | const USERAGENT_DEPENDENT_DIRECTIVES = [ |
||
39 | self::DIRECTIVE_ALLOW, |
||
40 | self::DIRECTIVE_CACHE_DELAY, |
||
41 | self::DIRECTIVE_CRAWL_DELAY, |
||
42 | self::DIRECTIVE_DISALLOW, |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * RAW robots.txt content |
||
47 | * @var string |
||
48 | */ |
||
49 | private $raw = ''; |
||
50 | |||
51 | /** |
||
52 | * Rule array |
||
53 | * @var array |
||
54 | */ |
||
55 | private $rules = []; |
||
56 | |||
57 | /** |
||
58 | * User-Agents |
||
59 | * @var array |
||
60 | */ |
||
61 | private $userAgents = [self::USERAGENT_DEFAULT]; |
||
62 | |||
63 | /** |
||
64 | * Previous directive |
||
65 | * @var string |
||
66 | */ |
||
67 | private $previous; |
||
68 | |||
69 | /** |
||
70 | * Current Directive |
||
71 | * @var string |
||
72 | */ |
||
73 | private $directive; |
||
74 | |||
75 | /** |
||
76 | * Current Rule |
||
77 | * @var string |
||
78 | */ |
||
79 | private $rule; |
||
80 | |||
81 | private $line = ''; |
||
82 | |||
83 | /** |
||
84 | * Constructor |
||
85 | * |
||
86 | * @param string $content - file content |
||
87 | * @throws TxtParserException |
||
88 | */ |
||
89 | public function __construct($content) |
||
101 | |||
102 | /** |
||
103 | * Parse robots.txt |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | private function parseTxt() |
||
128 | |||
129 | /** |
||
130 | * Generate Directive:Rule pair |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | private function generateRulePair() |
||
154 | |||
155 | /** |
||
156 | * Directives |
||
157 | * |
||
158 | * @param string|null $parent |
||
159 | * @return array |
||
160 | */ |
||
161 | private function directives($parent = null) |
||
184 | |||
185 | |||
186 | /** |
||
187 | * Parse line |
||
188 | * |
||
189 | * @param string|null $parent |
||
190 | * @return array|false |
||
191 | */ |
||
192 | private function parseLine($parent = null) |
||
211 | |||
212 | /** |
||
213 | * Add value to directive |
||
214 | * |
||
215 | * @return array|false |
||
216 | */ |
||
217 | private function add() |
||
237 | |||
238 | /** |
||
239 | * Set User-Agent(s) |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | private function setUserAgent() |
||
256 | |||
257 | /** |
||
258 | * Add float value |
||
259 | * |
||
260 | * @return array|false |
||
261 | */ |
||
262 | private function addFloat() |
||
271 | |||
272 | /** |
||
273 | * Add Host |
||
274 | * |
||
275 | * @return array|false |
||
276 | */ |
||
277 | private function addHost() |
||
300 | |||
301 | /** |
||
302 | * URL encoder according to RFC 3986 |
||
303 | * Returns a string containing the encoded URL with disallowed characters converted to their percentage encodings. |
||
304 | * @link http://publicmind.in/blog/url-encoding/ |
||
305 | * |
||
306 | * @param string $url |
||
307 | * @return string |
||
308 | */ |
||
309 | private function UrlEncode($url) |
||
334 | |||
335 | /** |
||
336 | * Validate host name |
||
337 | * |
||
338 | * @link http://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php |
||
339 | * |
||
340 | * @param string $host |
||
341 | * @return bool |
||
342 | */ |
||
343 | private static function UrlValidateHost($host) |
||
352 | |||
353 | /** |
||
354 | * Validate URL scheme |
||
355 | * |
||
356 | * @param string $scheme |
||
357 | * @return bool |
||
358 | */ |
||
359 | private static function UrlValidateScheme($scheme) |
||
367 | |||
368 | /** |
||
369 | * Add Sitemap |
||
370 | * |
||
371 | * @return array|false |
||
372 | */ |
||
373 | private function addSitemap() |
||
384 | |||
385 | /** |
||
386 | * Validate URL |
||
387 | * |
||
388 | * @param string $url |
||
389 | * @return bool |
||
390 | */ |
||
391 | public function UrlValidate($url) |
||
400 | |||
401 | /** |
||
402 | * Add Clean-Param record |
||
403 | * |
||
404 | * @return array |
||
405 | */ |
||
406 | private function addCleanParam() |
||
415 | |||
416 | /** |
||
417 | * Explode Clean-Param rule |
||
418 | * |
||
419 | * @param string $rule |
||
420 | * @return array |
||
421 | */ |
||
422 | private function explodeCleanParamRule($rule) |
||
438 | |||
439 | /** |
||
440 | * Add an Allow or Disallow rule |
||
441 | * |
||
442 | * @return array |
||
443 | */ |
||
444 | private function addDisAllow() |
||
461 | |||
462 | /** |
||
463 | * Assign User-Agent dependent rules to the User-Agent arrays |
||
464 | * |
||
465 | * @return array |
||
466 | */ |
||
467 | private function assignUserAgent() |
||
478 | |||
479 | /** |
||
480 | * Get rules |
||
481 | */ |
||
482 | public function getRules() |
||
486 | } |
||
487 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..