Complex classes like Cursor 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 Cursor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Cursor |
||
| 17 | { |
||
| 18 | public const INDENT_LEVEL = 4; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $line; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $length; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int |
||
| 32 | * |
||
| 33 | * It's possible for this to be 1 char past the end, meaning we've parsed all chars and have |
||
| 34 | * reached the end. In this state, any character-returning method MUST return null. |
||
| 35 | */ |
||
| 36 | private $currentPosition = 0; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | private $column = 0; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | private $indent = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $previousPosition = 0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int|null |
||
| 55 | */ |
||
| 56 | private $nextNonSpaceCache; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private $partiallyConsumedTab = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | private $lineContainsTabs; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | private $isMultibyte; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array<int, string> |
||
| 75 | */ |
||
| 76 | private $charCache = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $line The line being parsed (ASCII or UTF-8) |
||
| 80 | */ |
||
| 81 | 3000 | public function __construct(string $line) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the position of the next character which is not a space (or tab) |
||
| 91 | * |
||
| 92 | * @return int |
||
| 93 | */ |
||
| 94 | 2694 | public function getNextNonSpacePosition(): int |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Returns the next character which isn't a space (or tab) |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 2460 | public function getNextNonSpaceCharacter(): ?string |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Calculates the current indent (number of spaces after current position) |
||
| 133 | * |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | 2574 | public function getIndent(): int |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Whether the cursor is indented to INDENT_LEVEL |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | 2514 | public function isIndented(): bool |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param int|null $index |
||
| 157 | * |
||
| 158 | * @return string|null |
||
| 159 | */ |
||
| 160 | 2754 | public function getCharacter(?int $index = null): ?string |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Returns the next character (or null, if none) without advancing forwards |
||
| 184 | * |
||
| 185 | * @param int $offset |
||
| 186 | * |
||
| 187 | * @return string|null |
||
| 188 | */ |
||
| 189 | 1389 | public function peek(int $offset = 1): ?string |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Whether the remainder is blank |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | 2508 | public function isBlank(): bool |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Move the cursor forwards |
||
| 206 | */ |
||
| 207 | 78 | public function advance(): void |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Move the cursor forwards |
||
| 214 | * |
||
| 215 | * @param int $characters Number of characters to advance by |
||
| 216 | * @param bool $advanceByColumns Whether to advance by columns instead of spaces |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | 2835 | public function advanceBy(int $characters, bool $advanceByColumns = false): void |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Advances the cursor by a single space or tab, if present |
||
| 285 | * |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | 390 | public function advanceBySpaceOrTab(): bool |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Parse zero or more space/tab characters |
||
| 303 | * |
||
| 304 | * @return int Number of positions moved |
||
| 305 | */ |
||
| 306 | 2421 | public function advanceToNextNonSpaceOrTab(): int |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Parse zero or more space characters, including at most one newline. |
||
| 317 | * |
||
| 318 | * Tab characters are not parsed with this function. |
||
| 319 | * |
||
| 320 | * @return int Number of positions moved |
||
| 321 | */ |
||
| 322 | 483 | public function advanceToNextNonSpaceOrNewline(): int |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Move the position to the very end of the line |
||
| 347 | * |
||
| 348 | * @return int The number of characters moved |
||
| 349 | */ |
||
| 350 | 87 | public function advanceToEnd(): int |
|
| 359 | |||
| 360 | 2616 | public function getRemainder(): string |
|
| 380 | |||
| 381 | 2427 | public function getLine(): string |
|
| 385 | |||
| 386 | 525 | public function isAtEnd(): bool |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Try to match a regular expression |
||
| 393 | * |
||
| 394 | * Returns the matching text and advances to the end of that match |
||
| 395 | * |
||
| 396 | * @param string $regex |
||
| 397 | * |
||
| 398 | * @return string|null |
||
| 399 | */ |
||
| 400 | 2502 | public function match(string $regex): ?string |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
| 429 | * |
||
| 430 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
| 431 | * passing it back into restoreState(), as the number of values and their |
||
| 432 | * contents may change in any future release without warning. |
||
| 433 | * |
||
| 434 | * @return array<mixed> |
||
| 435 | */ |
||
| 436 | 1236 | public function saveState() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Restore the cursor to a previous state. |
||
| 450 | * |
||
| 451 | * Pass in the value previously obtained by calling saveState(). |
||
| 452 | * |
||
| 453 | * @param array<mixed> $state |
||
| 454 | * |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | 909 | public function restoreState($state): void |
|
| 468 | |||
| 469 | 753 | public function getPosition(): int |
|
| 473 | |||
| 474 | 480 | public function getPreviousText(): string |
|
| 478 | |||
| 479 | 429 | public function getSubstring(int $start, ?int $length = null): string |
|
| 489 | |||
| 490 | 285 | public function getColumn(): int |
|
| 494 | } |
||
| 495 |