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 LoadStatement 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 LoadStatement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class LoadStatement extends Statement |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * Options for `LOAD` statements and their slot ID. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | public static $OPTIONS = array( |
||
| 59 | 'LOW_PRIORITY' => 1, |
||
| 60 | 'CONCURRENT' => 1, |
||
| 61 | 'LOCAL' => 2 |
||
| 62 | ); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * FIELDS/COLUMNS Options for `LOAD DATA...INFILE` statements. |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | public static $FIELDS_OPTIONS = array( |
||
| 70 | 'TERMINATED BY' => array(1, 'expr'), |
||
| 71 | 'OPTIONALLY' => 2, |
||
| 72 | 'ENCLOSED BY' => array(3, 'expr'), |
||
| 73 | 'ESCAPED BY' => array(4, 'expr'), |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * LINES Options for `LOAD DATA...INFILE` statements. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | public static $LINES_OPTIONS = array( |
||
| 82 | 'STARTING BY' => array(1, 'expr'), |
||
| 83 | 'TERMINATED BY' => array(2, 'expr'), |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * File name being used to load data |
||
| 88 | * |
||
| 89 | * @var Expression |
||
| 90 | */ |
||
| 91 | public $file_name; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Table used as destination for this statement. |
||
| 95 | * |
||
| 96 | * @var Expression |
||
| 97 | */ |
||
| 98 | public $table; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Partitions used as source for this statement. |
||
| 102 | * |
||
| 103 | * @var ArrayObj |
||
| 104 | */ |
||
| 105 | public $partition; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Character set used in this statement. |
||
| 109 | * |
||
| 110 | * @var Expression |
||
| 111 | */ |
||
| 112 | public $charset_name; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Options for FIELDS/COLUMNS keyword. |
||
| 116 | * |
||
| 117 | * @var OptionsArray |
||
| 118 | * |
||
| 119 | * @see static::$FIELDS_OPTIONS |
||
| 120 | */ |
||
| 121 | public $fields_options; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Whether to use `FIELDS` or `COLUMNS` while building. |
||
| 125 | * |
||
| 126 | * @var bool |
||
| 127 | */ |
||
| 128 | public $fields_keyword; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Options for OPTIONS keyword. |
||
| 132 | * |
||
| 133 | * @var OptionsArray |
||
| 134 | * |
||
| 135 | * @see static::$LINES_OPTIONS |
||
| 136 | */ |
||
| 137 | public $lines_options; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Column names or user variables |
||
| 141 | * |
||
| 142 | * @var ExpressionArray |
||
| 143 | */ |
||
| 144 | public $col_name_or_user_var; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * SET clause's updated values(optional) |
||
| 148 | * |
||
| 149 | * @var SetOperation[] |
||
| 150 | */ |
||
| 151 | public $set; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Ignore 'number' LINES/ROWS |
||
| 155 | * |
||
| 156 | * @var Expression |
||
| 157 | */ |
||
| 158 | public $ignore_number; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * REPLACE/IGNORE Keyword |
||
| 162 | * |
||
| 163 | * @var string |
||
| 164 | */ |
||
| 165 | public $replace_ignore; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * LINES/ROWS Keyword |
||
| 169 | * |
||
| 170 | * @var string |
||
| 171 | */ |
||
| 172 | public $lines_rows; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 1 | public function build() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param Parser $parser the instance that requests parsing |
||
| 221 | * @param TokensList $list the list of tokens to be parsed |
||
| 222 | */ |
||
| 223 | 15 | public function parse(Parser $parser, TokensList $list) |
|
| 324 | |||
| 325 | 6 | public function parseFileOptions(Parser $parser, TokensList $list, $keyword = 'FIELDS') |
|
| 347 | |||
| 348 | |||
| 349 | 8 | public function parseKeywordsAccordingToState($parser, $list, $state) { |
|
| 403 | |||
| 404 | } |
||
| 405 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: