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_* |
||
| 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 | 365 | 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 | 374 | 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 | 379 | 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 | 374 | 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 | 365 | 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 | 365 | View Code Duplication | public static function isSymbol($str) |
| 395 | |||
| 396 | // ------------------------------------------------------------------------- |
||
| 397 | // String. |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Checks if the given character is the beginning of a string. |
||
| 401 | * |
||
| 402 | * @param string $str string to be checked |
||
| 403 | * |
||
| 404 | * @return int the appropriate flag for the string type |
||
| 405 | */ |
||
| 406 | 365 | View Code Duplication | public static function isString($str) |
| 419 | |||
| 420 | // ------------------------------------------------------------------------- |
||
| 421 | // Delimiter. |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Checks if the given character can be a separator for two lexeme. |
||
| 425 | * |
||
| 426 | * @param string $str string to be checked |
||
| 427 | * |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | 365 | public static function isSeparator($str) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Loads the specified context. |
||
| 442 | * |
||
| 443 | * Contexts may be used by accessing the context directly. |
||
| 444 | * |
||
| 445 | * @param string $context name of the context or full class name that |
||
| 446 | * defines the context |
||
| 447 | * |
||
| 448 | * @throws LoaderException if the specified context doesn't exist |
||
| 449 | */ |
||
| 450 | 2 | public static function load($context = '') |
|
| 451 | { |
||
| 452 | 2 | if (empty($context)) { |
|
| 453 | 1 | $context = self::$defaultContext; |
|
| 454 | } |
||
| 455 | 2 | if ($context[0] !== '\\') { |
|
| 456 | // Short context name (must be formatted into class name). |
||
| 457 | 2 | $context = self::$contextPrefix . $context; |
|
| 458 | } |
||
| 459 | 2 | if (!class_exists($context)) { |
|
| 460 | 2 | throw @new LoaderException( |
|
| 461 | 2 | 'Specified context ("' . $context . '") does not exist.', |
|
| 462 | 2 | $context |
|
| 463 | ); |
||
| 464 | } |
||
| 465 | 1 | self::$loadedContext = $context; |
|
| 466 | 1 | self::$KEYWORDS = $context::$KEYWORDS; |
|
| 467 | 1 | } |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Loads the context with the closest version to the one specified. |
||
| 471 | * |
||
| 472 | * The closest context is found by replacing last digits with zero until one |
||
| 473 | * is loaded successfully. |
||
| 474 | * |
||
| 475 | * @see Context::load() |
||
| 476 | * |
||
| 477 | * @param string $context name of the context or full class name that |
||
| 478 | * defines the context |
||
| 479 | * |
||
| 480 | * @return string The loaded context. `null` if no context was loaded. |
||
|
|
|||
| 481 | */ |
||
| 482 | 1 | public static function loadClosest($context = '') |
|
| 483 | { |
||
| 484 | /** |
||
| 485 | * The number of replaces done by `preg_replace`. |
||
| 486 | * This actually represents whether a new context was generated or not. |
||
| 487 | * |
||
| 488 | * @var int |
||
| 489 | */ |
||
| 490 | 1 | $count = 0; |
|
| 491 | |||
| 492 | // As long as a new context can be generated, we try to load it. |
||
| 493 | do { |
||
| 494 | try { |
||
| 495 | // Trying to load the new context. |
||
| 496 | 1 | static::load($context); |
|
| 497 | 1 | } catch (LoaderException $e) { |
|
| 498 | // If it didn't work, we are looking for a new one and skipping |
||
| 499 | // over to the next generation that will try the new context. |
||
| 500 | 1 | $context = preg_replace( |
|
| 501 | 1 | '/[1-9](0*)$/', |
|
| 502 | 1 | '0$1', |
|
| 503 | 1 | $context, |
|
| 504 | 1 | -1, |
|
| 505 | 1 | $count |
|
| 506 | ); |
||
| 507 | 1 | continue; |
|
| 508 | } |
||
| 509 | |||
| 510 | // Last generated context was valid (did not throw any exceptions). |
||
| 511 | // So we return it, to let the user know what context was loaded. |
||
| 512 | 1 | return $context; |
|
| 513 | 1 | } while ($count !== 0); |
|
| 514 | |||
| 515 | 1 | return null; |
|
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Sets the SQL mode. |
||
| 520 | * |
||
| 521 | * @param string $mode The list of modes. If empty, the mode is reset. |
||
| 522 | */ |
||
| 523 | 2 | public static function setMode($mode = '') |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Escapes the symbol by adding surrounding backticks. |
||
| 537 | * |
||
| 538 | * @param array|string $str the string to be escaped |
||
| 539 | * @param string $quote quote to be used when escaping |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | 19 | public static function escape($str, $quote = '`') |
|
| 565 | } |
||
| 566 | |||
| 567 | // Initializing the default context. |
||
| 569 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.