Total Complexity | 49 |
Total Lines | 452 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BaseTokenizer 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 BaseTokenizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class BaseTokenizer |
||
23 | { |
||
24 | /** |
||
25 | * @var string SQL code. |
||
26 | */ |
||
27 | private string $sql; |
||
28 | |||
29 | /** |
||
30 | * @var int SQL code string length. |
||
31 | */ |
||
32 | protected int $length; |
||
33 | |||
34 | /** |
||
35 | * @var int SQL code string current offset. |
||
36 | */ |
||
37 | protected int $offset; |
||
38 | |||
39 | /** |
||
40 | * @var \SplStack stack of active tokens. |
||
41 | */ |
||
42 | private $tokenStack; |
||
43 | |||
44 | /** |
||
45 | * @var SqlToken|null active token. It's usually a top of the token stack. |
||
46 | */ |
||
47 | private ?SqlToken $currentToken = null; |
||
48 | |||
49 | /** |
||
50 | * @var string[] cached substrings. |
||
51 | */ |
||
52 | private array $substrings; |
||
53 | |||
54 | /** |
||
55 | * @var string string current buffer value. |
||
56 | */ |
||
57 | private string $buffer = ''; |
||
58 | |||
59 | /** |
||
60 | * @var SqlToken resulting token of a last {@see tokenize()} call. |
||
61 | */ |
||
62 | private ?SqlToken $token = null; |
||
63 | |||
64 | public function __construct(string $sql) |
||
65 | { |
||
66 | $this->sql = $sql; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Tokenizes and returns a code type token. |
||
71 | * |
||
72 | * @return SqlToken code type token. |
||
73 | */ |
||
74 | public function tokenize(): SqlToken |
||
75 | { |
||
76 | $this->length = mb_strlen($this->sql, 'UTF-8'); |
||
77 | $this->offset = 0; |
||
78 | $this->substrings = []; |
||
79 | $this->buffer = ''; |
||
80 | |||
81 | $this->token = new SqlToken(); |
||
82 | $this->token->setType(SqlToken::TYPE_CODE); |
||
83 | $this->token->setContent($this->sql); |
||
84 | |||
85 | $this->tokenStack = new \SplStack(); |
||
86 | $this->tokenStack->push($this->token); |
||
87 | |||
88 | $tk = new SqlToken(); |
||
89 | $tk->setType(SqlToken::TYPE_STATEMENT); |
||
90 | $this->token[] = $tk; |
||
91 | |||
92 | $this->tokenStack->push($this->token[0]); |
||
93 | $this->currentToken = $this->tokenStack->top(); |
||
94 | |||
95 | while (!$this->isEof()) { |
||
96 | if ($this->isWhitespace($length) || $this->isComment($length)) { |
||
97 | $this->addTokenFromBuffer(); |
||
98 | $this->advance($length); |
||
99 | |||
100 | continue; |
||
101 | } |
||
102 | |||
103 | if ($this->tokenizeOperator($length) || $this->tokenizeDelimitedString($length)) { |
||
104 | $this->advance($length); |
||
105 | |||
106 | continue; |
||
107 | } |
||
108 | |||
109 | $this->buffer .= $this->substring(1); |
||
110 | $this->advance(1); |
||
111 | } |
||
112 | $this->addTokenFromBuffer(); |
||
113 | if ($this->token->getHasChildren() && !$this->token[-1]->getHasChildren()) { |
||
|
|||
114 | unset($this->token[-1]); |
||
115 | } |
||
116 | |||
117 | return $this->token; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Returns whether there's a whitespace at the current offset. |
||
122 | * |
||
123 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
124 | * |
||
125 | * @param int $length length of the matched string. |
||
126 | * |
||
127 | * @return bool whether there's a whitespace at the current offset. |
||
128 | */ |
||
129 | abstract protected function isWhitespace(int &$length): bool; |
||
130 | |||
131 | /** |
||
132 | * Returns whether there's a commentary at the current offset. |
||
133 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
134 | * |
||
135 | * @param int $length length of the matched string. |
||
136 | * |
||
137 | * @return bool whether there's a commentary at the current offset. |
||
138 | */ |
||
139 | abstract protected function isComment(int &$length): bool; |
||
140 | |||
141 | /** |
||
142 | * Returns whether there's an operator at the current offset. |
||
143 | * |
||
144 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
145 | * It may also set `$content` to a string that will be used as a token content. |
||
146 | * |
||
147 | * @param int $length length of the matched string. |
||
148 | * @param string|null $content optional content instead of the matched string. |
||
149 | * |
||
150 | * @return bool whether there's an operator at the current offset. |
||
151 | */ |
||
152 | abstract protected function isOperator(int &$length, ?string &$content): bool; |
||
153 | |||
154 | /** |
||
155 | * Returns whether there's an identifier at the current offset. |
||
156 | * |
||
157 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
158 | * It may also set `$content` to a string that will be used as a token content. |
||
159 | * |
||
160 | * @param int $length length of the matched string. |
||
161 | * @param string|null $content optional content instead of the matched string. |
||
162 | * |
||
163 | * @return bool whether there's an identifier at the current offset. |
||
164 | */ |
||
165 | abstract protected function isIdentifier(int &$length, ?string &$content): bool; |
||
166 | |||
167 | /** |
||
168 | * Returns whether there's a string literal at the current offset. |
||
169 | * |
||
170 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
171 | * It may also set `$content` to a string that will be used as a token content. |
||
172 | * |
||
173 | * @param int $length length of the matched string. |
||
174 | * @param string|null $content optional content instead of the matched string. |
||
175 | * |
||
176 | * @return bool whether there's a string literal at the current offset. |
||
177 | */ |
||
178 | abstract protected function isStringLiteral(int &$length, ?string &$content): bool; |
||
179 | |||
180 | /** |
||
181 | * Returns whether the given string is a keyword. |
||
182 | * |
||
183 | * The method may set `$content` to a string that will be used as a token content. |
||
184 | * |
||
185 | * @param string $string string to be matched. |
||
186 | * @param string|null $content optional content instead of the matched string. |
||
187 | * |
||
188 | * @return bool whether the given string is a keyword. |
||
189 | */ |
||
190 | abstract protected function isKeyword(string $string, ?string &$content): bool; |
||
191 | |||
192 | /** |
||
193 | * @param string $sql |
||
194 | */ |
||
195 | public function setSql(string $sql): void |
||
196 | { |
||
197 | $this->sql = $sql; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Returns whether the longest common prefix equals to the SQL code of the same length at the current offset. |
||
202 | * |
||
203 | * @param string[] $with strings to be tested. The method **will** modify this parameter to speed up lookups. |
||
204 | * @param bool $caseSensitive whether to perform a case sensitive comparison. |
||
205 | * @param int|null $length length of the matched string. |
||
206 | * @param string|null $content matched string. |
||
207 | * |
||
208 | * @return bool whether a match is found. |
||
209 | */ |
||
210 | protected function startsWithAnyLongest( |
||
211 | array &$with, |
||
212 | bool $caseSensitive, |
||
213 | ?int &$length = null, |
||
214 | ?string &$content = null |
||
215 | ): bool { |
||
216 | if (empty($with)) { |
||
217 | return false; |
||
218 | } |
||
219 | |||
220 | if (!is_array(reset($with))) { |
||
221 | usort($with, function ($string1, $string2) { |
||
222 | return mb_strlen($string2, 'UTF-8') - mb_strlen($string1, 'UTF-8'); |
||
223 | }); |
||
224 | |||
225 | $map = []; |
||
226 | |||
227 | foreach ($with as $string) { |
||
228 | $map[mb_strlen($string, 'UTF-8')][$caseSensitive ? $string : mb_strtoupper($string, 'UTF-8')] = true; |
||
229 | } |
||
230 | |||
231 | $with = $map; |
||
232 | } |
||
233 | foreach ($with as $testLength => $testValues) { |
||
234 | $content = $this->substring($testLength, $caseSensitive); |
||
235 | |||
236 | if (isset($testValues[$content])) { |
||
237 | $length = $testLength; |
||
238 | |||
239 | return true; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | return false; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Returns a string of the given length starting with the specified offset. |
||
248 | * |
||
249 | * @param int $length string length to be returned. |
||
250 | * @param bool $caseSensitive if it's `false`, the string will be uppercased. |
||
251 | * @param int|null $offset SQL code offset, defaults to current if `null` is passed. |
||
252 | * |
||
253 | * @return string result string, it may be empty if there's nothing to return. |
||
254 | */ |
||
255 | protected function substring(int $length, bool $caseSensitive = true, ?int $offset = null) |
||
256 | { |
||
257 | if ($offset === null) { |
||
258 | $offset = $this->offset; |
||
259 | } |
||
260 | |||
261 | if ($offset + $length > $this->length) { |
||
262 | return ''; |
||
263 | } |
||
264 | |||
265 | $cacheKey = $offset . ',' . $length; |
||
266 | |||
267 | if (!isset($this->substrings[$cacheKey . ',1'])) { |
||
268 | $this->substrings[$cacheKey . ',1'] = mb_substr($this->sql, $offset, $length, 'UTF-8'); |
||
269 | } |
||
270 | if (!$caseSensitive && !isset($this->substrings[$cacheKey . ',0'])) { |
||
271 | $this->substrings[$cacheKey . ',0'] = mb_strtoupper($this->substrings[$cacheKey . ',1'], 'UTF-8'); |
||
272 | } |
||
273 | |||
274 | return $this->substrings[$cacheKey . ',' . (int) $caseSensitive]; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Returns an index after the given string in the SQL code starting with the specified offset. |
||
279 | * |
||
280 | * @param string $string string to be found. |
||
281 | * @param int|null $offset SQL code offset, defaults to current if `null` is passed. |
||
282 | * |
||
283 | * @return int index after the given string or end of string index. |
||
284 | */ |
||
285 | protected function indexAfter(string $string, ?int $offset = null): int |
||
286 | { |
||
287 | if ($offset === null) { |
||
288 | $offset = $this->offset; |
||
289 | } |
||
290 | |||
291 | if ($offset + mb_strlen($string, 'UTF-8') > $this->length) { |
||
292 | return $this->length; |
||
293 | } |
||
294 | |||
295 | $afterIndexOf = mb_strpos($this->sql, $string, $offset, 'UTF-8'); |
||
296 | |||
297 | if ($afterIndexOf === false) { |
||
298 | $afterIndexOf = $this->length; |
||
299 | } else { |
||
300 | $afterIndexOf += mb_strlen($string, 'UTF-8'); |
||
301 | } |
||
302 | |||
303 | return $afterIndexOf; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Determines whether there is a delimited string at the current offset and adds it to the token children. |
||
308 | * |
||
309 | * @param int $length |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | private function tokenizeDelimitedString(int &$length): bool |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Determines whether there is an operator at the current offset and adds it to the token children. |
||
338 | * |
||
339 | * @param int $length |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | private function tokenizeOperator(int &$length): bool |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Determines a type of text in the buffer, tokenizes it and adds it to the token children. |
||
428 | */ |
||
429 | private function addTokenFromBuffer(): void |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Adds the specified length to the current offset. |
||
451 | * |
||
452 | * @param int $length |
||
453 | * |
||
454 | * @throws \InvalidArgumentException |
||
455 | */ |
||
456 | private function advance(int $length): void |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Returns whether the SQL code is completely traversed. |
||
468 | * |
||
469 | * @return bool |
||
470 | */ |
||
471 | private function isEof(): bool |
||
474 | } |
||
475 | } |
||
476 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.