Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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_RESERVED Token::FLAG_KEYWORD_COMPOSED |
||
| 82 | * Token::FLAG_KEYWORD_DATA_TYPE Token::FLAG_KEYWORD_KEY |
||
| 83 | * Token::FLAG_KEYWORD_FUNCTION |
||
| 84 | * |
||
| 85 | * Elements are sorted by flags, length and keyword. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | public static $KEYWORDS = array(); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * List of operators and their flags. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | public static $OPERATORS = array( |
||
| 97 | // Some operators (*, =) may have ambiguous flags, because they depend on |
||
| 98 | // the context they are being used in. |
||
| 99 | // For example: 1. SELECT * FROM table; # SQL specific (wildcard) |
||
| 100 | // SELECT 2 * 3; # arithmetic |
||
| 101 | // 2. SELECT * FROM table WHERE foo = 'bar'; |
||
| 102 | // SET @i = 0; |
||
| 103 | |||
| 104 | // @see Token::FLAG_OPERATOR_ARITHMETIC |
||
| 105 | '%' => 1, |
||
| 106 | '*' => 1, |
||
| 107 | '+' => 1, |
||
| 108 | '-' => 1, |
||
| 109 | '/' => 1, |
||
| 110 | |||
| 111 | // @see Token::FLAG_OPERATOR_LOGICAL |
||
| 112 | '!' => 2, |
||
| 113 | '!=' => 2, |
||
| 114 | '&&' => 2, |
||
| 115 | '<' => 2, |
||
| 116 | '<=' => 2, |
||
| 117 | '<=>' => 2, |
||
| 118 | '<>' => 2, |
||
| 119 | '=' => 2, |
||
| 120 | '>' => 2, |
||
| 121 | '>=' => 2, |
||
| 122 | '||' => 2, |
||
| 123 | |||
| 124 | // @see Token::FLAG_OPERATOR_BITWISE |
||
| 125 | '&' => 4, |
||
| 126 | '<<' => 4, |
||
| 127 | '>>' => 4, |
||
| 128 | '^' => 4, |
||
| 129 | '|' => 4, |
||
| 130 | '~' => 4, |
||
| 131 | |||
| 132 | // @see Token::FLAG_OPERATOR_ASSIGNMENT |
||
| 133 | ':=' => 8, |
||
| 134 | |||
| 135 | // @see Token::FLAG_OPERATOR_SQL |
||
| 136 | '(' => 16, |
||
| 137 | ')' => 16, |
||
| 138 | '.' => 16, |
||
| 139 | ',' => 16, |
||
| 140 | ';' => 16 |
||
| 141 | ); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * The mode of the MySQL server that will be used in lexing, parsing and |
||
| 145 | * building the statements. |
||
| 146 | * |
||
| 147 | * @var int |
||
| 148 | */ |
||
| 149 | public static $MODE = 0; |
||
| 150 | |||
| 151 | /* |
||
| 152 | * Server SQL Modes |
||
| 153 | * https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html |
||
| 154 | */ |
||
| 155 | |||
| 156 | // Compatibility mode for Microsoft's SQL server. |
||
| 157 | // This is the equivalent of ANSI_QUOTES. |
||
| 158 | const SQL_MODE_COMPAT_MYSQL = 2; |
||
| 159 | |||
| 160 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_allow_invalid_dates |
||
| 161 | const SQL_MODE_ALLOW_INVALID_DATES = 1; |
||
| 162 | |||
| 163 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ansi_quotes |
||
| 164 | const SQL_MODE_ANSI_QUOTES = 2; |
||
| 165 | |||
| 166 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_error_for_division_by_zero |
||
| 167 | const SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO = 4; |
||
| 168 | |||
| 169 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_high_not_precedence |
||
| 170 | const SQL_MODE_HIGH_NOT_PRECEDENCE = 8; |
||
| 171 | |||
| 172 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ignore_space |
||
| 173 | const SQL_MODE_IGNORE_SPACE = 16; |
||
| 174 | |||
| 175 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_create_user |
||
| 176 | const SQL_MODE_NO_AUTO_CREATE_USER = 32; |
||
| 177 | |||
| 178 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_value_on_zero |
||
| 179 | const SQL_MODE_NO_AUTO_VALUE_ON_ZERO = 64; |
||
| 180 | |||
| 181 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_backslash_escapes |
||
| 182 | const SQL_MODE_NO_BACKSLASH_ESCAPES = 128; |
||
| 183 | |||
| 184 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create |
||
| 185 | const SQL_MODE_NO_DIR_IN_CREATE = 256; |
||
| 186 | |||
| 187 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create |
||
| 188 | const SQL_MODE_NO_ENGINE_SUBSTITUTION = 512; |
||
| 189 | |||
| 190 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_field_options |
||
| 191 | const SQL_MODE_NO_FIELD_OPTIONS = 1024; |
||
| 192 | |||
| 193 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_key_options |
||
| 194 | const SQL_MODE_NO_KEY_OPTIONS = 2048; |
||
| 195 | |||
| 196 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_table_options |
||
| 197 | const SQL_MODE_NO_TABLE_OPTIONS = 4096; |
||
| 198 | |||
| 199 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_unsigned_subtraction |
||
| 200 | const SQL_MODE_NO_UNSIGNED_SUBTRACTION = 8192; |
||
| 201 | |||
| 202 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_date |
||
| 203 | const SQL_MODE_NO_ZERO_DATE = 16384; |
||
| 204 | |||
| 205 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_in_date |
||
| 206 | const SQL_MODE_NO_ZERO_IN_DATE = 32768; |
||
| 207 | |||
| 208 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_only_full_group_by |
||
| 209 | const SQL_MODE_ONLY_FULL_GROUP_BY = 65536; |
||
| 210 | |||
| 211 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_pipes_as_concat |
||
| 212 | const SQL_MODE_PIPES_AS_CONCAT = 131072; |
||
| 213 | |||
| 214 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_real_as_float |
||
| 215 | const SQL_MODE_REAL_AS_FLOAT = 262144; |
||
| 216 | |||
| 217 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_all_tables |
||
| 218 | const SQL_MODE_STRICT_ALL_TABLES = 524288; |
||
| 219 | |||
| 220 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_trans_tables |
||
| 221 | const SQL_MODE_STRICT_TRANS_TABLES = 1048576; |
||
| 222 | |||
| 223 | // Custom modes. |
||
| 224 | |||
| 225 | // The table and column names and any other field that must be escaped will |
||
| 226 | // not be. |
||
| 227 | // Reserved keywords are being escaped regardless this mode is used or not. |
||
| 228 | const SQL_MODE_NO_ENCLOSING_QUOTES = 1073741824; |
||
| 229 | |||
| 230 | /* |
||
| 231 | * Combination SQL Modes |
||
| 232 | * https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sql-mode-combo |
||
| 233 | */ |
||
| 234 | |||
| 235 | // REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE |
||
| 236 | const SQL_MODE_ANSI = 393234; |
||
| 237 | |||
| 238 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
| 239 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, |
||
| 240 | const SQL_MODE_DB2 = 138258; |
||
| 241 | |||
| 242 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
| 243 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER |
||
| 244 | const SQL_MODE_MAXDB = 138290; |
||
| 245 | |||
| 246 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
| 247 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS |
||
| 248 | const SQL_MODE_MSSQL = 138258; |
||
| 249 | |||
| 250 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
| 251 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER |
||
| 252 | const SQL_MODE_ORACLE = 138290; |
||
| 253 | |||
| 254 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
| 255 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS |
||
| 256 | const SQL_MODE_POSTGRESQL = 138258; |
||
| 257 | |||
| 258 | // STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, |
||
| 259 | // ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER |
||
| 260 | const SQL_MODE_TRADITIONAL = 1622052; |
||
| 261 | |||
| 262 | // ------------------------------------------------------------------------- |
||
| 263 | // Keyword. |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Checks if the given string is a keyword. |
||
| 267 | * |
||
| 268 | * @param string $str string to be checked |
||
| 269 | * @param bool $isReserved checks if the keyword is reserved |
||
| 270 | * |
||
| 271 | * @return int|null |
||
| 272 | */ |
||
| 273 | 468 | public static function isKeyword($str, $isReserved = false) |
|
| 287 | |||
| 288 | // ------------------------------------------------------------------------- |
||
| 289 | // Operator. |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Checks if the given string is an operator. |
||
| 293 | * |
||
| 294 | * @param string $str string to be checked |
||
| 295 | * |
||
| 296 | * @return int|null the appropriate flag for the operator |
||
| 297 | */ |
||
| 298 | 476 | public static function isOperator($str) |
|
| 306 | |||
| 307 | // ------------------------------------------------------------------------- |
||
| 308 | // Whitespace. |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Checks if the given character is a whitespace. |
||
| 312 | * |
||
| 313 | * @param string $str string to be checked |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | 481 | public static function isWhitespace($str) |
|
| 321 | |||
| 322 | // ------------------------------------------------------------------------- |
||
| 323 | // Comment. |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Checks if the given string is the beginning of a whitespace. |
||
| 327 | * |
||
| 328 | * @param string $str string to be checked |
||
| 329 | * @param mixed $end |
||
| 330 | * |
||
| 331 | * @return int|null the appropriate flag for the comment type |
||
| 332 | */ |
||
| 333 | 476 | public static function isComment($str, $end = false) |
|
| 366 | |||
| 367 | // ------------------------------------------------------------------------- |
||
| 368 | // Bool. |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Checks if the given string is a boolean value. |
||
| 372 | * This actually check only for `TRUE` and `FALSE` because `1` or `0` are |
||
| 373 | * actually numbers and are parsed by specific methods. |
||
| 374 | * |
||
| 375 | * @param string $str string to be checked |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | 468 | public static function isBool($str) |
|
| 385 | |||
| 386 | // ------------------------------------------------------------------------- |
||
| 387 | // Number. |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Checks if the given character can be a part of a number. |
||
| 391 | * |
||
| 392 | * @param string $str string to be checked |
||
| 393 | * |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | 1 | public static function isNumber($str) |
|
| 401 | |||
| 402 | // ------------------------------------------------------------------------- |
||
| 403 | // Symbol. |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Checks if the given character is the beginning of a symbol. A symbol |
||
| 407 | * can be either a variable or a field name. |
||
| 408 | * |
||
| 409 | * @param string $str string to be checked |
||
| 410 | * |
||
| 411 | * @return int|null the appropriate flag for the symbol type |
||
| 412 | */ |
||
| 413 | 468 | public static function isSymbol($str) |
|
| 428 | |||
| 429 | // ------------------------------------------------------------------------- |
||
| 430 | // String. |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Checks if the given character is the beginning of a string. |
||
| 434 | * |
||
| 435 | * @param string $str string to be checked |
||
| 436 | * |
||
| 437 | * @return int|null the appropriate flag for the string type |
||
| 438 | */ |
||
| 439 | 468 | public static function isString($str) |
|
| 452 | |||
| 453 | // ------------------------------------------------------------------------- |
||
| 454 | // Delimiter. |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Checks if the given character can be a separator for two lexeme. |
||
| 458 | * |
||
| 459 | * @param string $str string to be checked |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | 468 | public static function isSeparator($str) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Loads the specified context. |
||
| 475 | * |
||
| 476 | * Contexts may be used by accessing the context directly. |
||
| 477 | * |
||
| 478 | * @param string $context name of the context or full class name that |
||
| 479 | * defines the context |
||
| 480 | * |
||
| 481 | * @throws LoaderException if the specified context doesn't exist |
||
| 482 | */ |
||
| 483 | 21 | public static function load($context = '') |
|
| 484 | { |
||
| 485 | 21 | if (empty($context)) { |
|
| 486 | 18 | $context = self::$defaultContext; |
|
| 487 | } |
||
| 488 | 21 | if ($context[0] !== '\\') { |
|
| 489 | // Short context name (must be formatted into class name). |
||
| 490 | 20 | $context = self::$contextPrefix . $context; |
|
| 491 | } |
||
| 492 | 21 | if (! class_exists($context)) { |
|
| 493 | 6 | throw @new LoaderException( |
|
| 494 | 6 | 'Specified context ("' . $context . '") does not exist.', |
|
| 495 | $context |
||
| 496 | ); |
||
| 497 | } |
||
| 498 | 20 | self::$loadedContext = $context; |
|
| 499 | 20 | self::$KEYWORDS = $context::$KEYWORDS; |
|
| 500 | 20 | } |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Loads the context with the closest version to the one specified. |
||
| 504 | * |
||
| 505 | * The closest context is found by replacing last digits with zero until one |
||
| 506 | * is loaded successfully. |
||
| 507 | * |
||
| 508 | * @see Context::load() |
||
| 509 | * |
||
| 510 | * @param string $context name of the context or full class name that |
||
| 511 | * defines the context |
||
| 512 | * |
||
| 513 | * @return string|null The loaded context. `null` if no context was loaded. |
||
| 514 | */ |
||
| 515 | 7 | public static function loadClosest($context = '') |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Sets the SQL mode. |
||
| 547 | * |
||
| 548 | * @param string $mode The list of modes. If empty, the mode is reset. |
||
| 549 | */ |
||
| 550 | 262 | public static function setMode($mode = '') |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Escapes the symbol by adding surrounding backticks. |
||
| 564 | * |
||
| 565 | * @param array|string $str the string to be escaped |
||
| 566 | * @param string $quote quote to be used when escaping |
||
| 567 | * |
||
| 568 | * @return string|array |
||
| 569 | */ |
||
| 570 | 39 | public static function escape($str, $quote = '`') |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Returns char used to quote identifiers based on currently set SQL Mode (ie. standard or ANSI_QUOTES) |
||
| 595 | * @return string either " (double quote, ansi_quotes mode) or ` (backtick, standard mode) |
||
| 596 | */ |
||
| 597 | public static function getIdentifierQuote() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Function verifies that given SQL Mode constant is currently set |
||
| 604 | * |
||
| 605 | * @return boolean false on empty param, true/false on given constant/int value |
||
| 606 | * @param int $flag for example Context::SQL_MODE_ANSI_QUOTES |
||
| 607 | */ |
||
| 608 | public static function hasMode($flag = null) |
||
| 615 | } |
||
| 616 | |||
| 619 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: