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 | 14 | public function __construct(array $rdataHandlers = []) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $name |
||
| 74 | * @param string $zone |
||
| 75 | * |
||
| 76 | * @return Zone |
||
| 77 | * |
||
| 78 | * @throws ParseException |
||
| 79 | */ |
||
| 80 | 13 | public static function parse(string $name, string $zone): Zone |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $name |
||
| 87 | * @param string $string |
||
| 88 | * |
||
| 89 | * @return Zone |
||
| 90 | * |
||
| 91 | * @throws ParseException |
||
| 92 | */ |
||
| 93 | 14 | public function makeZone(string $name, string $string): Zone |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $line |
||
| 107 | * |
||
| 108 | * @throws ParseException |
||
| 109 | */ |
||
| 110 | 14 | private function processLine(string $line): void |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param ResourceRecordIterator $iterator |
||
| 127 | * |
||
| 128 | * @throws ParseException |
||
| 129 | */ |
||
| 130 | 14 | private function processEntry(ResourceRecordIterator $iterator): void |
|
| 165 | |||
| 166 | /** |
||
| 167 | * If no domain-name, TTL, or class is set on the record, populate object with last stated value. |
||
| 168 | * |
||
| 169 | * @see https://www.ietf.org/rfc/rfc1035 Section 5.1 |
||
| 170 | */ |
||
| 171 | 11 | private function populateWithLastStated(): void |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Processes control entries at the top of a BIND record, i.e. $ORIGIN, $TTL, $INCLUDE, etc. |
||
| 194 | * |
||
| 195 | * @param ResourceRecordIterator $iterator |
||
| 196 | */ |
||
| 197 | 7 | private function processControlEntry(ResourceRecordIterator $iterator): void |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Determine if iterant is a resource name. |
||
| 207 | * |
||
| 208 | * @param ResourceRecordIterator $iterator |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | 14 | private function isResourceName(ResourceRecordIterator $iterator): bool |
|
| 227 | |||
| 228 | 14 | private function isClass(ResourceRecordIterator $iterator, $origin = null): bool |
|
| 244 | |||
| 245 | 14 | private function isType(ResourceRecordIterator $iterator): bool |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Determine if iterant is a control entry such as $TTL, $ORIGIN, $INCLUDE, etcetera. |
||
| 252 | * |
||
| 253 | * @param ResourceRecordIterator $iterator |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | 14 | private function isControlEntry(ResourceRecordIterator $iterator): bool |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Determine if the iterant is a TTL (i.e. it is an integer). |
||
| 264 | * |
||
| 265 | * @param ResourceRecordIterator $iterator |
||
| 266 | * @param string $origin |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 14 | private function isTTL(ResourceRecordIterator $iterator, $origin = null): bool |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param ResourceRecordIterator $iterator |
||
| 289 | * |
||
| 290 | * @return RData\RdataInterface |
||
| 291 | * |
||
| 292 | * @throws ParseException |
||
| 293 | */ |
||
| 294 | 13 | private function extractRdata(ResourceRecordIterator $iterator): Rdata\RdataInterface |
|
| 309 | |||
| 310 | /** |
||
| 311 | * This handler addresses the special case where an integer resource name could be confused for a TTL, for instance: |
||
| 312 | * 50 IN PTR mx1.acme.com. |
||
| 313 | * |
||
| 314 | * In the above, if the integer is below 256 then it is assumed to represent an octet of an IPv4 address. |
||
| 315 | * |
||
| 316 | * @param ResourceRecordIterator $iterator |
||
| 317 | * |
||
| 318 | * @return Rdata\PTR |
||
| 319 | */ |
||
| 320 | 3 | private function ptrHandler(ResourceRecordIterator $iterator): Rdata\PTR |
|
| 336 | } |
||
| 337 |