Complex classes like Normaliser 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 Normaliser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Normaliser |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var StringIterator |
||
| 20 | */ |
||
| 21 | private $string; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $normalisedString = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | private $commentOptions; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $comment = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Comments that are within a multiline context, i.e. between brackets. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $multilineComments = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Normaliser constructor. |
||
| 47 | */ |
||
| 48 | 40 | public function __construct(string $zone, int $commentOptions = Comments::NONE) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @throws ParseException |
||
| 59 | */ |
||
| 60 | 40 | public static function normalise(string $zone, int $includeComments = Comments::NONE): string |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @throws ParseException |
||
| 67 | */ |
||
| 68 | 40 | public function process(): string |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Parses the comments. |
||
| 84 | * |
||
| 85 | * @param int $condition |
||
| 86 | */ |
||
| 87 | 40 | private function handleComment($condition = Comments::ALL): void |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Handle text inside of double quotations. When this function is called, the String pointer MUST be at the |
||
| 109 | * double quotation mark. |
||
| 110 | * |
||
| 111 | * @throws ParseException |
||
| 112 | */ |
||
| 113 | 40 | private function handleTxt(): void |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Move multi-line records onto single line. |
||
| 141 | * |
||
| 142 | * @throws ParseException |
||
| 143 | */ |
||
| 144 | 40 | private function handleMultiline(): void |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Remove superfluous whitespace characters from string. |
||
| 176 | * |
||
| 177 | * @throws \UnexpectedValueException |
||
| 178 | */ |
||
| 179 | 37 | private function removeWhitespace(): void |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Add current entry to normalisedString and moves iterator to next entry. |
||
| 197 | */ |
||
| 198 | 40 | private function append(): void |
|
| 209 | |||
| 210 | 10 | private function appendComment(): void |
|
| 242 | } |
||
| 243 |