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 CreateDefinition 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 CreateDefinition, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class CreateDefinition extends Component |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * All field options. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public static $FIELD_OPTIONS = array( |
||
| 34 | // Tells the `OptionsArray` to not sort the options. |
||
| 35 | // See the note below. |
||
| 36 | '_UNSORTED' => true, |
||
| 37 | |||
| 38 | 'NOT NULL' => 1, |
||
| 39 | 'NULL' => 1, |
||
| 40 | 'DEFAULT' => array(2, 'expr', array('breakOnAlias' => true)), |
||
| 41 | /* Following are not according to grammar, but MySQL happily accepts |
||
| 42 | * these at any location */ |
||
| 43 | 'CHARSET' => array(2, 'var'), |
||
| 44 | 'COLLATE' => array(3, 'var'), |
||
| 45 | 'AUTO_INCREMENT' => 3, |
||
| 46 | 'PRIMARY' => 4, |
||
| 47 | 'PRIMARY KEY' => 4, |
||
| 48 | 'UNIQUE' => 4, |
||
| 49 | 'UNIQUE KEY' => 4, |
||
| 50 | 'COMMENT' => array(5, 'var'), |
||
| 51 | 'COLUMN_FORMAT' => array(6, 'var'), |
||
| 52 | 'ON UPDATE' => array(7, 'expr'), |
||
| 53 | |||
| 54 | // Generated columns options. |
||
| 55 | 'GENERATED ALWAYS' => 8, |
||
| 56 | 'AS' => array(9, 'expr', array('parenthesesDelimited' => true)), |
||
| 57 | 'VIRTUAL' => 10, |
||
| 58 | 'PERSISTENT' => 11, |
||
| 59 | 'STORED' => 11, |
||
| 60 | // Common entries. |
||
| 61 | // |
||
| 62 | // NOTE: Some of the common options are not in the same order which |
||
| 63 | // causes troubles when checking if the options are in the right order. |
||
| 64 | // I should find a way to define multiple sets of options and make the |
||
| 65 | // parser select the right set. |
||
| 66 | // |
||
| 67 | // 'UNIQUE' => 4, |
||
| 68 | // 'UNIQUE KEY' => 4, |
||
| 69 | // 'COMMENT' => array(5, 'var'), |
||
| 70 | // 'NOT NULL' => 1, |
||
| 71 | // 'NULL' => 1, |
||
| 72 | // 'PRIMARY' => 4, |
||
| 73 | // 'PRIMARY KEY' => 4, |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The name of the new column. |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | public $name; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Whether this field is a constraint or not. |
||
| 85 | * |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | public $isConstraint; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The data type of thew new column. |
||
| 92 | * |
||
| 93 | * @var DataType |
||
| 94 | */ |
||
| 95 | public $type; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The key. |
||
| 99 | * |
||
| 100 | * @var Key |
||
| 101 | */ |
||
| 102 | public $key; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Check constraint |
||
| 106 | * |
||
| 107 | * @var check |
||
| 108 | */ |
||
| 109 | public $check; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The table that is referenced. |
||
| 113 | * |
||
| 114 | * @var Reference |
||
| 115 | */ |
||
| 116 | public $references; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The options of this field. |
||
| 120 | * |
||
| 121 | * @var OptionsArray |
||
| 122 | */ |
||
| 123 | public $options; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Constructor. |
||
| 127 | * |
||
| 128 | * @param string $name the name of the field |
||
|
|
|||
| 129 | * @param OptionsArray $options the options of this field |
||
| 130 | * @param DataType|Key $type the data type of this field or the key |
||
| 131 | * @param bool $isConstraint whether this field is a constraint or not |
||
| 132 | * @param Reference $references references |
||
| 133 | */ |
||
| 134 | 32 | public function __construct( |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param Parser $parser the parser that serves as context |
||
| 154 | * @param TokensList $list the list of tokens that are being parsed |
||
| 155 | * @param array $options parameters for parsing |
||
| 156 | * |
||
| 157 | * @return CreateDefinition[] |
||
| 158 | */ |
||
| 159 | 32 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param CreateDefinition|CreateDefinition[] $component the component to be built |
||
| 317 | * @param array $options parameters for building |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | 8 | public static function build($component, array $options = array()) |
|
| 360 | } |
||
| 361 |
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.