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 | * @psalm-readonly |
||
24 | */ |
||
25 | private $line; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | * |
||
30 | * @psalm-readonly |
||
31 | */ |
||
32 | private $length; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | * |
||
37 | * It's possible for this to be 1 char past the end, meaning we've parsed all chars and have |
||
38 | * reached the end. In this state, any character-returning method MUST return null. |
||
39 | */ |
||
40 | private $currentPosition = 0; |
||
41 | |||
42 | /** @var int */ |
||
43 | private $column = 0; |
||
44 | |||
45 | /** @var int */ |
||
46 | private $indent = 0; |
||
47 | |||
48 | /** @var int */ |
||
49 | private $previousPosition = 0; |
||
50 | |||
51 | /** @var int|null */ |
||
52 | private $nextNonSpaceCache; |
||
53 | |||
54 | /** @var bool */ |
||
55 | private $partiallyConsumedTab = false; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | * |
||
60 | * @psalm-readonly |
||
61 | */ |
||
62 | private $lineContainsTabs; |
||
63 | |||
64 | /** |
||
65 | * @var bool |
||
66 | * |
||
67 | * @psalm-readonly |
||
68 | */ |
||
69 | private $isMultibyte; |
||
70 | |||
71 | /** @var array<int, string> */ |
||
72 | private $charCache = []; |
||
73 | |||
74 | /** |
||
75 | * @param string $line The line being parsed (ASCII or UTF-8) |
||
76 | */ |
||
77 | 3000 | public function __construct(string $line) |
|
84 | |||
85 | /** |
||
86 | * Returns the position of the next character which is not a space (or tab) |
||
87 | */ |
||
88 | 2700 | public function getNextNonSpacePosition(): int |
|
115 | |||
116 | /** |
||
117 | * Returns the next character which isn't a space (or tab) |
||
118 | */ |
||
119 | 2466 | public function getNextNonSpaceCharacter(): ?string |
|
123 | |||
124 | /** |
||
125 | * Calculates the current indent (number of spaces after current position) |
||
126 | */ |
||
127 | 2580 | public function getIndent(): int |
|
135 | |||
136 | /** |
||
137 | * Whether the cursor is indented to INDENT_LEVEL |
||
138 | */ |
||
139 | 2520 | public function isIndented(): bool |
|
143 | |||
144 | 2760 | public function getCharacter(?int $index = null): ?string |
|
165 | |||
166 | /** |
||
167 | * Returns the next character (or null, if none) without advancing forwards |
||
168 | */ |
||
169 | 1332 | public function peek(int $offset = 1): ?string |
|
173 | |||
174 | /** |
||
175 | * Whether the remainder is blank |
||
176 | */ |
||
177 | 2514 | public function isBlank(): bool |
|
181 | |||
182 | /** |
||
183 | * Move the cursor forwards |
||
184 | */ |
||
185 | 513 | public function advance(): void |
|
189 | |||
190 | /** |
||
191 | * Move the cursor forwards |
||
192 | * |
||
193 | * @param int $characters Number of characters to advance by |
||
194 | * @param bool $advanceByColumns Whether to advance by columns instead of spaces |
||
195 | */ |
||
196 | 2868 | public function advanceBy(int $characters, bool $advanceByColumns = false): void |
|
261 | |||
262 | 2667 | private function advanceWithoutTabCharacters(int $characters): void |
|
271 | |||
272 | /** |
||
273 | * Advances the cursor by a single space or tab, if present |
||
274 | */ |
||
275 | 390 | public function advanceBySpaceOrTab(): bool |
|
287 | |||
288 | /** |
||
289 | * Parse zero or more space/tab characters |
||
290 | * |
||
291 | * @return int Number of positions moved |
||
292 | */ |
||
293 | 2532 | public function advanceToNextNonSpaceOrTab(): int |
|
301 | |||
302 | /** |
||
303 | * Parse zero or more space characters, including at most one newline. |
||
304 | * |
||
305 | * Tab characters are not parsed with this function. |
||
306 | * |
||
307 | * @return int Number of positions moved |
||
308 | */ |
||
309 | 249 | public function advanceToNextNonSpaceOrNewline(): int |
|
331 | |||
332 | /** |
||
333 | * Move the position to the very end of the line |
||
334 | * |
||
335 | * @return int The number of characters moved |
||
336 | */ |
||
337 | 798 | public function advanceToEnd(): int |
|
346 | |||
347 | 2634 | public function getRemainder(): string |
|
367 | |||
368 | 1965 | public function getLine(): string |
|
372 | |||
373 | 2154 | public function isAtEnd(): bool |
|
377 | |||
378 | /** |
||
379 | * Try to match a regular expression |
||
380 | * |
||
381 | * Returns the matching text and advances to the end of that match |
||
382 | */ |
||
383 | 2352 | public function match(string $regex): ?string |
|
409 | |||
410 | /** |
||
411 | * Encapsulates the current state of this cursor in case you need to rollback later. |
||
412 | * |
||
413 | * WARNING: Do not parse or use the return value for ANYTHING except for |
||
414 | * passing it back into restoreState(), as the number of values and their |
||
415 | * contents may change in any future release without warning. |
||
416 | */ |
||
417 | 1689 | public function saveState(): CursorState |
|
428 | |||
429 | /** |
||
430 | * Restore the cursor to a previous state. |
||
431 | * |
||
432 | * Pass in the value previously obtained by calling saveState(). |
||
433 | */ |
||
434 | 1596 | public function restoreState(CursorState $state): void |
|
445 | |||
446 | 735 | public function getPosition(): int |
|
450 | |||
451 | 399 | public function getPreviousText(): string |
|
455 | |||
456 | 426 | public function getSubstring(int $start, ?int $length = null): string |
|
468 | |||
469 | 285 | public function getColumn(): int |
|
473 | } |
||
474 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.