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 | 2997 | 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 | 2697 | public function getNextNonSpacePosition(): int |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Returns the next character which isn't a space (or tab) |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 2463 | public function getNextNonSpaceCharacter(): ?string |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Calculates the current indent (number of spaces after current position) |
||
| 133 | * |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | 2577 | public function getIndent(): int |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Whether the cursor is indented to INDENT_LEVEL |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | 2517 | public function isIndented(): bool |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param int|null $index |
||
| 157 | * |
||
| 158 | * @return string|null |
||
| 159 | */ |
||
| 160 | 2757 | 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 | 1329 | public function peek(int $offset = 1): ?string |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Whether the remainder is blank |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | 2511 | public function isBlank(): bool |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Move the cursor forwards |
||
| 206 | */ |
||
| 207 | 510 | 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 | 2865 | public function advanceBy(int $characters, bool $advanceByColumns = false): void |
|
| 221 | { |
||
| 222 | 2865 | if ($characters === 0) { |
|
| 223 | 2574 | $this->previousPosition = $this->currentPosition; |
|
| 224 | |||
| 225 | 2574 | return; |
|
| 226 | } |
||
| 227 | |||
| 228 | 2694 | $this->previousPosition = $this->currentPosition; |
|
| 229 | 2694 | $this->nextNonSpaceCache = null; |
|
| 230 | |||
| 231 | // Optimization to avoid tab handling logic if we have no tabs |
||
| 232 | 2694 | if (!$this->lineContainsTabs || false === \strpos( |
|
| 233 | 51 | $nextFewChars = $this->isMultibyte ? |
|
| 234 | 6 | \mb_substr($this->line, $this->currentPosition, $characters, 'UTF-8') : |
|
| 235 | 51 | \substr($this->line, $this->currentPosition, $characters), |
|
| 236 | 2694 | "\t" |
|
| 237 | )) { |
||
| 238 | 2682 | $length = \min($characters, $this->length - $this->currentPosition); |
|
| 239 | 2682 | $this->partiallyConsumedTab = false; |
|
| 240 | 2682 | $this->currentPosition += $length; |
|
| 241 | 2682 | $this->column += $length; |
|
| 242 | |||
| 243 | 2682 | return; |
|
| 244 | } |
||
| 245 | |||
| 246 | 45 | if ($characters === 1 && !empty($nextFewChars)) { |
|
| 247 | 18 | $asArray = [$nextFewChars]; |
|
| 248 | 39 | } elseif ($this->isMultibyte) { |
|
| 249 | /** @var string[] $asArray */ |
||
| 250 | $asArray = \preg_split('//u', $nextFewChars, -1, \PREG_SPLIT_NO_EMPTY); |
||
| 251 | } else { |
||
| 252 | 39 | $asArray = \str_split($nextFewChars); |
|
| 253 | } |
||
| 254 | |||
| 255 | 45 | foreach ($asArray as $relPos => $c) { |
|
| 256 | 45 | if ($c === "\t") { |
|
| 257 | 45 | $charsToTab = 4 - ($this->column % 4); |
|
| 258 | 45 | if ($advanceByColumns) { |
|
| 259 | 33 | $this->partiallyConsumedTab = $charsToTab > $characters; |
|
| 260 | 33 | $charsToAdvance = $charsToTab > $characters ? $characters : $charsToTab; |
|
| 261 | 33 | $this->column += $charsToAdvance; |
|
| 262 | 33 | $this->currentPosition += $this->partiallyConsumedTab ? 0 : 1; |
|
| 263 | 33 | $characters -= $charsToAdvance; |
|
| 264 | } else { |
||
| 265 | 18 | $this->partiallyConsumedTab = false; |
|
| 266 | 18 | $this->column += $charsToTab; |
|
| 267 | 18 | $this->currentPosition++; |
|
| 268 | 45 | $characters--; |
|
| 269 | } |
||
| 270 | } else { |
||
| 271 | 12 | $this->partiallyConsumedTab = false; |
|
| 272 | 12 | $this->currentPosition++; |
|
| 273 | 12 | $this->column++; |
|
| 274 | 12 | $characters--; |
|
| 275 | } |
||
| 276 | |||
| 277 | 45 | if ($characters <= 0) { |
|
| 278 | 45 | break; |
|
| 279 | } |
||
| 280 | } |
||
| 281 | 45 | } |
|
| 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 | 2529 | 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 | 249 | 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 | 795 | public function advanceToEnd(): int |
|
| 359 | |||
| 360 | 2631 | public function getRemainder(): string |
|
| 380 | |||
| 381 | 1962 | public function getLine(): string |
|
| 385 | |||
| 386 | 2151 | 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 | 2370 | 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 | 1686 | public function saveState(): CursorState |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Restore the cursor to a previous state. |
||
| 448 | * |
||
| 449 | * Pass in the value previously obtained by calling saveState(). |
||
| 450 | * |
||
| 451 | * @param CursorState $state |
||
| 452 | */ |
||
| 453 | 1593 | public function restoreState(CursorState $state): void |
|
| 464 | |||
| 465 | 732 | public function getPosition(): int |
|
| 469 | |||
| 470 | 396 | public function getPreviousText(): string |
|
| 474 | |||
| 475 | 423 | public function getSubstring(int $start, ?int $length = null): string |
|
| 485 | |||
| 486 | 285 | public function getColumn(): int |
|
| 490 | } |
||
| 491 |