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 | * @param string $line |
||
| 78 | */ |
||
| 79 | 2430 | public function __construct($line) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Returns the position of the next character which is not a space (or tab) |
||
| 90 | * |
||
| 91 | * @deprecated Use getNextNonSpacePosition() instead |
||
| 92 | * |
||
| 93 | * @return int |
||
| 94 | */ |
||
| 95 | 16 | public function getFirstNonSpacePosition() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the position of the next character which is not a space (or tab) |
||
| 104 | * |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | 2112 | public function getNextNonSpacePosition() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the next character which isn't a space (or tab) |
||
| 136 | * |
||
| 137 | * @deprecated Use getNextNonSpaceCharacter() instead |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 16 | public function getFirstNonSpaceCharacter() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Returns the next character which isn't a space (or tab) |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 1905 | public function getNextNonSpaceCharacter() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Calculates the current indent (number of spaces after current position) |
||
| 160 | * |
||
| 161 | * @return int |
||
| 162 | */ |
||
| 163 | 1890 | public function getIndent() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Whether the cursor is indented to INDENT_LEVEL |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | 1932 | public function isIndented() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param int|null $index |
||
| 184 | * |
||
| 185 | * @return string|null |
||
| 186 | */ |
||
| 187 | 2205 | public function getCharacter($index = null) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the next character (or null, if none) without advancing forwards |
||
| 203 | * |
||
| 204 | * @param int $offset |
||
| 205 | * |
||
| 206 | * @return string|null |
||
| 207 | */ |
||
| 208 | 1014 | public function peek($offset = 1) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Whether the remainder is blank |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | 1950 | public function isBlank() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Move the cursor forwards |
||
| 225 | */ |
||
| 226 | 789 | public function advance() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Move the cursor forwards |
||
| 233 | * |
||
| 234 | * @param int $characters Number of characters to advance by |
||
| 235 | * @param bool $advanceByColumns Whether to advance by columns instead of spaces |
||
| 236 | */ |
||
| 237 | 2286 | public function advanceBy($characters, $advanceByColumns = false) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Advances the cursor by a single space or tab, if present |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 333 | public function advanceBySpaceOrTab() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Advances the cursor while the given character is matched |
||
| 314 | * |
||
| 315 | * @param string $character Character to match |
||
| 316 | * @param int|null $maximumCharactersToAdvance Maximum number of characters to advance before giving up |
||
| 317 | * |
||
| 318 | * @return int Number of positions moved (0 if unsuccessful) |
||
| 319 | * |
||
| 320 | * @deprecated Use match() instead |
||
| 321 | */ |
||
| 322 | 30 | public function advanceWhileMatches($character, $maximumCharactersToAdvance = null) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Parse zero or more space characters, including at most one newline. |
||
| 350 | * |
||
| 351 | * @deprecated Use advanceToNextNonSpaceOrNewline() instead |
||
| 352 | */ |
||
| 353 | 36 | public function advanceToFirstNonSpace() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Parse zero or more space/tab characters |
||
| 362 | * |
||
| 363 | * @return int Number of positions moved |
||
| 364 | */ |
||
| 365 | 1842 | public function advanceToNextNonSpaceOrTab() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Parse zero or more space characters, including at most one newline. |
||
| 376 | * |
||
| 377 | * Tab characters are not parsed with this function. |
||
| 378 | * |
||
| 379 | * @return int Number of positions moved |
||
| 380 | */ |
||
| 381 | 441 | public function advanceToNextNonSpaceOrNewline() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Move the position to the very end of the line |
||
| 401 | * |
||
| 402 | * @return int The number of characters moved |
||
| 403 | */ |
||
| 404 | 84 | public function advanceToEnd() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | 2037 | public function getRemainder() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 1887 | public function getLine() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * @return bool |
||
| 444 | */ |
||
| 445 | 411 | public function isAtEnd() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Try to match a regular expression |
||
| 452 | * |
||
| 453 | * Returns the matching text and advances to the end of that match |
||
| 454 | * |
||
| 455 | * @param string $regex |
||
| 456 | * |
||
| 457 | * @return string|null |
||
| 458 | */ |
||
| 459 | 1902 | public function match($regex) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
| 487 | * |
||
| 488 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
| 489 | * passing it back into restoreState(), as the number of values and their |
||
| 490 | * contents may change in any future release without warning. |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | 1011 | public function saveState() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Restore the cursor to a previous state. |
||
| 508 | * |
||
| 509 | * Pass in the value previously obtained by calling saveState(). |
||
| 510 | * |
||
| 511 | * @param array $state |
||
| 512 | */ |
||
| 513 | 795 | public function restoreState($state) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * @return int |
||
| 527 | */ |
||
| 528 | 648 | public function getPosition() |
|
| 532 | |||
| 533 | /** |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | 870 | public function getPreviousText() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * @return int |
||
| 543 | */ |
||
| 544 | 240 | public function getColumn() |
|
| 548 | } |
||
| 549 |
If you suppress an error, we recommend checking for the error condition explicitly: