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 int |
||
| 78 | */ |
||
| 79 | private $charCache = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $line |
||
| 83 | */ |
||
| 84 | 2391 | public function __construct($line, $encoding = 'UTF-8') |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the position of the next character which is not a space (or tab) |
||
| 95 | * |
||
| 96 | * @return int |
||
| 97 | */ |
||
| 98 | 2118 | public function getNextNonSpacePosition() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Returns the next character which isn't a space (or tab) |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | 1911 | public function getNextNonSpaceCharacter() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Calculates the current indent (number of spaces after current position) |
||
| 137 | * |
||
| 138 | * @return int |
||
| 139 | */ |
||
| 140 | 1998 | public function getIndent() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Whether the cursor is indented to INDENT_LEVEL |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | 1938 | public function isIndented() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param int|null $index |
||
| 161 | * |
||
| 162 | * @return string|null |
||
| 163 | */ |
||
| 164 | 2172 | public function getCharacter($index = null) |
|
| 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 | 1020 | public function peek($offset = 1) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Whether the remainder is blank |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | 1956 | public function isBlank() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Move the cursor forwards |
||
| 206 | */ |
||
| 207 | 795 | public function advance() |
|
| 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 | 2247 | public function advanceBy($characters, $advanceByColumns = false) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Advances the cursor by a single space or tab, if present |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | 339 | public function advanceBySpaceOrTab() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Parse zero or more space/tab characters |
||
| 293 | * |
||
| 294 | * @return int Number of positions moved |
||
| 295 | */ |
||
| 296 | 1848 | public function advanceToNextNonSpaceOrTab() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Parse zero or more space characters, including at most one newline. |
||
| 307 | * |
||
| 308 | * Tab characters are not parsed with this function. |
||
| 309 | * |
||
| 310 | * @return int Number of positions moved |
||
| 311 | */ |
||
| 312 | 441 | public function advanceToNextNonSpaceOrNewline() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Move the position to the very end of the line |
||
| 332 | * |
||
| 333 | * @return int The number of characters moved |
||
| 334 | */ |
||
| 335 | 84 | public function advanceToEnd() |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | 2043 | public function getRemainder() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 1887 | public function getLine() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 411 | public function isAtEnd() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Try to match a regular expression |
||
| 383 | * |
||
| 384 | * Returns the matching text and advances to the end of that match |
||
| 385 | * |
||
| 386 | * @param string $regex |
||
| 387 | * |
||
| 388 | * @return string|null |
||
| 389 | */ |
||
| 390 | 1908 | public function match($regex) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
| 419 | * |
||
| 420 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
| 421 | * passing it back into restoreState(), as the number of values and their |
||
| 422 | * contents may change in any future release without warning. |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | 1011 | public function saveState() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Restore the cursor to a previous state. |
||
| 440 | * |
||
| 441 | * Pass in the value previously obtained by calling saveState(). |
||
| 442 | * |
||
| 443 | * @param array $state |
||
| 444 | */ |
||
| 445 | 795 | public function restoreState($state) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @return int |
||
| 459 | */ |
||
| 460 | 648 | public function getPosition() |
|
| 464 | |||
| 465 | /** |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | 870 | public function getPreviousText() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * @return int |
||
| 475 | */ |
||
| 476 | 240 | public function getColumn() |
|
| 480 | } |
||
| 481 |