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 |
||
| 14 | class Cursor |
||
| 15 | { |
||
| 16 | const INDENT_LEVEL = 4; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $line; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var int |
||
| 25 | */ |
||
| 26 | private $length; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int |
||
| 30 | * |
||
| 31 | * It's possible for this to be 1 char past the end, meaning we've parsed all chars and have |
||
| 32 | * reached the end. In this state, any character-returning method MUST return null. |
||
| 33 | */ |
||
| 34 | private $currentPosition = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $column = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | private $indent = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | private $previousPosition = 0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int|null |
||
| 53 | */ |
||
| 54 | private $firstNonSpaceCache; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $line |
||
| 58 | */ |
||
| 59 | 2292 | public function __construct($line) |
|
| 60 | { |
||
| 61 | 2292 | $this->line = $line; |
|
| 62 | 2292 | $this->length = mb_strlen($line, 'utf-8'); |
|
| 63 | 2292 | } |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Returns the position of the next non-space character |
||
| 67 | * |
||
| 68 | * @return int |
||
| 69 | */ |
||
| 70 | 1980 | public function getFirstNonSpacePosition() |
|
| 71 | { |
||
| 72 | 1980 | if ($this->firstNonSpaceCache !== null) { |
|
| 73 | 1854 | return $this->firstNonSpaceCache; |
|
| 74 | } |
||
| 75 | |||
| 76 | 1980 | $i = $this->currentPosition; |
|
| 77 | 1980 | $cols = $this->column; |
|
| 78 | |||
| 79 | 1980 | while (($c = $this->getCharacter($i)) !== null) { |
|
| 80 | 1965 | if ($c === ' ') { |
|
| 81 | 474 | $i++; |
|
| 82 | 474 | $cols++; |
|
| 83 | 1965 | } elseif ($c === "\t") { |
|
| 84 | 18 | $i++; |
|
| 85 | 18 | $cols += (4 - ($cols % 4)); |
|
| 86 | 18 | } else { |
|
| 87 | 1932 | break; |
|
| 88 | } |
||
| 89 | 480 | } |
|
| 90 | |||
| 91 | 1980 | $nextNonSpace = ($c === null) ? $this->length : $i; |
|
| 92 | 1980 | $this->indent = $cols - $this->column; |
|
| 93 | |||
| 94 | 1980 | return $this->firstNonSpaceCache = $nextNonSpace; |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns the next character which isn't a space |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | 1827 | public function getFirstNonSpaceCharacter() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Calculates the current indent (number of spaces after current position) |
||
| 109 | * |
||
| 110 | * @return int |
||
| 111 | */ |
||
| 112 | 1914 | public function getIndent() |
|
| 113 | { |
||
| 114 | 1914 | $this->getFirstNonSpacePosition(); |
|
| 115 | |||
| 116 | 1914 | return $this->indent; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Whether the cursor is indented to INDENT_LEVEL |
||
| 121 | * |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | 1854 | public function isIndented() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param int|null $index |
||
| 131 | * |
||
| 132 | * @return string|null |
||
| 133 | */ |
||
| 134 | 2073 | public function getCharacter($index = null) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Returns the next character (or null, if none) without advancing forwards |
||
| 150 | * |
||
| 151 | * @param int $offset |
||
| 152 | * |
||
| 153 | * @return string|null |
||
| 154 | */ |
||
| 155 | 978 | public function peek($offset = 1) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Whether the remainder is blank |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 1872 | public function isBlank() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Move the cursor forwards |
||
| 172 | */ |
||
| 173 | 756 | public function advance() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Move the cursor forwards |
||
| 180 | * |
||
| 181 | * @param int $characters Number of characters to advance by |
||
| 182 | */ |
||
| 183 | 2115 | public function advanceBy($characters, $advanceByColumns = false) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Advances the cursor while the given character is matched |
||
| 225 | * |
||
| 226 | * @param string $character Character to match |
||
| 227 | * @param int|null $maximumCharactersToAdvance Maximum number of characters to advance before giving up |
||
| 228 | * |
||
| 229 | * @return int Number of positions moved (0 if unsuccessful) |
||
| 230 | */ |
||
| 231 | 144 | public function advanceWhileMatches($character, $maximumCharactersToAdvance = null) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Parse zero or more space characters, including at most one newline |
||
| 257 | * |
||
| 258 | * @return int Number of positions moved |
||
| 259 | */ |
||
| 260 | 1863 | public function advanceToFirstNonSpace() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | 1956 | public function getRemainder() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | 1812 | public function getLine() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | 1977 | public function isAtEnd() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Try to match a regular expression |
||
| 308 | * |
||
| 309 | * Returns the matching text and advances to the end of that match |
||
| 310 | * |
||
| 311 | * @param string $regex |
||
| 312 | * |
||
| 313 | * @return string|null |
||
| 314 | */ |
||
| 315 | 1827 | public function match($regex) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @return CursorState |
||
| 336 | */ |
||
| 337 | 1770 | public function saveState() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param CursorState $state |
||
| 352 | */ |
||
| 353 | 1689 | public function restoreState(CursorState $state) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @return int |
||
| 366 | */ |
||
| 367 | 588 | public function getPosition() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | 810 | public function getPreviousText() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @return int |
||
| 382 | */ |
||
| 383 | 234 | public function getColumn() |
|
| 387 | } |
||
| 388 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.