Complex classes like Context 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 Context, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class Context |
||
22 | { |
||
23 | /** |
||
24 | * The maximum length of a keyword. |
||
25 | * |
||
26 | * @see static::$TOKEN_KEYWORD |
||
27 | * |
||
28 | * @var int |
||
29 | */ |
||
30 | const KEYWORD_MAX_LENGTH = 30; |
||
31 | |||
32 | /** |
||
33 | * The maximum length of a label. |
||
34 | * |
||
35 | * @see static::$TOKEN_LABEL |
||
36 | * Ref: https://dev.mysql.com/doc/refman/5.7/en/statement-labels.html |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | const LABEL_MAX_LENGTH = 16; |
||
41 | |||
42 | /** |
||
43 | * The maximum length of an operator. |
||
44 | * |
||
45 | * @see static::$TOKEN_OPERATOR |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | const OPERATOR_MAX_LENGTH = 4; |
||
50 | |||
51 | /** |
||
52 | * The name of the default content. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | public static $defaultContext = '\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700'; |
||
57 | |||
58 | /** |
||
59 | * The name of the loaded context. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | public static $loadedContext = '\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700'; |
||
64 | |||
65 | /** |
||
66 | * The prefix concatenated to the context name when an incomplete class name |
||
67 | * is specified. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | public static $contextPrefix = '\\PhpMyAdmin\\SqlParser\\Contexts\\Context'; |
||
72 | |||
73 | /** |
||
74 | * List of keywords. |
||
75 | * |
||
76 | * Because, PHP's associative arrays are basically hash tables, it is more |
||
77 | * efficient to store keywords as keys instead of values. |
||
78 | * |
||
79 | * The value associated to each keyword represents its flags. |
||
80 | * |
||
81 | * @see Token::FLAG_KEYWORD_* |
||
82 | * |
||
83 | * Elements are sorted by flags, length and keyword. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | public static $KEYWORDS = array(); |
||
88 | |||
89 | /** |
||
90 | * List of operators and their flags. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | public static $OPERATORS = array( |
||
95 | // Some operators (*, =) may have ambiguous flags, because they depend on |
||
96 | // the context they are being used in. |
||
97 | // For example: 1. SELECT * FROM table; # SQL specific (wildcard) |
||
98 | // SELECT 2 * 3; # arithmetic |
||
99 | // 2. SELECT * FROM table WHERE foo = 'bar'; |
||
100 | // SET @i = 0; |
||
101 | |||
102 | // @see Token::FLAG_OPERATOR_ARITHMETIC |
||
103 | '%' => 1, '*' => 1, '+' => 1, '-' => 1, '/' => 1, |
||
104 | |||
105 | // @see Token::FLAG_OPERATOR_LOGICAL |
||
106 | '!' => 2, '!=' => 2, '&&' => 2, '<' => 2, '<=' => 2, |
||
107 | '<=>' => 2, '<>' => 2, '=' => 2, '>' => 2, '>=' => 2, |
||
108 | '||' => 2, |
||
109 | |||
110 | // @see Token::FLAG_OPERATOR_BITWISE |
||
111 | '&' => 4, '<<' => 4, '>>' => 4, '^' => 4, '|' => 4, |
||
112 | '~' => 4, |
||
113 | |||
114 | // @see Token::FLAG_OPERATOR_ASSIGNMENT |
||
115 | ':=' => 8, |
||
116 | |||
117 | // @see Token::FLAG_OPERATOR_SQL |
||
118 | '(' => 16, ')' => 16, '.' => 16, ',' => 16, ';' => 16, |
||
119 | ); |
||
120 | |||
121 | /** |
||
122 | * The mode of the MySQL server that will be used in lexing, parsing and |
||
123 | * building the statements. |
||
124 | * |
||
125 | * @var int |
||
126 | */ |
||
127 | public static $MODE = 0; |
||
128 | |||
129 | /* |
||
130 | * Server SQL Modes |
||
131 | * https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html |
||
132 | */ |
||
133 | |||
134 | // Compatibility mode for Microsoft's SQL server. |
||
135 | // This is the equivalent of ANSI_QUOTES. |
||
136 | const SQL_MODE_COMPAT_MYSQL = 2; |
||
137 | |||
138 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_allow_invalid_dates |
||
139 | const SQL_MODE_ALLOW_INVALID_DATES = 1; |
||
140 | |||
141 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ansi_quotes |
||
142 | const SQL_MODE_ANSI_QUOTES = 2; |
||
143 | |||
144 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_error_for_division_by_zero |
||
145 | const SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO = 4; |
||
146 | |||
147 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_high_not_precedence |
||
148 | const SQL_MODE_HIGH_NOT_PRECEDENCE = 8; |
||
149 | |||
150 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ignore_space |
||
151 | const SQL_MODE_IGNORE_SPACE = 16; |
||
152 | |||
153 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_create_user |
||
154 | const SQL_MODE_NO_AUTO_CREATE_USER = 32; |
||
155 | |||
156 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_value_on_zero |
||
157 | const SQL_MODE_NO_AUTO_VALUE_ON_ZERO = 64; |
||
158 | |||
159 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_backslash_escapes |
||
160 | const SQL_MODE_NO_BACKSLASH_ESCAPES = 128; |
||
161 | |||
162 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create |
||
163 | const SQL_MODE_NO_DIR_IN_CREATE = 256; |
||
164 | |||
165 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create |
||
166 | const SQL_MODE_NO_ENGINE_SUBSTITUTION = 512; |
||
167 | |||
168 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_field_options |
||
169 | const SQL_MODE_NO_FIELD_OPTIONS = 1024; |
||
170 | |||
171 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_key_options |
||
172 | const SQL_MODE_NO_KEY_OPTIONS = 2048; |
||
173 | |||
174 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_table_options |
||
175 | const SQL_MODE_NO_TABLE_OPTIONS = 4096; |
||
176 | |||
177 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_unsigned_subtraction |
||
178 | const SQL_MODE_NO_UNSIGNED_SUBTRACTION = 8192; |
||
179 | |||
180 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_date |
||
181 | const SQL_MODE_NO_ZERO_DATE = 16384; |
||
182 | |||
183 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_in_date |
||
184 | const SQL_MODE_NO_ZERO_IN_DATE = 32768; |
||
185 | |||
186 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_only_full_group_by |
||
187 | const SQL_MODE_ONLY_FULL_GROUP_BY = 65536; |
||
188 | |||
189 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_pipes_as_concat |
||
190 | const SQL_MODE_PIPES_AS_CONCAT = 131072; |
||
191 | |||
192 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_real_as_float |
||
193 | const SQL_MODE_REAL_AS_FLOAT = 262144; |
||
194 | |||
195 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_all_tables |
||
196 | const SQL_MODE_STRICT_ALL_TABLES = 524288; |
||
197 | |||
198 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_trans_tables |
||
199 | const SQL_MODE_STRICT_TRANS_TABLES = 1048576; |
||
200 | |||
201 | // Custom modes. |
||
202 | |||
203 | // The table and column names and any other field that must be escaped will |
||
204 | // not be. |
||
205 | // Reserved keywords are being escaped regardless this mode is used or not. |
||
206 | const SQL_MODE_NO_ENCLOSING_QUOTES = 1073741824; |
||
207 | |||
208 | /* |
||
209 | * Combination SQL Modes |
||
210 | * https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sql-mode-combo |
||
211 | */ |
||
212 | |||
213 | // REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE |
||
214 | const SQL_MODE_ANSI = 393234; |
||
215 | |||
216 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
217 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, |
||
218 | const SQL_MODE_DB2 = 138258; |
||
219 | |||
220 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
221 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER |
||
222 | const SQL_MODE_MAXDB = 138290; |
||
223 | |||
224 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
225 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS |
||
226 | const SQL_MODE_MSSQL = 138258; |
||
227 | |||
228 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
229 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER |
||
230 | const SQL_MODE_ORACLE = 138290; |
||
231 | |||
232 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
233 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS |
||
234 | const SQL_MODE_POSTGRESQL = 138258; |
||
235 | |||
236 | // STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, |
||
237 | // ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER |
||
238 | const SQL_MODE_TRADITIONAL = 1622052; |
||
239 | |||
240 | // ------------------------------------------------------------------------- |
||
241 | // Keyword. |
||
242 | |||
243 | /** |
||
244 | * Checks if the given string is a keyword. |
||
245 | * |
||
246 | * @param string $str string to be checked |
||
247 | * @param bool $isReserved checks if the keyword is reserved |
||
248 | * |
||
249 | * @return int |
||
250 | */ |
||
251 | 366 | public static function isKeyword($str, $isReserved = false) |
|
267 | |||
268 | // ------------------------------------------------------------------------- |
||
269 | // Operator. |
||
270 | |||
271 | /** |
||
272 | * Checks if the given string is an operator. |
||
273 | * |
||
274 | * @param string $str string to be checked |
||
275 | * |
||
276 | * @return int the appropriate flag for the operator |
||
277 | */ |
||
278 | 375 | public static function isOperator($str) |
|
286 | |||
287 | // ------------------------------------------------------------------------- |
||
288 | // Whitespace. |
||
289 | |||
290 | /** |
||
291 | * Checks if the given character is a whitespace. |
||
292 | * |
||
293 | * @param string $str string to be checked |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | 380 | public static function isWhitespace($str) |
|
301 | |||
302 | // ------------------------------------------------------------------------- |
||
303 | // Comment. |
||
304 | |||
305 | /** |
||
306 | * Checks if the given string is the beginning of a whitespace. |
||
307 | * |
||
308 | * @param string $str string to be checked |
||
309 | * |
||
310 | * @return int the appropriate flag for the comment type |
||
311 | */ |
||
312 | 375 | public static function isComment($str, $end=false) |
|
335 | |||
336 | // ------------------------------------------------------------------------- |
||
337 | // Bool. |
||
338 | |||
339 | /** |
||
340 | * Checks if the given string is a boolean value. |
||
341 | * This actually check only for `TRUE` and `FALSE` because `1` or `0` are |
||
342 | * actually numbers and are parsed by specific methods. |
||
343 | * |
||
344 | * @param string $str string to be checked |
||
345 | * |
||
346 | * @return bool |
||
347 | */ |
||
348 | 366 | public static function isBool($str) |
|
354 | |||
355 | // ------------------------------------------------------------------------- |
||
356 | // Number. |
||
357 | |||
358 | /** |
||
359 | * Checks if the given character can be a part of a number. |
||
360 | * |
||
361 | * @param string $str string to be checked |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | 1 | public static function isNumber($str) |
|
370 | |||
371 | // ------------------------------------------------------------------------- |
||
372 | // Symbol. |
||
373 | |||
374 | /** |
||
375 | * Checks if the given character is the beginning of a symbol. A symbol |
||
376 | * can be either a variable or a field name. |
||
377 | * |
||
378 | * @param string $str string to be checked |
||
379 | * |
||
380 | * @return int the appropriate flag for the symbol type |
||
381 | */ |
||
382 | 366 | public static function isSymbol($str) |
|
383 | { |
||
384 | 366 | if (strlen($str) == 0) { |
|
385 | return null; |
||
386 | } |
||
387 | 366 | if ($str[0] === '@') { |
|
388 | 29 | return Token::FLAG_SYMBOL_VARIABLE; |
|
389 | 365 | } elseif ($str[0] === '`') { |
|
390 | 78 | return Token::FLAG_SYMBOL_BACKTICK; |
|
391 | 365 | } elseif ($str[0] === ':') { |
|
392 | 1 | return Token::FLAG_SYMBOL_PARAMETER; |
|
393 | } |
||
394 | |||
395 | 365 | return null; |
|
396 | } |
||
397 | |||
398 | // ------------------------------------------------------------------------- |
||
399 | // String. |
||
400 | |||
401 | /** |
||
402 | * Checks if the given character is the beginning of a string. |
||
403 | * |
||
404 | * @param string $str string to be checked |
||
405 | * |
||
406 | * @return int the appropriate flag for the string type |
||
407 | */ |
||
408 | 366 | public static function isString($str) |
|
421 | |||
422 | // ------------------------------------------------------------------------- |
||
423 | // Delimiter. |
||
424 | |||
425 | /** |
||
426 | * Checks if the given character can be a separator for two lexeme. |
||
427 | * |
||
428 | * @param string $str string to be checked |
||
429 | * |
||
430 | * @return bool |
||
431 | */ |
||
432 | 366 | public static function isSeparator($str) |
|
433 | { |
||
434 | // NOTES: Only non alphanumeric ASCII characters may be separators. |
||
435 | // `~` is the last printable ASCII character. |
||
436 | 366 | return ($str <= '~') && ($str !== '_') |
|
437 | 366 | && (($str < '0') || ($str > '9')) |
|
438 | 366 | && (($str < 'a') || ($str > 'z')) |
|
439 | 366 | && (($str < 'A') || ($str > 'Z')); |
|
440 | } |
||
441 | |||
442 | /** |
||
443 | * Loads the specified context. |
||
444 | * |
||
445 | * Contexts may be used by accessing the context directly. |
||
446 | * |
||
447 | * @param string $context name of the context or full class name that |
||
448 | * defines the context |
||
449 | * |
||
450 | * @throws LoaderException if the specified context doesn't exist |
||
451 | */ |
||
452 | 12 | public static function load($context = '') |
|
470 | |||
471 | /** |
||
472 | * Loads the context with the closest version to the one specified. |
||
473 | * |
||
474 | * The closest context is found by replacing last digits with zero until one |
||
475 | * is loaded successfully. |
||
476 | * |
||
477 | * @see Context::load() |
||
478 | * |
||
479 | * @param string $context name of the context or full class name that |
||
480 | * defines the context |
||
481 | * |
||
482 | * @return string The loaded context. `null` if no context was loaded. |
||
|
|||
483 | */ |
||
484 | 1 | public static function loadClosest($context = '') |
|
519 | |||
520 | /** |
||
521 | * Sets the SQL mode. |
||
522 | * |
||
523 | * @param string $mode The list of modes. If empty, the mode is reset. |
||
524 | */ |
||
525 | 2 | public static function setMode($mode = '') |
|
536 | |||
537 | /** |
||
538 | * Escapes the symbol by adding surrounding backticks. |
||
539 | * |
||
540 | * @param array|string $str the string to be escaped |
||
541 | * @param string $quote quote to be used when escaping |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | 19 | public static function escape($str, $quote = '`') |
|
567 | } |
||
568 | |||
569 | // Initializing the default context. |
||
571 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.