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 | /** |
||
25 | * The maximum length of a keyword. |
||
26 | * |
||
27 | * @see static::$TOKEN_KEYWORD |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | const KEYWORD_MAX_LENGTH = 30; |
||
32 | |||
33 | /** |
||
34 | * The maximum length of an operator. |
||
35 | * |
||
36 | * @see static::$TOKEN_OPERATOR |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | const OPERATOR_MAX_LENGTH = 4; |
||
41 | |||
42 | /** |
||
43 | * The name of the default content. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | public static $defaultContext = '\\SqlParser\\Contexts\\ContextMySql50700'; |
||
48 | |||
49 | /** |
||
50 | * The name of the loaded context. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | public static $loadedContext = '\\SqlParser\\Contexts\\ContextMySql50700'; |
||
55 | |||
56 | /** |
||
57 | * The prefix concatenated to the context name when an incomplete class name |
||
58 | * is specified. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | public static $contextPrefix = '\\SqlParser\\Contexts\\Context'; |
||
63 | |||
64 | /** |
||
65 | * List of keywords. |
||
66 | * |
||
67 | * Because, PHP's associative arrays are basically hash tables, it is more |
||
68 | * efficient to store keywords as keys instead of values. |
||
69 | * |
||
70 | * The value associated to each keyword represents its flags. |
||
71 | * |
||
72 | * @see Token::FLAG_KEYWORD_* |
||
73 | * |
||
74 | * Elements are sorted by flags, length and keyword. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | public static $KEYWORDS = array(); |
||
79 | |||
80 | /** |
||
81 | * List of operators and their flags. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | public static $OPERATORS = array( |
||
86 | |||
87 | // Some operators (*, =) may have ambiguous flags, because they depend on |
||
88 | // the context they are being used in. |
||
89 | // For example: 1. SELECT * FROM table; # SQL specific (wildcard) |
||
90 | // SELECT 2 * 3; # arithmetic |
||
1 ignored issue
–
show
|
|||
91 | // 2. SELECT * FROM table WHERE foo = 'bar'; |
||
92 | // SET @i = 0; |
||
93 | |||
94 | // @see Token::FLAG_OPERATOR_ARITHMETIC |
||
95 | '%' => 1, '*' => 1, '+' => 1, '-' => 1, '/' => 1, |
||
96 | |||
97 | // @see Token::FLAG_OPERATOR_LOGICAL |
||
98 | '!' => 2, '!=' => 2, '&&' => 2, '<' => 2, '<=' => 2, |
||
99 | '<=>' => 2, '<>' => 2, '=' => 2, '>' => 2, '>=' => 2, |
||
100 | '||' => 2, |
||
101 | |||
102 | // @see Token::FLAG_OPERATOR_BITWISE |
||
103 | '&' => 4, '<<' => 4, '>>' => 4, '^' => 4, '|' => 4, |
||
104 | '~' => 4, |
||
105 | |||
106 | // @see Token::FLAG_OPERATOR_ASSIGNMENT |
||
107 | ':=' => 8, |
||
108 | |||
109 | // @see Token::FLAG_OPERATOR_SQL |
||
110 | '(' => 16, ')' => 16, '.' => 16, ',' => 16, ';' => 16, |
||
111 | ); |
||
112 | |||
113 | /** |
||
114 | * The mode of the MySQL server that will be used in lexing, parsing and |
||
115 | * building the statements. |
||
116 | * |
||
117 | * @var int |
||
118 | */ |
||
119 | public static $MODE = 0; |
||
120 | |||
121 | /* |
||
122 | * Server SQL Modes |
||
123 | * https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html |
||
124 | */ |
||
125 | |||
126 | // Compatibility mode for Microsoft's SQL server. |
||
127 | // This is the equivalent of ANSI_QUOTES. |
||
128 | const COMPAT_MYSQL = 2; |
||
129 | |||
130 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_allow_invalid_dates |
||
131 | const ALLOW_INVALID_DATES = 1; |
||
132 | |||
133 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ansi_quotes |
||
134 | const ANSI_QUOTES = 2; |
||
135 | |||
136 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_error_for_division_by_zero |
||
137 | const ERROR_FOR_DIVISION_BY_ZERO = 4; |
||
138 | |||
139 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_high_not_precedence |
||
140 | const HIGH_NOT_PRECEDENCE = 8; |
||
141 | |||
142 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ignore_space |
||
143 | const IGNORE_SPACE = 16; |
||
144 | |||
145 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_create_user |
||
146 | const NO_AUTO_CREATE_USER = 32; |
||
147 | |||
148 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_value_on_zero |
||
149 | const NO_AUTO_VALUE_ON_ZERO = 64; |
||
150 | |||
151 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_backslash_escapes |
||
152 | const NO_BACKSLASH_ESCAPES = 128; |
||
153 | |||
154 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create |
||
155 | const NO_DIR_IN_CREATE = 256; |
||
156 | |||
157 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create |
||
158 | const NO_ENGINE_SUBSTITUTION = 512; |
||
159 | |||
160 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_field_options |
||
161 | const NO_FIELD_OPTIONS = 1024; |
||
162 | |||
163 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_key_options |
||
164 | const NO_KEY_OPTIONS = 2048; |
||
165 | |||
166 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_table_options |
||
167 | const NO_TABLE_OPTIONS = 4096; |
||
168 | |||
169 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_unsigned_subtraction |
||
170 | const NO_UNSIGNED_SUBTRACTION = 8192; |
||
171 | |||
172 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_date |
||
173 | const NO_ZERO_DATE = 16384; |
||
174 | |||
175 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_in_date |
||
176 | const NO_ZERO_IN_DATE = 32768; |
||
177 | |||
178 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_only_full_group_by |
||
179 | const ONLY_FULL_GROUP_BY = 65536; |
||
180 | |||
181 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_pipes_as_concat |
||
182 | const PIPES_AS_CONCAT = 131072; |
||
183 | |||
184 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_real_as_float |
||
185 | const REAL_AS_FLOAT = 262144; |
||
186 | |||
187 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_all_tables |
||
188 | const STRICT_ALL_TABLES = 524288; |
||
189 | |||
190 | // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_trans_tables |
||
191 | const STRICT_TRANS_TABLES = 1048576; |
||
192 | |||
193 | // Custom modes. |
||
194 | |||
195 | // The table and column names and any other field that must be escaped will |
||
196 | // not be. |
||
197 | // Reserved keywords are being escaped regardless this mode is used or not. |
||
198 | const NO_ENCLOSING_QUOTES = 1073741824; |
||
199 | |||
200 | /* |
||
201 | * Combination SQL Modes |
||
202 | * https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sql-mode-combo |
||
203 | */ |
||
204 | |||
205 | // REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE |
||
206 | const SQL_MODE_ANSI = 393234; |
||
207 | |||
208 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
209 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, |
||
210 | const SQL_MODE_DB2 = 138258; |
||
211 | |||
212 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
213 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER |
||
214 | const SQL_MODE_MAXDB = 138290; |
||
215 | |||
216 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
217 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS |
||
218 | const SQL_MODE_MSSQL = 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_ORACLE = 138290; |
||
223 | |||
224 | // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, |
||
225 | // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS |
||
226 | const SQL_MODE_POSTGRESQL = 138258; |
||
227 | |||
228 | // STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, |
||
229 | // ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER |
||
230 | const SQL_MODE_TRADITIONAL = 1622052; |
||
231 | |||
232 | // ------------------------------------------------------------------------- |
||
233 | // Keyword. |
||
234 | |||
235 | /** |
||
236 | * Checks if the given string is a keyword. |
||
237 | * |
||
238 | * @param string $str String to be checked. |
||
239 | * @param bool $isReserved Checks if the keyword is reserved. |
||
240 | * |
||
241 | * @return int |
||
242 | */ |
||
243 | public static function isKeyword($str, $isReserved = false) |
||
258 | |||
259 | // ------------------------------------------------------------------------- |
||
260 | // Operator. |
||
261 | |||
262 | /** |
||
263 | * Checks if the given string is an operator. |
||
264 | * |
||
265 | * @param string $str String to be checked. |
||
266 | * |
||
267 | * @return int The appropriate flag for the operator. |
||
268 | */ |
||
269 | public static function isOperator($str) |
||
276 | |||
277 | // ------------------------------------------------------------------------- |
||
278 | // Whitespace. |
||
279 | |||
280 | /** |
||
281 | * Checks if the given character is a whitespace. |
||
282 | * |
||
283 | * @param string $str String to be checked. |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | public static function isWhitespace($str) |
||
291 | |||
292 | // ------------------------------------------------------------------------- |
||
293 | // Comment. |
||
294 | |||
295 | /** |
||
296 | * Checks if the given string is the beginning of a whitespace. |
||
297 | * |
||
298 | * @param string $str String to be checked. |
||
299 | * |
||
300 | * @return int The appropriate flag for the comment type. |
||
301 | */ |
||
302 | public static function isComment($str) |
||
319 | |||
320 | // ------------------------------------------------------------------------- |
||
321 | // Bool. |
||
322 | |||
323 | /** |
||
324 | * Checks if the given string is a boolean value. |
||
325 | * This actually check only for `TRUE` and `FALSE` because `1` or `0` are |
||
326 | * actually numbers and are parsed by specific methods. |
||
327 | * |
||
328 | * @param string $str String to be checked. |
||
329 | * |
||
330 | * @return bool |
||
331 | */ |
||
332 | public static function isBool($str) |
||
337 | |||
338 | // ------------------------------------------------------------------------- |
||
339 | // Number. |
||
340 | |||
341 | /** |
||
342 | * Checks if the given character can be a part of a number. |
||
343 | * |
||
344 | * @param string $str String to be checked. |
||
345 | * |
||
346 | * @return bool |
||
347 | */ |
||
348 | public static function isNumber($str) |
||
353 | |||
354 | // ------------------------------------------------------------------------- |
||
355 | // Symbol. |
||
356 | |||
357 | /** |
||
358 | * Checks if the given character is the beginning of a symbol. A symbol |
||
359 | * can be either a variable or a field name. |
||
360 | * |
||
361 | * @param string $str String to be checked. |
||
362 | * |
||
363 | * @return int The appropriate flag for the symbol type. |
||
364 | */ |
||
365 | View Code Duplication | public static function isSymbol($str) |
|
374 | |||
375 | // ------------------------------------------------------------------------- |
||
376 | // String. |
||
377 | |||
378 | /** |
||
379 | * Checks if the given character is the beginning of a string. |
||
380 | * |
||
381 | * @param string $str String to be checked. |
||
382 | * |
||
383 | * @return int The appropriate flag for the string type. |
||
384 | */ |
||
385 | View Code Duplication | public static function isString($str) |
|
394 | |||
395 | // ------------------------------------------------------------------------- |
||
396 | // Delimiter. |
||
397 | |||
398 | /** |
||
399 | * Checks if the given character can be a separator for two lexeme. |
||
400 | * |
||
401 | * @param string $str String to be checked. |
||
402 | * |
||
403 | * @return bool |
||
404 | */ |
||
405 | public static function isSeparator($str) |
||
414 | |||
415 | /** |
||
416 | * Loads the specified context. |
||
417 | * |
||
418 | * Contexts may be used by accessing the context directly. |
||
419 | * |
||
420 | * @param string $context Name of the context or full class name that |
||
421 | * defines the context. |
||
422 | * |
||
423 | * @throws \Exception If the specified context doesn't exist. |
||
424 | * |
||
425 | * @return void |
||
426 | */ |
||
427 | public static function load($context = '') |
||
444 | |||
445 | /** |
||
446 | * Loads the context with the closest version to the one specified. |
||
447 | * |
||
448 | * The closest context is found by replacing last digits with zero until one |
||
449 | * is loaded successfully. |
||
450 | * |
||
451 | * @see Context::load() |
||
452 | * |
||
453 | * @param string $context Name of the context or full class name that |
||
454 | * defines the context. |
||
455 | * |
||
456 | * @return string The loaded context. `null` if no context was loaded. |
||
457 | */ |
||
458 | public static function loadClosest($context = '') |
||
493 | |||
494 | /** |
||
495 | * Sets the SQL mode. |
||
496 | * |
||
497 | * @param string $mode The list of modes. If empty, the mode is reset. |
||
498 | * |
||
499 | * @return void |
||
500 | */ |
||
501 | public static function setMode($mode = '') |
||
512 | |||
513 | /** |
||
514 | * Escapes the symbol by adding surrounding backticks. |
||
515 | * |
||
516 | * @param array|string $str The string to be escaped. |
||
517 | * @param string $quote Quote to be used when escaping. |
||
518 | * |
||
519 | * @return string |
||
520 | */ |
||
521 | public static function escape($str, $quote = '`') |
||
542 | } |
||
543 | |||
546 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.