Total Complexity | 46 |
Total Lines | 326 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TokenReader 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.
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 TokenReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class TokenReader implements TokenReaderInterface |
||
8 | { |
||
9 | protected const PATTERN_COMMENT = "/#[\u{0009}\u{0020}-\u{FFFF}]*/A"; |
||
10 | protected const PATTERN_NAME = '/[_A-Za-z][_0-9A-Za-z]*/A'; |
||
11 | protected const PATTERN_INT = '/((?!\.)(-?(0|[1-9][0-9]*)))+$/A'; |
||
12 | protected const PATTERN_FLOAT = '/-?(0|[1-9][0-9]*)(\.[0-9]+)?((E|e)(\+|-)?[0-9]+)?/A'; |
||
13 | protected const PATTERN_SPREAD = '/\.\.\./A'; |
||
14 | protected const PATTERN_STRING = "/\"([^\"\\\u{000A}\u{000D}]|(\\([\u{0020}-\u{FFFF}]|[\"\\/bfnrt]))))*\"/As"; |
||
15 | protected const PATTERN_BLOCK_STRING = '/"""("?"?(\\"""|\\(?!=""")|[^"\\]))*"""/As'; |
||
16 | protected const PUNCTUATION = '!$&:=@|()[]{}'; |
||
17 | |||
18 | /** |
||
19 | * The lexer owning this token reader. |
||
20 | * |
||
21 | * @var LexerInterface |
||
22 | */ |
||
23 | protected $lexer; |
||
24 | |||
25 | /** |
||
26 | * @inheritdoc |
||
27 | */ |
||
28 | public function setLexer(LexerInterface $lexer) |
||
29 | { |
||
30 | $this->lexer = $lexer; |
||
31 | return $this; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @inheritdoc |
||
36 | */ |
||
37 | public function getLexer(): LexerInterface |
||
38 | { |
||
39 | return $this->lexer; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @inheritdoc |
||
44 | * @throws SyntaxErrorException |
||
45 | */ |
||
46 | public function read(string $body, int $pos, int $line, int $col, Token $prev): ?Token |
||
47 | { |
||
48 | if (false !== \strpos(self::PUNCTUATION, $body[$pos])) { |
||
49 | return $this->createPunctuation($body, $pos, $line, $col, $prev); |
||
50 | } |
||
51 | |||
52 | if (\preg_match(static::PATTERN_COMMENT, $body, $match, null, $pos)) { |
||
53 | return $this->createComment($pos, $pos + \mb_strlen($match[0]), $line, $col, $prev, $match[0]); |
||
54 | } |
||
55 | |||
56 | if (\preg_match(static::PATTERN_NAME, $body, $match, null, $pos)) { |
||
57 | return $this->createName($pos, $pos + \mb_strlen($match[0]), $line, $col, $prev, $match[0]); |
||
58 | } |
||
59 | |||
60 | if (\preg_match(static::PATTERN_INT, $body, $match, null, $pos)) { |
||
61 | return $this->createInt($pos, $pos + \mb_strlen($match[0]), $line, $col, $prev, $match[0]); |
||
62 | } |
||
63 | |||
64 | if (\preg_match(static::PATTERN_FLOAT, $body, $match, null, $pos)) { |
||
65 | return $this->createFloat($pos, $pos + \mb_strlen($match[0]), $line, $col, $prev, $match[0]); |
||
66 | } |
||
67 | |||
68 | if (\preg_match(static::PATTERN_SPREAD, $body, $match, null, $pos)) { |
||
69 | return $this->createSpread($pos, $pos + 3, $line, $col, $prev); |
||
70 | } |
||
71 | |||
72 | if (\preg_match(static::PATTERN_STRING, $body, $match, null, $pos)) { |
||
73 | return $this->createString($pos, $pos + \mb_strlen($match[0]), $line, $col, $prev, $match[0]); |
||
74 | } |
||
75 | |||
76 | if (\preg_match(static::PATTERN_BLOCK_STRING, $body, $match, null, $pos)) { |
||
77 | return $this->createBlockString($pos, $pos + \mb_strlen($match[0]), $line, $col, $prev, $match[0]); |
||
78 | } |
||
79 | |||
80 | return null; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param string $kind |
||
85 | * @param int $start |
||
86 | * @param int $line |
||
87 | * @param int $col |
||
88 | * @param Token $prev |
||
89 | * @param null|string $value |
||
90 | * @return Token |
||
91 | */ |
||
92 | protected function createToken( |
||
93 | string $kind, |
||
94 | int $start, |
||
95 | int $end, |
||
96 | int $line, |
||
97 | int $col, |
||
98 | Token $prev, |
||
99 | ?string $value = null |
||
100 | ): Token { |
||
101 | return new Token($kind, $start, $end, $line, $col, $prev, $value); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $body |
||
106 | * @param int $pos |
||
107 | * @param int $line |
||
108 | * @param int $col |
||
109 | * @param Token $prev |
||
110 | * @return Token |
||
111 | */ |
||
112 | protected function createPunctuation(string $body, int $pos, int $line, int $col, Token $prev): Token |
||
113 | { |
||
114 | $code = \ord($body[$pos]); |
||
115 | |||
116 | switch ($code) { |
||
117 | case 33: // ! |
||
118 | return $this->createBang($pos, $line, $col, $prev); |
||
119 | case 36: // $ |
||
120 | return $this->createDollar($pos, $line, $col, $prev); |
||
121 | case 38: // & |
||
122 | return $this->createAmp($pos, $line, $col, $prev); |
||
123 | case 58: // : |
||
124 | return $this->createColon($pos, $line, $col, $prev); |
||
125 | case 61: // = |
||
126 | return $this->createEquals($pos, $line, $col, $prev); |
||
127 | case 64: // @ |
||
128 | return $this->createAt($pos, $line, $col, $prev); |
||
129 | case 124: // | |
||
130 | return $this->createPipe($pos, $line, $col, $prev); |
||
131 | case 40: |
||
132 | case 41: // ( or )~ |
||
133 | return $this->createParenthesis($code, $pos, $line, $col, $prev); |
||
134 | case 91: |
||
135 | case 93: // [ or ] |
||
136 | return $this->createBracket($code, $pos, $line, $col, $prev); |
||
137 | case 123: |
||
138 | case 125: // { or } |
||
139 | return $this->createBrace($code, $pos, $line, $col, $prev); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param int $pos |
||
145 | * @param int $line |
||
146 | * @param int $col |
||
147 | * @param Token $prev |
||
148 | * @return Token |
||
149 | */ |
||
150 | protected function createColon(int $pos, int $line, int $col, Token $prev): Token |
||
151 | { |
||
152 | return new Token(TokenKindEnum::COLON, $pos, $pos + 1, $line, $col, $prev); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param int $pos |
||
157 | * @param int $line |
||
158 | * @param int $col |
||
159 | * @param Token $prev |
||
160 | * @return Token |
||
161 | */ |
||
162 | protected function createAmp(int $pos, int $line, int $col, Token $prev): Token |
||
163 | { |
||
164 | return new Token(TokenKindEnum::AMP, $pos, $pos + 1, $line, $col, $prev); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param int $pos |
||
169 | * @param int $line |
||
170 | * @param int $col |
||
171 | * @param Token $prev |
||
172 | * @return Token |
||
173 | */ |
||
174 | protected function createBang(int $pos, int $line, int $col, Token $prev): Token |
||
175 | { |
||
176 | return new Token(TokenKindEnum::BANG, $pos, $pos + 1, $line, $col, $prev); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param int $code |
||
181 | * @param int $pos |
||
182 | * @param int $line |
||
183 | * @param int $col |
||
184 | * @param Token $prev |
||
185 | * @return Token |
||
186 | */ |
||
187 | protected function createBrace(int $code, int $pos, int $line, int $col, Token $prev): Token |
||
188 | { |
||
189 | return $code === 123 |
||
190 | ? new Token(TokenKindEnum::BRACE_L, $pos, $pos + 1, $line, $col, $prev) |
||
191 | : new Token(TokenKindEnum::BRACE_R, $pos, $pos + 1, $line, $col, $prev); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param int $code |
||
196 | * @param int $pos |
||
197 | * @param int $line |
||
198 | * @param int $col |
||
199 | * @param Token $prev |
||
200 | * @return Token |
||
201 | */ |
||
202 | protected function createBracket(int $code, int $pos, int $line, int $col, Token $prev): Token |
||
203 | { |
||
204 | return $code === 91 |
||
205 | ? new Token(TokenKindEnum::BRACKET_L, $pos, $pos + 1, $line, $col, $prev) |
||
206 | : new Token(TokenKindEnum::BRACKET_R, $pos, $pos + 1, $line, $col, $prev); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param int $pos |
||
211 | * @param int $line |
||
212 | * @param int $col |
||
213 | * @param Token $prev |
||
214 | * @return Token |
||
215 | */ |
||
216 | protected function createDollar(int $pos, int $line, int $col, Token $prev): Token |
||
217 | { |
||
218 | return new Token(TokenKindEnum::DOLLAR, $pos, $pos + 1, $line, $col, $prev); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param int $pos |
||
223 | * @param int $line |
||
224 | * @param int $col |
||
225 | * @param Token $prev |
||
226 | * @return Token |
||
227 | */ |
||
228 | protected function createPipe(int $pos, int $line, int $col, Token $prev): Token |
||
229 | { |
||
230 | return new Token(TokenKindEnum::PIPE, $pos, $pos + 1, $line, $col, $prev); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param int $code |
||
235 | * @param int $pos |
||
236 | * @param int $line |
||
237 | * @param int $col |
||
238 | * @param Token $prev |
||
239 | * @return Token |
||
240 | */ |
||
241 | protected function createParenthesis(int $code, int $pos, int $line, int $col, Token $prev): Token |
||
242 | { |
||
243 | return $code === 40 |
||
244 | ? new Token(TokenKindEnum::PAREN_L, $pos, $pos + 1, $line, $col, $prev) |
||
245 | : new Token(TokenKindEnum::PAREN_R, $pos, $pos + 1, $line, $col, $prev); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param int $pos |
||
250 | * @param int $line |
||
251 | * @param int $col |
||
252 | * @param Token $prev |
||
253 | * @return Token |
||
254 | */ |
||
255 | protected function createEquals(int $pos, int $line, int $col, Token $prev): Token |
||
256 | { |
||
257 | return new Token(TokenKindEnum::EQUALS, $pos, $pos + 1, $line, $col, $prev); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param int $pos |
||
262 | * @param int $line |
||
263 | * @param int $col |
||
264 | * @param Token $prev |
||
265 | * @return Token |
||
266 | */ |
||
267 | protected function createAt(int $pos, int $line, int $col, Token $prev): Token |
||
268 | { |
||
269 | return new Token(TokenKindEnum::AT, $pos, $pos + 1, $line, $col, $prev); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @param array ...$args |
||
274 | * @return Token |
||
275 | */ |
||
276 | protected function createComment(...$args): Token |
||
277 | { |
||
278 | return $this->createToken(TokenKindEnum::COMMENT, ...$args); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param array ...$args |
||
283 | * @return Token |
||
284 | */ |
||
285 | protected function createName(...$args): Token |
||
286 | { |
||
287 | return $this->createToken(TokenKindEnum::NAME, ...$args); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @param array ...$args |
||
292 | * @return Token |
||
293 | */ |
||
294 | protected function createInt(...$args): Token |
||
295 | { |
||
296 | return $this->createToken(TokenKindEnum::INT, ...$args); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param array ...$args |
||
301 | * @return Token |
||
302 | */ |
||
303 | protected function createFloat(...$args): Token |
||
304 | { |
||
305 | return $this->createToken(TokenKindEnum::FLOAT, ...$args); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param array ...$args |
||
310 | * @return Token |
||
311 | */ |
||
312 | protected function createSpread(...$args): Token |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @param array ...$args |
||
319 | * @return Token |
||
320 | */ |
||
321 | protected function createString(...$args): Token |
||
322 | { |
||
323 | return $this->createToken(TokenKindEnum::STRING, ...$args); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @param array ...$args |
||
328 | * @return Token |
||
329 | */ |
||
330 | protected function createBlockString(...$args): Token |
||
331 | { |
||
332 | return $this->createToken(TokenKindEnum::BLOCK_STRING, ...$args); |
||
333 | } |
||
334 | |||
335 | // /** |
||
336 | // * @param string $body |
||
603 |