Complex classes like Lexer 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 Lexer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Lexer |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Whitespace tokens. |
||
| 23 | */ |
||
| 24 | private const SPACE_TOKENS = [ |
||
| 25 | Token::T_SPACE, |
||
| 26 | Token::T_COMMENT, |
||
| 27 | Token::T_NEWLINE, |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Tag map. |
||
| 32 | */ |
||
| 33 | private const TAG_MAP = [ |
||
| 34 | ':' => Token::T_COLON, |
||
| 35 | ';' => Token::T_SEMICOLON, |
||
| 36 | '$' => Token::T_DOLLAR, |
||
| 37 | '%%' => Token::T_MARK, |
||
| 38 | '%{' => Token::T_BEGIN_INC, |
||
| 39 | '%}' => Token::T_END_INC, |
||
| 40 | '%token' => Token::T_TOKEN, |
||
| 41 | '%term' => Token::T_TOKEN, |
||
| 42 | '%left' => Token::T_LEFT, |
||
| 43 | '%right' => Token::T_RIGHT, |
||
| 44 | '%nonassoc' => Token::T_NON_ASSOC, |
||
| 45 | '%prec' => Token::T_PRECTOK, |
||
| 46 | '%type' => Token::T_TYPE, |
||
| 47 | '%union' => Token::T_UNION, |
||
| 48 | '%start' => Token::T_START, |
||
| 49 | '%expect' => Token::T_EXPECT, |
||
| 50 | '%pure_parser' => Token::T_PURE_PARSER, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $buffer; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $fileName; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | protected $line; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $offset; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var Token |
||
| 75 | */ |
||
| 76 | protected $currentToken; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Token |
||
| 80 | */ |
||
| 81 | protected $backToken; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $backChar; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $prevIsDollar; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $char; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $value; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $code |
||
| 105 | * @param string $filename |
||
| 106 | */ |
||
| 107 | public function startLexing(string $code, string $filename = '') |
||
| 108 | { |
||
| 109 | $this->buffer = $code; |
||
| 110 | $this->fileName = $filename; |
||
| 111 | |||
| 112 | $this->reset(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | protected function reset() |
||
| 119 | { |
||
| 120 | $this->line = 1; |
||
| 121 | $this->offset = 0; |
||
| 122 | $this->backChar = null; |
||
| 123 | $this->backToken = null; |
||
| 124 | $this->prevIsDollar = false; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @throws LexingException |
||
| 129 | * @throws ParseException |
||
| 130 | * |
||
| 131 | * @return Token |
||
| 132 | */ |
||
| 133 | public function getToken(): Token |
||
| 134 | { |
||
| 135 | $this->currentToken = $this->getRawToken(); |
||
| 136 | |||
| 137 | while (\in_array($this->currentToken->getType(), self::SPACE_TOKENS)) { |
||
| 138 | $this->currentToken = $this->getRawToken(); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $this->currentToken; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @throws LexingException |
||
| 146 | */ |
||
| 147 | public function ungetToken() |
||
| 148 | { |
||
| 149 | if ($this->backToken !== null) { |
||
| 150 | throw new LexingException('Too many ungetToken calls'); |
||
| 151 | } |
||
| 152 | |||
| 153 | $this->backToken = $this->currentToken; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @throws LexingException |
||
| 158 | * @throws ParseException |
||
| 159 | * |
||
| 160 | * @return Token |
||
| 161 | */ |
||
| 162 | public function peek(): Token |
||
| 163 | { |
||
| 164 | $result = $this->getToken(); |
||
| 165 | $this->ungetToken(); |
||
| 166 | |||
| 167 | return $result; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @throws LexingException |
||
| 172 | * @throws ParseException |
||
| 173 | * |
||
| 174 | * @return Token |
||
| 175 | */ |
||
| 176 | public function getRawToken() |
||
| 177 | { |
||
| 178 | if ($this->backToken !== null) { |
||
| 179 | $this->currentToken = $this->backToken; |
||
| 180 | $this->backToken = null; |
||
| 181 | |||
| 182 | return $this->currentToken; |
||
| 183 | } |
||
| 184 | |||
| 185 | $this->char = $this->getChar(); |
||
| 186 | $this->value = ''; |
||
| 187 | |||
| 188 | switch (true) { |
||
| 189 | case $this->isWhitespace(): |
||
| 190 | return $this->token(Token::T_SPACE, $this->value); |
||
| 191 | case $this->isNewline(): |
||
| 192 | return $this->token(Token::T_NEWLINE, $this->value); |
||
| 193 | case $this->isComment(): |
||
| 194 | return $this->token(Token::T_COMMENT, $this->value); |
||
| 195 | case $this->isEof(): |
||
| 196 | return $this->token(Token::T_EOF, $this->value); |
||
| 197 | } |
||
| 198 | |||
| 199 | $tag = $this->detectToken(); |
||
| 200 | |||
| 201 | switch (true) { |
||
| 202 | case isset(self::TAG_MAP[$this->value]): |
||
| 203 | return $this->token(self::TAG_MAP[$this->value], $this->value); |
||
| 204 | default: |
||
| 205 | return $this->token($tag, $this->value); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @throws LexingException |
||
| 211 | * |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | protected function isWhitespace(): bool |
||
| 215 | { |
||
| 216 | if (Utils::isWhite($this->char)) { |
||
| 217 | while (Utils::isWhite($this->char)) { |
||
| 218 | $this->value .= $this->char; |
||
| 219 | $this->char = $this->getChar(); |
||
| 220 | } |
||
| 221 | $this->ungetChar($this->char); |
||
| 222 | |||
| 223 | return true; |
||
| 224 | } |
||
| 225 | |||
| 226 | return false; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | protected function isNewline(): bool |
||
| 233 | { |
||
| 234 | if ($this->char === "\n") { |
||
| 235 | $this->value = $this->char; |
||
| 236 | |||
| 237 | return true; |
||
| 238 | } |
||
| 239 | |||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @throws LexingException |
||
| 245 | * @throws ParseException |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | protected function isComment(): bool |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | protected function isEof(): bool |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @throws LexingException |
||
| 309 | * @throws ParseException |
||
| 310 | * |
||
| 311 | * @return int |
||
| 312 | */ |
||
| 313 | protected function detectToken() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | protected function getChar(): string |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $char |
||
| 421 | * |
||
| 422 | * @throws LexingException |
||
| 423 | */ |
||
| 424 | protected function ungetChar(string $char) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param int $type |
||
| 439 | * @param string $value |
||
| 440 | * |
||
| 441 | * @return Token |
||
| 442 | */ |
||
| 443 | protected function token(int $type, string $value): Token |
||
| 447 | } |
||
| 448 |
When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.
Let’s take a look at these examples: