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 |
||
| 19 | class Parser |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $string; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Zone |
||
| 28 | */ |
||
| 29 | private $zone; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Array of methods that take an ArrayIterator and return an Rdata object. The array key is the Rdata type. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $rdataHandlers = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ResourceRecord |
||
| 40 | */ |
||
| 41 | private $currentResourceRecord; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $lastStatedDomain; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $lastStatedTtl; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $lastStatedClass; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Parser constructor. |
||
| 60 | * |
||
| 61 | * @param array $rdataHandlers |
||
| 62 | */ |
||
| 63 | 17 | public function __construct(array $rdataHandlers = []) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $name |
||
| 74 | * @param string $zone |
||
| 75 | * @param int $commentOptions |
||
| 76 | * |
||
| 77 | * @return Zone |
||
| 78 | * |
||
| 79 | * @throws ParseException |
||
| 80 | */ |
||
| 81 | 16 | public static function parse(string $name, string $zone, int $commentOptions = Normaliser::COMMENTS_NONE): Zone |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $name |
||
| 88 | * @param string $string |
||
| 89 | * @param int $commentOptions |
||
| 90 | * |
||
| 91 | * @return Zone |
||
| 92 | * |
||
| 93 | * @throws ParseException |
||
| 94 | */ |
||
| 95 | 17 | public function makeZone(string $name, string $string, int $commentOptions = Normaliser::COMMENTS_NONE): Zone |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $line |
||
| 109 | * |
||
| 110 | * @throws ParseException |
||
| 111 | */ |
||
| 112 | 17 | private function processLine(string $line): void |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param ResourceRecordIterator $iterator |
||
| 139 | * |
||
| 140 | * @throws ParseException |
||
| 141 | */ |
||
| 142 | 17 | private function processEntry(ResourceRecordIterator $iterator): void |
|
| 177 | |||
| 178 | /** |
||
| 179 | * If no domain-name, TTL, or class is set on the record, populate object with last stated value. |
||
| 180 | * |
||
| 181 | * @see https://www.ietf.org/rfc/rfc1035 Section 5.1 |
||
| 182 | */ |
||
| 183 | 14 | private function populateWithLastStated(): void |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Processes control entries at the top of a BIND record, i.e. $ORIGIN, $TTL, $INCLUDE, etc. |
||
| 206 | * |
||
| 207 | * @param ResourceRecordIterator $iterator |
||
| 208 | */ |
||
| 209 | 9 | private function processControlEntry(ResourceRecordIterator $iterator): void |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Determine if iterant is a resource name. |
||
| 219 | * |
||
| 220 | * @param ResourceRecordIterator $iterator |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | 17 | private function isResourceName(ResourceRecordIterator $iterator): bool |
|
| 239 | |||
| 240 | 17 | private function isClass(ResourceRecordIterator $iterator, $origin = null): bool |
|
| 256 | |||
| 257 | 17 | private function isType(ResourceRecordIterator $iterator): bool |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Determine if iterant is a control entry such as $TTL, $ORIGIN, $INCLUDE, etcetera. |
||
| 264 | * |
||
| 265 | * @param ResourceRecordIterator $iterator |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 17 | private function isControlEntry(ResourceRecordIterator $iterator): bool |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Determine if the iterant is a TTL (i.e. it is an integer). |
||
| 276 | * |
||
| 277 | * @param ResourceRecordIterator $iterator |
||
| 278 | * @param string $origin |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 17 | private function isTTL(ResourceRecordIterator $iterator, $origin = null): bool |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Split a DNS zone line into a resource record and a comment. |
||
| 301 | * |
||
| 302 | * @param string $rr |
||
| 303 | * |
||
| 304 | * @return array [$entry, $comment] |
||
| 305 | */ |
||
| 306 | 17 | private function extractComment(string $rr): array |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param ResourceRecordIterator $iterator |
||
| 352 | * |
||
| 353 | * @return RData\RdataInterface |
||
| 354 | * |
||
| 355 | * @throws ParseException |
||
| 356 | */ |
||
| 357 | 16 | private function extractRdata(ResourceRecordIterator $iterator): Rdata\RdataInterface |
|
| 372 | |||
| 373 | /** |
||
| 374 | * This handler addresses the special case where an integer resource name could be confused for a TTL, for instance: |
||
| 375 | * 50 IN PTR mx1.acme.com. |
||
| 376 | * |
||
| 377 | * In the above, if the integer is below 256 then it is assumed to represent an octet of an IPv4 address. |
||
| 378 | * |
||
| 379 | * @param ResourceRecordIterator $iterator |
||
| 380 | * |
||
| 381 | * @return Rdata\PTR |
||
| 382 | */ |
||
| 383 | 3 | private function ptrHandler(ResourceRecordIterator $iterator): Rdata\PTR |
|
| 399 | } |
||
| 400 |