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 | /** @var string */ |
||
21 | private $line; |
||
22 | |||
23 | /** @var int */ |
||
24 | private $length; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | * |
||
29 | * It's possible for this to be 1 char past the end, meaning we've parsed all chars and have |
||
30 | * reached the end. In this state, any character-returning method MUST return null. |
||
31 | */ |
||
32 | private $currentPosition = 0; |
||
33 | |||
34 | /** @var int */ |
||
35 | private $column = 0; |
||
36 | |||
37 | /** @var int */ |
||
38 | private $indent = 0; |
||
39 | |||
40 | /** @var int */ |
||
41 | private $previousPosition = 0; |
||
42 | |||
43 | /** @var int|null */ |
||
44 | private $nextNonSpaceCache; |
||
45 | |||
46 | /** @var bool */ |
||
47 | private $partiallyConsumedTab = false; |
||
48 | |||
49 | /** @var bool */ |
||
50 | private $lineContainsTabs; |
||
51 | |||
52 | /** @var bool */ |
||
53 | private $isMultibyte; |
||
54 | |||
55 | /** @var array<int, string> */ |
||
56 | private $charCache = []; |
||
57 | |||
58 | /** |
||
59 | * @param string $line The line being parsed (ASCII or UTF-8) |
||
60 | */ |
||
61 | 3000 | public function __construct(string $line) |
|
68 | |||
69 | /** |
||
70 | * Returns the position of the next character which is not a space (or tab) |
||
71 | */ |
||
72 | 2700 | public function getNextNonSpacePosition(): int |
|
98 | |||
99 | /** |
||
100 | * Returns the next character which isn't a space (or tab) |
||
101 | */ |
||
102 | 2466 | public function getNextNonSpaceCharacter(): ?string |
|
106 | |||
107 | /** |
||
108 | * Calculates the current indent (number of spaces after current position) |
||
109 | */ |
||
110 | 2580 | public function getIndent(): int |
|
118 | |||
119 | /** |
||
120 | * Whether the cursor is indented to INDENT_LEVEL |
||
121 | */ |
||
122 | 2520 | public function isIndented(): bool |
|
126 | |||
127 | 2760 | public function getCharacter(?int $index = null): ?string |
|
148 | |||
149 | /** |
||
150 | * Returns the next character (or null, if none) without advancing forwards |
||
151 | */ |
||
152 | 1332 | public function peek(int $offset = 1): ?string |
|
156 | |||
157 | /** |
||
158 | * Whether the remainder is blank |
||
159 | */ |
||
160 | 2514 | public function isBlank(): bool |
|
164 | |||
165 | /** |
||
166 | * Move the cursor forwards |
||
167 | */ |
||
168 | 513 | public function advance(): void |
|
172 | |||
173 | /** |
||
174 | * Move the cursor forwards |
||
175 | * |
||
176 | * @param int $characters Number of characters to advance by |
||
177 | * @param bool $advanceByColumns Whether to advance by columns instead of spaces |
||
178 | */ |
||
179 | 2868 | public function advanceBy(int $characters, bool $advanceByColumns = false): void |
|
243 | |||
244 | /** |
||
245 | * Advances the cursor by a single space or tab, if present |
||
246 | */ |
||
247 | 390 | public function advanceBySpaceOrTab(): bool |
|
259 | |||
260 | /** |
||
261 | * Parse zero or more space/tab characters |
||
262 | * |
||
263 | * @return int Number of positions moved |
||
264 | */ |
||
265 | 2532 | public function advanceToNextNonSpaceOrTab(): int |
|
273 | |||
274 | /** |
||
275 | * Parse zero or more space characters, including at most one newline. |
||
276 | * |
||
277 | * Tab characters are not parsed with this function. |
||
278 | * |
||
279 | * @return int Number of positions moved |
||
280 | */ |
||
281 | 249 | public function advanceToNextNonSpaceOrNewline(): int |
|
303 | |||
304 | /** |
||
305 | * Move the position to the very end of the line |
||
306 | * |
||
307 | * @return int The number of characters moved |
||
308 | */ |
||
309 | 798 | public function advanceToEnd(): int |
|
318 | |||
319 | 2634 | public function getRemainder(): string |
|
339 | |||
340 | 1965 | public function getLine(): string |
|
344 | |||
345 | 2154 | public function isAtEnd(): bool |
|
349 | |||
350 | /** |
||
351 | * Try to match a regular expression |
||
352 | * |
||
353 | * Returns the matching text and advances to the end of that match |
||
354 | */ |
||
355 | 2352 | public function match(string $regex): ?string |
|
381 | |||
382 | /** |
||
383 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
384 | * |
||
385 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
386 | * passing it back into restoreState(), as the number of values and their |
||
387 | * contents may change in any future release without warning. |
||
388 | */ |
||
389 | 1689 | public function saveState(): CursorState |
|
400 | |||
401 | /** |
||
402 | * Restore the cursor to a previous state. |
||
403 | * |
||
404 | * Pass in the value previously obtained by calling saveState(). |
||
405 | */ |
||
406 | 1596 | public function restoreState(CursorState $state): void |
|
417 | |||
418 | 735 | public function getPosition(): int |
|
422 | |||
423 | 399 | public function getPreviousText(): string |
|
427 | |||
428 | 426 | public function getSubstring(int $start, ?int $length = null): string |
|
440 | |||
441 | 285 | public function getColumn(): int |
|
445 | } |
||
446 |