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 | 2532 | public function __construct(string $line) |
|
82 | { |
||
83 | 2532 | $this->line = $line; |
|
84 | 2532 | $this->length = \mb_strlen($line, 'UTF-8') ?: 0; |
|
85 | 2532 | $this->isMultibyte = $this->length !== \strlen($line); |
|
86 | 2532 | $this->lineContainsTabs = false !== \strpos($line, "\t"); |
|
87 | 2532 | } |
|
88 | |||
89 | /** |
||
90 | * Returns the position of the next character which is not a space (or tab) |
||
91 | * |
||
92 | * @return int |
||
93 | */ |
||
94 | 2250 | public function getNextNonSpacePosition(): int |
|
95 | { |
||
96 | 2250 | if ($this->nextNonSpaceCache !== null) { |
|
97 | 2070 | return $this->nextNonSpaceCache; |
|
98 | } |
||
99 | |||
100 | 2250 | $i = $this->currentPosition; |
|
101 | 2250 | $cols = $this->column; |
|
102 | |||
103 | 2250 | while (($c = $this->getCharacter($i)) !== null) { |
|
104 | 2226 | if ($c === ' ') { |
|
105 | 510 | $i++; |
|
106 | 510 | $cols++; |
|
107 | 2184 | } elseif ($c === "\t") { |
|
108 | 36 | $i++; |
|
109 | 36 | $cols += (4 - ($cols % 4)); |
|
110 | } else { |
||
111 | 2184 | break; |
|
112 | } |
||
113 | } |
||
114 | |||
115 | 2250 | $nextNonSpace = ($c === null) ? $this->length : $i; |
|
116 | 2250 | $this->indent = $cols - $this->column; |
|
117 | |||
118 | 2250 | return $this->nextNonSpaceCache = $nextNonSpace; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Returns the next character which isn't a space (or tab) |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 2043 | public function getNextNonSpaceCharacter(): ?string |
|
127 | { |
||
128 | 2043 | return $this->getCharacter($this->getNextNonSpacePosition()); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Calculates the current indent (number of spaces after current position) |
||
133 | * |
||
134 | * @return int |
||
135 | */ |
||
136 | 2130 | public function getIndent(): int |
|
137 | { |
||
138 | 2130 | if ($this->nextNonSpaceCache === null) { |
|
139 | 2130 | $this->getNextNonSpacePosition(); |
|
140 | } |
||
141 | |||
142 | 2130 | return $this->indent; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Whether the cursor is indented to INDENT_LEVEL |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | 2070 | public function isIndented(): bool |
|
151 | { |
||
152 | 2070 | return $this->getIndent() >= self::INDENT_LEVEL; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param int|null $index |
||
157 | * |
||
158 | * @return string|null |
||
159 | */ |
||
160 | 2313 | public function getCharacter(?int $index = null): ?string |
|
161 | { |
||
162 | 2313 | if ($index === null) { |
|
163 | 2085 | $index = $this->currentPosition; |
|
164 | } |
||
165 | |||
166 | // Index out-of-bounds, or we're at the end |
||
167 | 2313 | if ($index < 0 || $index >= $this->length) { |
|
168 | 2154 | return null; |
|
169 | } |
||
170 | |||
171 | 2271 | if ($this->isMultibyte) { |
|
172 | 105 | if (isset($this->charCache[$index])) { |
|
173 | 48 | return $this->charCache[$index]; |
|
174 | } |
||
175 | |||
176 | 105 | return $this->charCache[$index] = \mb_substr($this->line, $index, 1, 'UTF-8'); |
|
177 | } |
||
178 | |||
179 | 2172 | return $this->line[$index]; |
|
180 | } |
||
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 | 1158 | public function peek(int $offset = 1): ?string |
|
193 | |||
194 | /** |
||
195 | * Whether the remainder is blank |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | 2088 | public function isBlank(): bool |
|
203 | |||
204 | /** |
||
205 | * Move the cursor forwards |
||
206 | */ |
||
207 | 39 | 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 | 2376 | public function advanceBy(int $characters, bool $advanceByColumns = false) |
|
279 | |||
280 | /** |
||
281 | * Advances the cursor by a single space or tab, if present |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | 348 | public function advanceBySpaceOrTab(): bool |
|
297 | |||
298 | /** |
||
299 | * Parse zero or more space/tab characters |
||
300 | * |
||
301 | * @return int Number of positions moved |
||
302 | */ |
||
303 | 1977 | public function advanceToNextNonSpaceOrTab(): int |
|
311 | |||
312 | /** |
||
313 | * Parse zero or more space characters, including at most one newline. |
||
314 | * |
||
315 | * Tab characters are not parsed with this function. |
||
316 | * |
||
317 | * @return int Number of positions moved |
||
318 | */ |
||
319 | 474 | public function advanceToNextNonSpaceOrNewline(): int |
|
341 | |||
342 | /** |
||
343 | * Move the position to the very end of the line |
||
344 | * |
||
345 | * @return int The number of characters moved |
||
346 | */ |
||
347 | 84 | public function advanceToEnd(): int |
|
356 | |||
357 | /** |
||
358 | * @return string |
||
359 | */ |
||
360 | 2154 | public function getRemainder(): string |
|
380 | |||
381 | /** |
||
382 | * @return string |
||
383 | */ |
||
384 | 2010 | public function getLine(): string |
|
388 | |||
389 | /** |
||
390 | * @return bool |
||
391 | */ |
||
392 | 444 | public function isAtEnd(): bool |
|
396 | |||
397 | /** |
||
398 | * Try to match a regular expression |
||
399 | * |
||
400 | * Returns the matching text and advances to the end of that match |
||
401 | * |
||
402 | * @param string $regex |
||
403 | * |
||
404 | * @return string|null |
||
405 | */ |
||
406 | 2064 | public function match(string $regex): ?string |
|
432 | |||
433 | /** |
||
434 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
435 | * |
||
436 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
437 | * passing it back into restoreState(), as the number of values and their |
||
438 | * contents may change in any future release without warning. |
||
439 | * |
||
440 | * @return array |
||
441 | */ |
||
442 | 1092 | public function saveState() |
|
453 | |||
454 | /** |
||
455 | * Restore the cursor to a previous state. |
||
456 | * |
||
457 | * Pass in the value previously obtained by calling saveState(). |
||
458 | * |
||
459 | * @param array $state |
||
460 | */ |
||
461 | 855 | public function restoreState($state) |
|
472 | |||
473 | /** |
||
474 | * @return int |
||
475 | */ |
||
476 | 717 | public function getPosition(): int |
|
480 | |||
481 | /** |
||
482 | * @return string |
||
483 | */ |
||
484 | 456 | public function getPreviousText(): string |
|
488 | |||
489 | /** |
||
490 | * @param int $start |
||
491 | * @param int|null $length |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | 405 | public function getSubstring(int $start, ?int $length = null): string |
|
505 | |||
506 | /** |
||
507 | * @return int |
||
508 | */ |
||
509 | 249 | public function getColumn(): int |
|
513 | } |
||
514 |