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 |
||
| 14 | class Normaliser |
||
| 15 | { |
||
| 16 | const COMMENTS_NONE = 0; |
||
| 17 | const COMMENTS_END_OF_RECORD_ENTRY = 1; |
||
| 18 | const COMMENTS_WITHIN_MULTILINE = 2; |
||
| 19 | const COMMENTS_WITHOUT_RECORD_ENTRY = 4; |
||
| 20 | const COMMENTS_ALL = 7; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var StringIterator |
||
| 24 | */ |
||
| 25 | private $string; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $normalisedString = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | private $commentOptions; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $comment = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Comments that are within a multiline context, i.e. between brackets. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $multilineComments = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Normaliser constructor. |
||
| 51 | * |
||
| 52 | * @param string $zone |
||
| 53 | * @param int $commentOptions |
||
| 54 | */ |
||
| 55 | 27 | public function __construct(string $zone, int $commentOptions = self::COMMENTS_NONE) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $zone |
||
| 66 | * @param int $includeComments |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | * |
||
| 70 | * @throws ParseException |
||
| 71 | */ |
||
| 72 | 27 | public static function normalise(string $zone, int $includeComments = self::COMMENTS_NONE): string |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @return string |
||
| 79 | * |
||
| 80 | * @throws ParseException |
||
| 81 | */ |
||
| 82 | 27 | public function process(): string |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Parses the comments. |
||
| 98 | * |
||
| 99 | * @param int $condition |
||
| 100 | */ |
||
| 101 | 27 | private function handleComment($condition = self::COMMENTS_ALL): void |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Handle text inside of double quotations. When this function is called, the String pointer MUST be at the |
||
| 123 | * double quotation mark. |
||
| 124 | * |
||
| 125 | * @throws ParseException |
||
| 126 | */ |
||
| 127 | 27 | private function handleTxt(): void |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Move multi-line records onto single line. |
||
| 155 | * |
||
| 156 | * @throws ParseException |
||
| 157 | */ |
||
| 158 | 27 | private function handleMultiline(): void |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Remove superfluous whitespace characters from string. |
||
| 190 | * |
||
| 191 | * @throws \UnexpectedValueException |
||
| 192 | */ |
||
| 193 | 24 | private function removeWhitespace(): void |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Add current entry to normalisedString and moves iterator to next entry. |
||
| 213 | */ |
||
| 214 | 27 | private function append(): void |
|
| 225 | |||
| 226 | 8 | private function appendComment(): void |
|
| 258 | } |
||
| 259 |