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:
| 1 | <?php |
||
| 23 | class Parser extends Core |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Array of classes that are used in parsing the SQL statements. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | public static $STATEMENT_PARSERS = array( |
||
| 31 | // MySQL Utility Statements |
||
| 32 | 'DESCRIBE' => 'PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement', |
||
| 33 | 'DESC' => 'PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement', |
||
| 34 | 'EXPLAIN' => 'PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement', |
||
| 35 | 'FLUSH' => '', |
||
| 36 | 'GRANT' => '', |
||
| 37 | 'HELP' => '', |
||
| 38 | 'SET PASSWORD' => '', |
||
| 39 | 'STATUS' => '', |
||
| 40 | 'USE' => '', |
||
| 41 | |||
| 42 | // Table Maintenance Statements |
||
| 43 | // https://dev.mysql.com/doc/refman/5.7/en/table-maintenance-sql.html |
||
| 44 | 'ANALYZE' => 'PhpMyAdmin\\SqlParser\\Statements\\AnalyzeStatement', |
||
| 45 | 'BACKUP' => 'PhpMyAdmin\\SqlParser\\Statements\\BackupStatement', |
||
| 46 | 'CHECK' => 'PhpMyAdmin\\SqlParser\\Statements\\CheckStatement', |
||
| 47 | 'CHECKSUM' => 'PhpMyAdmin\\SqlParser\\Statements\\ChecksumStatement', |
||
| 48 | 'OPTIMIZE' => 'PhpMyAdmin\\SqlParser\\Statements\\OptimizeStatement', |
||
| 49 | 'REPAIR' => 'PhpMyAdmin\\SqlParser\\Statements\\RepairStatement', |
||
| 50 | 'RESTORE' => 'PhpMyAdmin\\SqlParser\\Statements\\RestoreStatement', |
||
| 51 | |||
| 52 | // Database Administration Statements |
||
| 53 | // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-server-administration.html |
||
| 54 | 'SET' => 'PhpMyAdmin\\SqlParser\\Statements\\SetStatement', |
||
| 55 | 'SHOW' => 'PhpMyAdmin\\SqlParser\\Statements\\ShowStatement', |
||
| 56 | |||
| 57 | // Data Definition Statements. |
||
| 58 | // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-definition.html |
||
| 59 | 'ALTER' => 'PhpMyAdmin\\SqlParser\\Statements\\AlterStatement', |
||
| 60 | 'CREATE' => 'PhpMyAdmin\\SqlParser\\Statements\\CreateStatement', |
||
| 61 | 'DROP' => 'PhpMyAdmin\\SqlParser\\Statements\\DropStatement', |
||
| 62 | 'RENAME' => 'PhpMyAdmin\\SqlParser\\Statements\\RenameStatement', |
||
| 63 | 'TRUNCATE' => 'PhpMyAdmin\\SqlParser\\Statements\\TruncateStatement', |
||
| 64 | |||
| 65 | // Data Manipulation Statements. |
||
| 66 | // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-manipulation.html |
||
| 67 | 'CALL' => 'PhpMyAdmin\\SqlParser\\Statements\\CallStatement', |
||
| 68 | 'DELETE' => 'PhpMyAdmin\\SqlParser\\Statements\\DeleteStatement', |
||
| 69 | 'DO' => '', |
||
| 70 | 'HANDLER' => '', |
||
| 71 | 'INSERT' => 'PhpMyAdmin\\SqlParser\\Statements\\InsertStatement', |
||
| 72 | 'LOAD' => '', |
||
| 73 | 'REPLACE' => 'PhpMyAdmin\\SqlParser\\Statements\\ReplaceStatement', |
||
| 74 | 'SELECT' => 'PhpMyAdmin\\SqlParser\\Statements\\SelectStatement', |
||
| 75 | 'UPDATE' => 'PhpMyAdmin\\SqlParser\\Statements\\UpdateStatement', |
||
| 76 | |||
| 77 | // Prepared Statements. |
||
| 78 | // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html |
||
| 79 | 'DEALLOCATE' => '', |
||
| 80 | 'EXECUTE' => '', |
||
| 81 | 'PREPARE' => '', |
||
| 82 | |||
| 83 | // Transactional and Locking Statements |
||
| 84 | // https://dev.mysql.com/doc/refman/5.7/en/commit.html |
||
| 85 | 'BEGIN' => 'PhpMyAdmin\\SqlParser\\Statements\\TransactionStatement', |
||
| 86 | 'COMMIT' => 'PhpMyAdmin\\SqlParser\\Statements\\TransactionStatement', |
||
| 87 | 'ROLLBACK' => 'PhpMyAdmin\\SqlParser\\Statements\\TransactionStatement', |
||
| 88 | 'START TRANSACTION' => 'PhpMyAdmin\\SqlParser\\Statements\\TransactionStatement', |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Array of classes that are used in parsing SQL components. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | public static $KEYWORD_PARSERS = array( |
||
| 97 | // This is not a proper keyword and was added here to help the |
||
| 98 | // formatter. |
||
| 99 | 'PARTITION BY' => array(), |
||
| 100 | 'SUBPARTITION BY' => array(), |
||
| 101 | |||
| 102 | // This is not a proper keyword and was added here to help the |
||
| 103 | // builder. |
||
| 104 | '_OPTIONS' => array( |
||
| 105 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\OptionsArray', |
||
| 106 | 'field' => 'options', |
||
| 107 | ), |
||
| 108 | '_END_OPTIONS' => array( |
||
| 109 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\OptionsArray', |
||
| 110 | 'field' => 'end_options', |
||
| 111 | ), |
||
| 112 | |||
| 113 | 'UNION' => array( |
||
| 114 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\UnionKeyword', |
||
| 115 | 'field' => 'union', |
||
| 116 | ), |
||
| 117 | 'UNION ALL' => array( |
||
| 118 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\UnionKeyword', |
||
| 119 | 'field' => 'union', |
||
| 120 | ), |
||
| 121 | 'UNION DISTINCT' => array( |
||
| 122 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\UnionKeyword', |
||
| 123 | 'field' => 'union', |
||
| 124 | ), |
||
| 125 | |||
| 126 | // Actual clause parsers. |
||
| 127 | 'ALTER' => array( |
||
| 128 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Expression', |
||
| 129 | 'field' => 'table', |
||
| 130 | 'options' => array('parseField' => 'table'), |
||
| 131 | ), |
||
| 132 | 'ANALYZE' => array( |
||
| 133 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 134 | 'field' => 'tables', |
||
| 135 | 'options' => array('parseField' => 'table'), |
||
| 136 | ), |
||
| 137 | 'BACKUP' => array( |
||
| 138 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 139 | 'field' => 'tables', |
||
| 140 | 'options' => array('parseField' => 'table'), |
||
| 141 | ), |
||
| 142 | 'CALL' => array( |
||
| 143 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\FunctionCall', |
||
| 144 | 'field' => 'call', |
||
| 145 | ), |
||
| 146 | 'CHECK' => array( |
||
| 147 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 148 | 'field' => 'tables', |
||
| 149 | 'options' => array('parseField' => 'table'), |
||
| 150 | ), |
||
| 151 | 'CHECKSUM' => array( |
||
| 152 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 153 | 'field' => 'tables', |
||
| 154 | 'options' => array('parseField' => 'table'), |
||
| 155 | ), |
||
| 156 | 'CROSS JOIN' => array( |
||
| 157 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 158 | 'field' => 'join', |
||
| 159 | ), |
||
| 160 | 'DROP' => array( |
||
| 161 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 162 | 'field' => 'fields', |
||
| 163 | 'options' => array('parseField' => 'table'), |
||
| 164 | ), |
||
| 165 | 'FROM' => array( |
||
| 166 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 167 | 'field' => 'from', |
||
| 168 | 'options' => array('field' => 'table'), |
||
| 169 | ), |
||
| 170 | 'GROUP BY' => array( |
||
| 171 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\OrderKeyword', |
||
| 172 | 'field' => 'group', |
||
| 173 | ), |
||
| 174 | 'HAVING' => array( |
||
| 175 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Condition', |
||
| 176 | 'field' => 'having', |
||
| 177 | ), |
||
| 178 | 'INTO' => array( |
||
| 179 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\IntoKeyword', |
||
| 180 | 'field' => 'into', |
||
| 181 | ), |
||
| 182 | 'JOIN' => array( |
||
| 183 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 184 | 'field' => 'join', |
||
| 185 | ), |
||
| 186 | 'LEFT JOIN' => array( |
||
| 187 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 188 | 'field' => 'join', |
||
| 189 | ), |
||
| 190 | 'LEFT OUTER JOIN' => array( |
||
| 191 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 192 | 'field' => 'join', |
||
| 193 | ), |
||
| 194 | 'ON' => array( |
||
| 195 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Expression', |
||
| 196 | 'field' => 'table', |
||
| 197 | 'options' => array('parseField' => 'table'), |
||
| 198 | ), |
||
| 199 | 'RIGHT JOIN' => array( |
||
| 200 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 201 | 'field' => 'join', |
||
| 202 | ), |
||
| 203 | 'RIGHT OUTER JOIN' => array( |
||
| 204 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 205 | 'field' => 'join', |
||
| 206 | ), |
||
| 207 | 'INNER JOIN' => array( |
||
| 208 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 209 | 'field' => 'join', |
||
| 210 | ), |
||
| 211 | 'FULL JOIN' => array( |
||
| 212 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 213 | 'field' => 'join', |
||
| 214 | ), |
||
| 215 | 'FULL OUTER JOIN' => array( |
||
| 216 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 217 | 'field' => 'join', |
||
| 218 | ), |
||
| 219 | 'NATURAL JOIN' => array( |
||
| 220 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 221 | 'field' => 'join', |
||
| 222 | ), |
||
| 223 | 'NATURAL LEFT JOIN' => array( |
||
| 224 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 225 | 'field' => 'join', |
||
| 226 | ), |
||
| 227 | 'NATURAL RIGHT JOIN' => array( |
||
| 228 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 229 | 'field' => 'join', |
||
| 230 | ), |
||
| 231 | 'NATURAL LEFT OUTER JOIN' => array( |
||
| 232 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 233 | 'field' => 'join', |
||
| 234 | ), |
||
| 235 | 'NATURAL RIGHT OUTER JOIN' => array( |
||
| 236 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\JoinKeyword', |
||
| 237 | 'field' => 'join', |
||
| 238 | ), |
||
| 239 | 'LIMIT' => array( |
||
| 240 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Limit', |
||
| 241 | 'field' => 'limit', |
||
| 242 | ), |
||
| 243 | 'OPTIMIZE' => array( |
||
| 244 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 245 | 'field' => 'tables', |
||
| 246 | 'options' => array('parseField' => 'table'), |
||
| 247 | ), |
||
| 248 | 'ORDER BY' => array( |
||
| 249 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\OrderKeyword', |
||
| 250 | 'field' => 'order', |
||
| 251 | ), |
||
| 252 | 'PARTITION' => array( |
||
| 253 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ArrayObj', |
||
| 254 | 'field' => 'partition', |
||
| 255 | ), |
||
| 256 | 'PROCEDURE' => array( |
||
| 257 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\FunctionCall', |
||
| 258 | 'field' => 'procedure', |
||
| 259 | ), |
||
| 260 | 'RENAME' => array( |
||
| 261 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\RenameOperation', |
||
| 262 | 'field' => 'renames', |
||
| 263 | ), |
||
| 264 | 'REPAIR' => array( |
||
| 265 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 266 | 'field' => 'tables', |
||
| 267 | 'options' => array('parseField' => 'table'), |
||
| 268 | ), |
||
| 269 | 'RESTORE' => array( |
||
| 270 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 271 | 'field' => 'tables', |
||
| 272 | 'options' => array('parseField' => 'table'), |
||
| 273 | ), |
||
| 274 | 'SET' => array( |
||
| 275 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\SetOperation', |
||
| 276 | 'field' => 'set', |
||
| 277 | ), |
||
| 278 | 'SELECT' => array( |
||
| 279 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 280 | 'field' => 'expr', |
||
| 281 | ), |
||
| 282 | 'TRUNCATE' => array( |
||
| 283 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Expression', |
||
| 284 | 'field' => 'table', |
||
| 285 | 'options' => array('parseField' => 'table'), |
||
| 286 | ), |
||
| 287 | 'UPDATE' => array( |
||
| 288 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', |
||
| 289 | 'field' => 'tables', |
||
| 290 | 'options' => array('parseField' => 'table'), |
||
| 291 | ), |
||
| 292 | 'VALUE' => array( |
||
| 293 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Array2d', |
||
| 294 | 'field' => 'values', |
||
| 295 | ), |
||
| 296 | 'VALUES' => array( |
||
| 297 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Array2d', |
||
| 298 | 'field' => 'values', |
||
| 299 | ), |
||
| 300 | 'WHERE' => array( |
||
| 301 | 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Condition', |
||
| 302 | 'field' => 'where', |
||
| 303 | ), |
||
| 304 | ); |
||
| 305 | |||
| 306 | /** |
||
| 307 | * The list of tokens that are parsed. |
||
| 308 | * |
||
| 309 | * @var TokensList |
||
| 310 | */ |
||
| 311 | public $list; |
||
| 312 | |||
| 313 | /** |
||
| 314 | * List of statements parsed. |
||
| 315 | * |
||
| 316 | * @var Statement[] |
||
| 317 | */ |
||
| 318 | public $statements = array(); |
||
| 319 | |||
| 320 | /** |
||
| 321 | * The number of opened brackets. |
||
| 322 | * |
||
| 323 | * @var int |
||
| 324 | */ |
||
| 325 | public $brackets = 0; |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Constructor. |
||
| 329 | * |
||
| 330 | * @param string|UtfString|TokensList $list the list of tokens to be parsed |
||
|
|
|||
| 331 | * @param bool $strict whether strict mode should be enabled or not |
||
| 332 | */ |
||
| 333 | 309 | public function __construct($list = null, $strict = false) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Builds the parse trees. |
||
| 351 | */ |
||
| 352 | 247 | public function parse() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Creates a new error log. |
||
| 556 | * |
||
| 557 | * @param string $msg the error message |
||
| 558 | * @param Token $token the token that produced the error |
||
| 559 | * @param int $code the code of the error |
||
| 560 | * |
||
| 561 | * @throws ParserException throws the exception, if strict mode is enabled |
||
| 562 | */ |
||
| 563 | 73 | public function error($msg, Token $token = null, $code = 0) |
|
| 571 | } |
||
| 572 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.