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 |
||
| 21 | class IntoKeyword extends Component |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * FIELDS/COLUMNS Options for `SELECT...INTO` statements. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public static $FIELDS_OPTIONS = array( |
||
| 29 | 'TERMINATED BY' => array( |
||
| 30 | 1, |
||
| 31 | 'expr', |
||
| 32 | ), |
||
| 33 | 'OPTIONALLY' => 2, |
||
| 34 | 'ENCLOSED BY' => array( |
||
| 35 | 3, |
||
| 36 | 'expr', |
||
| 37 | ), |
||
| 38 | 'ESCAPED BY' => array( |
||
| 39 | 4, |
||
| 40 | 'expr', |
||
| 41 | ) |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * LINES Options for `SELECT...INTO` statements. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | public static $LINES_OPTIONS = array( |
||
| 50 | 'STARTING BY' => array( |
||
| 51 | 1, |
||
| 52 | 'expr', |
||
| 53 | ), |
||
| 54 | 'TERMINATED BY' => array( |
||
| 55 | 2, |
||
| 56 | 'expr', |
||
| 57 | ) |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Type of target (OUTFILE or SYMBOL). |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | public $type; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The destination, which can be a table or a file. |
||
| 69 | * |
||
| 70 | * @var string|Expression |
||
| 71 | */ |
||
| 72 | public $dest; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The name of the columns. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | public $columns; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The values to be selected into (SELECT .. INTO @var1). |
||
| 83 | * |
||
| 84 | * @var Expression[] |
||
| 85 | */ |
||
| 86 | public $values; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Options for FIELDS/COLUMNS keyword. |
||
| 90 | * |
||
| 91 | * @var OptionsArray |
||
| 92 | * |
||
| 93 | * @see static::$FIELDS_OPTIONS |
||
| 94 | */ |
||
| 95 | public $fields_options; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Whether to use `FIELDS` or `COLUMNS` while building. |
||
| 99 | * |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | public $fields_keyword; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Options for OPTIONS keyword. |
||
| 106 | * |
||
| 107 | * @var OptionsArray |
||
| 108 | * |
||
| 109 | * @see static::$LINES_OPTIONS |
||
| 110 | */ |
||
| 111 | public $lines_options; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Constructor. |
||
| 115 | * |
||
| 116 | * @param string $type type of destination (may be OUTFILE) |
||
|
|
|||
| 117 | * @param string|Expression $dest actual destination |
||
| 118 | * @param array $columns column list of destination |
||
| 119 | * @param array $values selected fields |
||
| 120 | * @param OptionsArray $fields_options options for FIELDS/COLUMNS keyword |
||
| 121 | * @param bool $fields_keyword options for OPTIONS keyword |
||
| 122 | */ |
||
| 123 | 39 | public function __construct( |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param Parser $parser the parser that serves as context |
||
| 141 | * @param TokensList $list the list of tokens that are being parsed |
||
| 142 | * @param array $options parameters for parsing |
||
| 143 | * |
||
| 144 | * @return IntoKeyword |
||
| 145 | */ |
||
| 146 | 39 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
| 242 | |||
| 243 | 4 | View Code Duplication | public function parseFileOptions(Parser $parser, TokensList $list, $keyword = 'FIELDS') |
| 244 | { |
||
| 245 | 4 | ++$list->idx; |
|
| 246 | |||
| 247 | 4 | if ($keyword === 'FIELDS' || $keyword === 'COLUMNS') { |
|
| 248 | // parse field options |
||
| 249 | 4 | $this->fields_options = OptionsArray::parse( |
|
| 250 | 4 | $parser, |
|
| 251 | $list, |
||
| 252 | static::$FIELDS_OPTIONS |
||
| 253 | ); |
||
| 254 | |||
| 255 | 4 | $this->fields_keyword = ($keyword === 'FIELDS'); |
|
| 256 | } else { |
||
| 257 | // parse line options |
||
| 258 | 3 | $this->lines_options = OptionsArray::parse( |
|
| 259 | 3 | $parser, |
|
| 260 | $list, |
||
| 261 | static::$LINES_OPTIONS |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | 4 | } |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param IntoKeyword $component the component to be built |
||
| 268 | * @param array $options parameters for building |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | 9 | public static function build($component, array $options = array()) |
|
| 297 | } |
||
| 298 |
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.