Complex classes like SqlTokenizer 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 SqlTokenizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | abstract class SqlTokenizer extends Component |
||
32 | { |
||
33 | /** |
||
34 | * @var string SQL code. |
||
35 | */ |
||
36 | public $sql; |
||
37 | |||
38 | /** |
||
39 | * @var int SQL code string length. |
||
40 | */ |
||
41 | protected $length; |
||
42 | /** |
||
43 | * @var int SQL code string current offset. |
||
44 | */ |
||
45 | protected $offset; |
||
46 | |||
47 | /** |
||
48 | * @var \SplStack stack of active tokens. |
||
49 | */ |
||
50 | private $_tokenStack; |
||
51 | /** |
||
52 | * @var SqlToken active token. It's usually a top of the token stack. |
||
53 | */ |
||
54 | private $_currentToken; |
||
55 | /** |
||
56 | * @var string[] cached substrings. |
||
57 | */ |
||
58 | private $_substrings; |
||
59 | /** |
||
60 | * @var string current buffer value. |
||
61 | */ |
||
62 | private $_buffer = ''; |
||
63 | /** |
||
64 | * @var SqlToken resulting token of a last [[tokenize()]] call. |
||
65 | */ |
||
66 | private $_token; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Constructor. |
||
71 | * @param string $sql SQL code to be tokenized. |
||
72 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
73 | */ |
||
74 | 13 | public function __construct($sql, $config = []) |
|
79 | |||
80 | /** |
||
81 | * Tokenizes and returns a code type token. |
||
82 | * @return SqlToken code type token. |
||
83 | */ |
||
84 | 13 | public function tokenize() |
|
120 | |||
121 | /** |
||
122 | * Returns whether there's a whitespace at the current offset. |
||
123 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
124 | * @param int $length length of the matched string. |
||
125 | * @return bool whether there's a whitespace at the current offset. |
||
126 | */ |
||
127 | abstract protected function isWhitespace(&$length); |
||
128 | |||
129 | /** |
||
130 | * Returns whether there's a commentary at the current offset. |
||
131 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
132 | * @param int $length length of the matched string. |
||
133 | * @return bool whether there's a commentary at the current offset. |
||
134 | */ |
||
135 | abstract protected function isComment(&$length); |
||
136 | |||
137 | /** |
||
138 | * Returns whether there's an operator at the current offset. |
||
139 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
140 | * It may also set `$content` to a string that will be used as a token content. |
||
141 | * @param int $length length of the matched string. |
||
142 | * @param string $content optional content instead of the matched string. |
||
143 | * @return bool whether there's an operator at the current offset. |
||
144 | */ |
||
145 | abstract protected function isOperator(&$length, &$content); |
||
146 | |||
147 | /** |
||
148 | * Returns whether there's an identifier at the current offset. |
||
149 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
150 | * It may also set `$content` to a string that will be used as a token content. |
||
151 | * @param int $length length of the matched string. |
||
152 | * @param string $content optional content instead of the matched string. |
||
153 | * @return bool whether there's an identifier at the current offset. |
||
154 | */ |
||
155 | abstract protected function isIdentifier(&$length, &$content); |
||
156 | |||
157 | /** |
||
158 | * Returns whether there's a string literal at the current offset. |
||
159 | * If this methos returns `true`, it has to set the `$length` parameter to the length of the matched string. |
||
160 | * It may also set `$content` to a string that will be used as a token content. |
||
161 | * @param int $length length of the matched string. |
||
162 | * @param string $content optional content instead of the matched string. |
||
163 | * @return bool whether there's a string literal at the current offset. |
||
164 | */ |
||
165 | abstract protected function isStringLiteral(&$length, &$content); |
||
166 | |||
167 | /** |
||
168 | * Returns whether the given string is a keyword. |
||
169 | * The method may set `$content` to a string that will be used as a token content. |
||
170 | * @param string $string string to be matched. |
||
171 | * @param string $content optional content instead of the matched string. |
||
172 | * @return bool whether the given string is a keyword. |
||
173 | */ |
||
174 | abstract protected function isKeyword($string, &$content); |
||
175 | |||
176 | /** |
||
177 | * Returns whether the longest common prefix equals to the SQL code of the same length at the current offset. |
||
178 | * @param string[] $with strings to be tested. |
||
179 | * The method **will** modify this parameter to speed up lookups. |
||
180 | * @param bool $caseSensitive whether to perform a case sensitive comparison. |
||
181 | * @param int|null $length length of the matched string. |
||
182 | * @param string|null $content matched string. |
||
183 | * @return bool whether a match is found. |
||
184 | */ |
||
185 | 13 | protected function startsWithAnyLongest(array &$with, $caseSensitive, &$length = null, &$content = null) |
|
210 | |||
211 | /** |
||
212 | * Returns a string of the given length starting with the specified offset. |
||
213 | * @param int $length string length to be returned. |
||
214 | * @param bool $caseSensitive if it's `false`, the string will be uppercased. |
||
215 | * @param int|null $offset SQL code offset, defaults to current if `null` is passed. |
||
216 | * @return string result string, it may be empty if there's nothing to return. |
||
217 | */ |
||
218 | 13 | protected function substring($length, $caseSensitive = true, $offset = null) |
|
236 | |||
237 | /** |
||
238 | * Returns an index after the given string in the SQL code starting with the specified offset. |
||
239 | * @param string $string string to be found. |
||
240 | * @param int|null $offset SQL code offset, defaults to current if `null` is passed. |
||
241 | * @return int index after the given string or end of string index. |
||
242 | */ |
||
243 | 13 | protected function indexAfter($string, $offset = null) |
|
260 | |||
261 | /** |
||
262 | * Determines whether there is a delimited string at the current offset and adds it to the token children. |
||
263 | * @param int $length |
||
264 | * @return bool |
||
265 | */ |
||
266 | 13 | private function tokenizeDelimitedString(&$length) |
|
283 | |||
284 | /** |
||
285 | * Determines whether there is an operator at the current offset and adds it to the token children. |
||
286 | * @param int $length |
||
287 | * @return bool |
||
288 | */ |
||
289 | 13 | private function tokenizeOperator(&$length) |
|
346 | |||
347 | /** |
||
348 | * Determines a type of text in the buffer, tokenizes it and adds it to the token children. |
||
349 | */ |
||
350 | 13 | private function addTokenFromBuffer() |
|
365 | |||
366 | /** |
||
367 | * Adds the specified length to the current offset. |
||
368 | * @param int $length |
||
369 | * @throws InvalidParamException |
||
370 | */ |
||
371 | 13 | private function advance($length) |
|
380 | |||
381 | /** |
||
382 | * Returns whether the SQL code is completely traversed. |
||
383 | * @return bool |
||
384 | */ |
||
385 | 13 | private function isEof() |
|
389 | } |
||
390 |