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 string |
||
65 | */ |
||
66 | private $encoding; |
||
67 | |||
68 | /** |
||
69 | * @var bool |
||
70 | */ |
||
71 | private $lineContainsTabs; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | private $isMultibyte; |
||
77 | |||
78 | /** |
||
79 | * @var array<int, string> |
||
80 | */ |
||
81 | private $charCache = []; |
||
82 | |||
83 | /** |
||
84 | * @param string $line The line being parsed (ASCII or UTF-8) |
||
85 | */ |
||
86 | 2526 | public function __construct(string $line) |
|
94 | |||
95 | /** |
||
96 | * Returns the position of the next character which is not a space (or tab) |
||
97 | * |
||
98 | * @return int |
||
99 | */ |
||
100 | 2244 | public function getNextNonSpacePosition(): int |
|
126 | |||
127 | /** |
||
128 | * Returns the next character which isn't a space (or tab) |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | 2037 | public function getNextNonSpaceCharacter(): ?string |
|
136 | |||
137 | /** |
||
138 | * Calculates the current indent (number of spaces after current position) |
||
139 | * |
||
140 | * @return int |
||
141 | */ |
||
142 | 2124 | public function getIndent(): int |
|
150 | |||
151 | /** |
||
152 | * Whether the cursor is indented to INDENT_LEVEL |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | 2064 | public function isIndented(): bool |
|
160 | |||
161 | /** |
||
162 | * @param int|null $index |
||
163 | * |
||
164 | * @return string|null |
||
165 | */ |
||
166 | 2298 | public function getCharacter(?int $index = null): ?string |
|
185 | |||
186 | /** |
||
187 | * Returns the next character (or null, if none) without advancing forwards |
||
188 | * |
||
189 | * @param int $offset |
||
190 | * |
||
191 | * @return string|null |
||
192 | */ |
||
193 | 1152 | public function peek(int $offset = 1): ?string |
|
197 | |||
198 | /** |
||
199 | * Whether the remainder is blank |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 2082 | public function isBlank(): bool |
|
207 | |||
208 | /** |
||
209 | * Move the cursor forwards |
||
210 | */ |
||
211 | 1011 | public function advance() |
|
215 | |||
216 | /** |
||
217 | * Move the cursor forwards |
||
218 | * |
||
219 | * @param int $characters Number of characters to advance by |
||
220 | * @param bool $advanceByColumns Whether to advance by columns instead of spaces |
||
221 | */ |
||
222 | 2370 | public function advanceBy(int $characters, bool $advanceByColumns = false) |
|
282 | |||
283 | /** |
||
284 | * Advances the cursor by a single space or tab, if present |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | 348 | public function advanceBySpaceOrTab(): bool |
|
300 | |||
301 | /** |
||
302 | * Parse zero or more space/tab characters |
||
303 | * |
||
304 | * @return int Number of positions moved |
||
305 | */ |
||
306 | 1971 | 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 | 471 | public function advanceToNextNonSpaceOrNewline(): int |
|
339 | |||
340 | /** |
||
341 | * Move the position to the very end of the line |
||
342 | * |
||
343 | * @return int The number of characters moved |
||
344 | */ |
||
345 | 84 | public function advanceToEnd(): int |
|
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | 2166 | public function getRemainder(): string |
|
378 | |||
379 | /** |
||
380 | * @return string |
||
381 | */ |
||
382 | 2004 | public function getLine(): string |
|
386 | |||
387 | /** |
||
388 | * @return bool |
||
389 | */ |
||
390 | 441 | public function isAtEnd(): bool |
|
394 | |||
395 | /** |
||
396 | * Try to match a regular expression |
||
397 | * |
||
398 | * Returns the matching text and advances to the end of that match |
||
399 | * |
||
400 | * @param string $regex |
||
401 | * |
||
402 | * @return string|null |
||
403 | */ |
||
404 | 2085 | public function match(string $regex): ?string |
|
430 | |||
431 | /** |
||
432 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
433 | * |
||
434 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
435 | * passing it back into restoreState(), as the number of values and their |
||
436 | * contents may change in any future release without warning. |
||
437 | * |
||
438 | * @return array |
||
439 | */ |
||
440 | 1089 | public function saveState() |
|
451 | |||
452 | /** |
||
453 | * Restore the cursor to a previous state. |
||
454 | * |
||
455 | * Pass in the value previously obtained by calling saveState(). |
||
456 | * |
||
457 | * @param array $state |
||
458 | */ |
||
459 | 852 | public function restoreState($state) |
|
470 | |||
471 | /** |
||
472 | * @return int |
||
473 | */ |
||
474 | 714 | public function getPosition(): int |
|
478 | |||
479 | /** |
||
480 | * @return string |
||
481 | */ |
||
482 | 999 | public function getPreviousText(): string |
|
486 | |||
487 | /** |
||
488 | * @param int $start |
||
489 | * @param int|null $length |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | 405 | public function getSubstring(int $start, ?int $length = null): string |
|
494 | { |
||
495 | 405 | if ($this->isMultibyte) { |
|
496 | 21 | return \mb_substr($this->line, $start, $length, $this->encoding); |
|
497 | 384 | } elseif ($length !== null) { |
|
498 | 381 | return \substr($this->line, $start, $length); |
|
499 | } |
||
500 | |||
501 | 3 | return \substr($this->line, $start); |
|
502 | } |
||
503 | |||
504 | /** |
||
505 | * @return int |
||
506 | */ |
||
507 | 249 | public function getColumn(): int |
|
511 | } |
||
512 |