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 Statement 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 Statement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class Statement |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Options for this statement. |
||
| 27 | * |
||
| 28 | * The option would be the key and the value can be an integer or an array. |
||
| 29 | * |
||
| 30 | * The integer represents only the index used. |
||
| 31 | * |
||
| 32 | * The array may have two keys: `0` is used to represent the index used and |
||
| 33 | * `1` is the type of the option (which may be 'var' or 'var='). Both |
||
| 34 | * options mean they expect a value after the option (e.g. `A = B` or `A B`, |
||
| 35 | * in which case `A` is the key and `B` is the value). The only difference |
||
| 36 | * is in the building process. `var` options are built as `A B` and `var=` |
||
| 37 | * options are built as `A = B` |
||
| 38 | * |
||
| 39 | * Two options that can be used together must have different values for |
||
| 40 | * indexes, else, when they will be used together, an error will occur. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | public static $OPTIONS = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The clauses of this statement, in order. |
||
| 48 | * |
||
| 49 | * The value attributed to each clause is used by the builder and it may |
||
| 50 | * have one of the following values: |
||
| 51 | * |
||
| 52 | * - 1 = 01 - add the clause only |
||
| 53 | * - 2 = 10 - add the keyword |
||
| 54 | * - 3 = 11 - add both the keyword and the clause |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | public static $CLAUSES = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The options of this query. |
||
| 62 | * |
||
| 63 | * @var OptionsArray |
||
| 64 | * |
||
| 65 | * @see static::$OPTIONS |
||
| 66 | */ |
||
| 67 | public $options; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The index of the first token used in this statement. |
||
| 71 | * |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | public $first; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The index of the last token used in this statement. |
||
| 78 | * |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | public $last; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Constructor. |
||
| 85 | * |
||
| 86 | * @param Parser $parser The instance that requests parsing. |
||
|
|
|||
| 87 | * @param TokensList $list The list of tokens to be parsed. |
||
| 88 | */ |
||
| 89 | public function __construct(Parser $parser = null, TokensList $list = null) |
||
| 95 | 139 | ||
| 96 | /** |
||
| 97 | * Builds the string representation of this statement. |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function build() |
||
| 102 | 7 | { |
|
| 103 | /** |
||
| 104 | * Query to be returned. |
||
| 105 | * |
||
| 106 | * @var string $query |
||
| 107 | */ |
||
| 108 | $query = ''; |
||
| 109 | 7 | ||
| 110 | /** |
||
| 111 | * Clauses which were built already. |
||
| 112 | * |
||
| 113 | * It is required to keep track of built clauses because some fields, |
||
| 114 | * for example `join` is used by multiple clauses (`JOIN`, `LEFT JOIN`, |
||
| 115 | * `LEFT OUTER JOIN`, etc.). The same happens for `VALUE` and `VALUES`. |
||
| 116 | * |
||
| 117 | * A clause is considered built just after fields' value |
||
| 118 | * (`$this->field`) was used in building. |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | $built = array(); |
||
| 123 | 7 | ||
| 124 | /** |
||
| 125 | * Statement's clauses. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | $clauses = $this->getClauses(); |
||
| 130 | 7 | ||
| 131 | foreach ($clauses as $clause) { |
||
| 132 | 7 | /** |
|
| 133 | * The name of the clause. |
||
| 134 | * |
||
| 135 | * @var string $name |
||
| 136 | */ |
||
| 137 | $name = $clause[0]; |
||
| 138 | 6 | ||
| 139 | /** |
||
| 140 | * The type of the clause. |
||
| 141 | * |
||
| 142 | * @see self::$CLAUSES |
||
| 143 | * @var int $type |
||
| 144 | */ |
||
| 145 | $type = $clause[1]; |
||
| 146 | 6 | ||
| 147 | /** |
||
| 148 | * The builder (parser) of this clause. |
||
| 149 | * |
||
| 150 | * @var Component $class |
||
| 151 | */ |
||
| 152 | $class = Parser::$KEYWORD_PARSERS[$name]['class']; |
||
| 153 | 6 | ||
| 154 | /** |
||
| 155 | * The name of the field that is used as source for the builder. |
||
| 156 | * Same field is used to store the result of parsing. |
||
| 157 | * |
||
| 158 | * @var string $field |
||
| 159 | */ |
||
| 160 | $field = Parser::$KEYWORD_PARSERS[$name]['field']; |
||
| 161 | 6 | ||
| 162 | // The field is empty, there is nothing to be built. |
||
| 163 | if (empty($this->$field)) { |
||
| 164 | 6 | continue; |
|
| 165 | 6 | } |
|
| 166 | |||
| 167 | // Checking if this field was already built. |
||
| 168 | if ($type & 1) { |
||
| 169 | 6 | if (!empty($built[$field])) { |
|
| 170 | 6 | continue; |
|
| 171 | 2 | } |
|
| 172 | $built[$field] = true; |
||
| 173 | 6 | } |
|
| 174 | 6 | ||
| 175 | // Checking if the name of the clause should be added. |
||
| 176 | if ($type & 2) { |
||
| 177 | 6 | $query .= $name . ' '; |
|
| 178 | 6 | } |
|
| 179 | 6 | ||
| 180 | // Checking if the result of the builder should be added. |
||
| 181 | if ($type & 1) { |
||
| 182 | 6 | $query .= $class::build($this->$field) . ' '; |
|
| 183 | 6 | } |
|
| 184 | 6 | } |
|
| 185 | 7 | ||
| 186 | return $query; |
||
| 187 | 7 | } |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Parses the statements defined by the tokens list. |
||
| 191 | * |
||
| 192 | * @param Parser $parser The instance that requests parsing. |
||
| 193 | * @param TokensList $list The list of tokens to be parsed. |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | public function parse(Parser $parser, TokensList $list) |
||
| 375 | |||
| 376 | /** |
||
| 377 | 85 | * Function called before the token is processed. |
|
| 378 | * |
||
| 379 | * @param Parser $parser The instance that requests parsing. |
||
| 380 | * @param TokensList $list The list of tokens to be parsed. |
||
| 381 | * @param Token $token The token that is being parsed. |
||
| 382 | * |
||
| 383 | * @return void |
||
| 384 | 2 | */ |
|
| 385 | public function before(Parser $parser, TokensList $list, Token $token) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Function called after the token was processed. |
||
| 392 | * |
||
| 393 | * @param Parser $parser The instance that requests parsing. |
||
| 394 | * @param TokensList $list The list of tokens to be parsed. |
||
| 395 | * @param Token $token The token that is being parsed. |
||
| 396 | 2 | * |
|
| 397 | * @return void |
||
| 398 | 2 | */ |
|
| 399 | public function after(Parser $parser, TokensList $list, Token $token) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Gets the clauses of this statement. |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public function getClauses() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Builds the string representation of this statement. |
||
| 416 | * |
||
| 417 | * @see static::build |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function __toString() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Validates the order of the clauses in parsed statement |
||
| 428 | * Ideally this should be called after successfully |
||
| 429 | * completing the parsing of each statement |
||
| 430 | * |
||
| 431 | * @param Parser $parser The instance that requests parsing. |
||
| 432 | * @param TokensList $list The list of tokens to be parsed. |
||
| 433 | * |
||
| 434 | * @return boolean |
||
| 435 | */ |
||
| 436 | public function validateClauseOrder($parser, $list) |
||
| 468 | } |
||
| 469 |
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.