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 | 2385 | public function __construct($line) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Returns the position of the next character which is not a space (or tab) |
||
| 90 | * |
||
| 91 | * @return int |
||
| 92 | */ |
||
| 93 | 2112 | public function getNextNonSpacePosition() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the next character which isn't a space (or tab) |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | 1905 | public function getNextNonSpaceCharacter() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Calculates the current indent (number of spaces after current position) |
||
| 132 | * |
||
| 133 | * @return int |
||
| 134 | */ |
||
| 135 | 1890 | public function getIndent() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Whether the cursor is indented to INDENT_LEVEL |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 1932 | public function isIndented() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param int|null $index |
||
| 156 | * |
||
| 157 | * @return string|null |
||
| 158 | */ |
||
| 159 | 2166 | public function getCharacter($index = null) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the next character (or null, if none) without advancing forwards |
||
| 175 | * |
||
| 176 | * @param int $offset |
||
| 177 | * |
||
| 178 | * @return string|null |
||
| 179 | */ |
||
| 180 | 1014 | public function peek($offset = 1) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Whether the remainder is blank |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | 1950 | public function isBlank() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Move the cursor forwards |
||
| 197 | */ |
||
| 198 | 789 | public function advance() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Move the cursor forwards |
||
| 205 | * |
||
| 206 | * @param int $characters Number of characters to advance by |
||
| 207 | * @param bool $advanceByColumns Whether to advance by columns instead of spaces |
||
| 208 | */ |
||
| 209 | 2241 | public function advanceBy($characters, $advanceByColumns = false) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Advances the cursor by a single space or tab, if present |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 333 | public function advanceBySpaceOrTab() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Parse zero or more space/tab characters |
||
| 286 | * |
||
| 287 | * @return int Number of positions moved |
||
| 288 | */ |
||
| 289 | 1842 | public function advanceToNextNonSpaceOrTab() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Parse zero or more space characters, including at most one newline. |
||
| 300 | * |
||
| 301 | * Tab characters are not parsed with this function. |
||
| 302 | * |
||
| 303 | * @return int Number of positions moved |
||
| 304 | */ |
||
| 305 | 441 | public function advanceToNextNonSpaceOrNewline() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Move the position to the very end of the line |
||
| 325 | * |
||
| 326 | * @return int The number of characters moved |
||
| 327 | */ |
||
| 328 | 84 | public function advanceToEnd() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | 2037 | public function getRemainder() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | 1887 | public function getLine() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @return bool |
||
| 368 | */ |
||
| 369 | 411 | public function isAtEnd() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Try to match a regular expression |
||
| 376 | * |
||
| 377 | * Returns the matching text and advances to the end of that match |
||
| 378 | * |
||
| 379 | * @param string $regex |
||
| 380 | * |
||
| 381 | * @return string|null |
||
| 382 | */ |
||
| 383 | 1902 | public function match($regex) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
| 411 | * |
||
| 412 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
| 413 | * passing it back into restoreState(), as the number of values and their |
||
| 414 | * contents may change in any future release without warning. |
||
| 415 | * |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | 1011 | public function saveState() |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Restore the cursor to a previous state. |
||
| 432 | * |
||
| 433 | * Pass in the value previously obtained by calling saveState(). |
||
| 434 | * |
||
| 435 | * @param array $state |
||
| 436 | */ |
||
| 437 | 795 | public function restoreState($state) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @return int |
||
| 451 | */ |
||
| 452 | 648 | public function getPosition() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | 870 | public function getPreviousText() |
|
| 464 | |||
| 465 | /** |
||
| 466 | * @return int |
||
| 467 | */ |
||
| 468 | 240 | public function getColumn() |
|
| 472 | } |
||
| 473 |