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 |
||
| 14 | class Cursor |
||
| 15 | { |
||
| 16 | const INDENT_LEVEL = 4; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $line; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var int |
||
| 25 | */ |
||
| 26 | private $length; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int |
||
| 30 | * |
||
| 31 | * It's possible for this to be 1 char past the end, meaning we've parsed all chars and have |
||
| 32 | * reached the end. In this state, any character-returning method MUST return null. |
||
| 33 | */ |
||
| 34 | private $currentPosition = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $column = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | private $indent = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | private $previousPosition = 0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int|null |
||
| 53 | */ |
||
| 54 | private $nextNonSpaceCache; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | private $partiallyConsumedTab = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $encoding; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | private $lineContainsTabs; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | private $isMultibyte; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array<int, string> |
||
| 78 | */ |
||
| 79 | private $charCache = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $line The line being parsed |
||
| 83 | * @param string $encoding The encoding of that line |
||
| 84 | */ |
||
| 85 | 2478 | public function __construct(string $line, string $encoding = 'UTF-8') |
|
| 93 | |||
| 94 | 60 | public function getEncoding(): string |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the position of the next character which is not a space (or tab) |
||
| 101 | * |
||
| 102 | * @return int |
||
| 103 | */ |
||
| 104 | 2208 | public function getNextNonSpacePosition(): int |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the next character which isn't a space (or tab) |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 2001 | public function getNextNonSpaceCharacter(): ?string |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Calculates the current indent (number of spaces after current position) |
||
| 143 | * |
||
| 144 | * @return int |
||
| 145 | */ |
||
| 146 | 2088 | public function getIndent(): int |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Whether the cursor is indented to INDENT_LEVEL |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | 2028 | public function isIndented(): bool |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @param int|null $index |
||
| 167 | * |
||
| 168 | * @return string|null |
||
| 169 | */ |
||
| 170 | 2262 | public function getCharacter(?int $index = null): ?string |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Returns the next character (or null, if none) without advancing forwards |
||
| 190 | * |
||
| 191 | * @param int $offset |
||
| 192 | * |
||
| 193 | * @return string|null |
||
| 194 | */ |
||
| 195 | 1065 | public function peek(int $offset = 1): ?string |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Whether the remainder is blank |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | 2046 | public function isBlank(): bool |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Move the cursor forwards |
||
| 212 | */ |
||
| 213 | 846 | public function advance() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Move the cursor forwards |
||
| 220 | * |
||
| 221 | * @param int $characters Number of characters to advance by |
||
| 222 | * @param bool $advanceByColumns Whether to advance by columns instead of spaces |
||
| 223 | */ |
||
| 224 | 2334 | public function advanceBy(int $characters, bool $advanceByColumns = false) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Advances the cursor by a single space or tab, if present |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | 348 | public function advanceBySpaceOrTab(): bool |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Parse zero or more space/tab characters |
||
| 299 | * |
||
| 300 | * @return int Number of positions moved |
||
| 301 | */ |
||
| 302 | 1935 | public function advanceToNextNonSpaceOrTab(): int |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Parse zero or more space characters, including at most one newline. |
||
| 313 | * |
||
| 314 | * Tab characters are not parsed with this function. |
||
| 315 | * |
||
| 316 | * @return int Number of positions moved |
||
| 317 | */ |
||
| 318 | 477 | public function advanceToNextNonSpaceOrNewline(): int |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Move the position to the very end of the line |
||
| 338 | * |
||
| 339 | * @return int The number of characters moved |
||
| 340 | */ |
||
| 341 | 84 | public function advanceToEnd(): int |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 2130 | public function getRemainder(): string |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | 1977 | public function getLine(): string |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | 447 | public function isAtEnd(): bool |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Try to match a regular expression |
||
| 389 | * |
||
| 390 | * Returns the matching text and advances to the end of that match |
||
| 391 | * |
||
| 392 | * @param string $regex |
||
| 393 | * |
||
| 394 | * @return string|null |
||
| 395 | */ |
||
| 396 | 1998 | public function match(string $regex): ?string |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
| 425 | * |
||
| 426 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
| 427 | * passing it back into restoreState(), as the number of values and their |
||
| 428 | * contents may change in any future release without warning. |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | 1077 | public function saveState() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Restore the cursor to a previous state. |
||
| 446 | * |
||
| 447 | * Pass in the value previously obtained by calling saveState(). |
||
| 448 | * |
||
| 449 | * @param array $state |
||
| 450 | */ |
||
| 451 | 843 | public function restoreState($state) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @return int |
||
| 465 | */ |
||
| 466 | 699 | public function getPosition(): int |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | 921 | public function getPreviousText(): string |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @return int |
||
| 481 | */ |
||
| 482 | 249 | public function getColumn(): int |
|
| 486 | } |
||
| 487 |