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 |
||
30 | class CreateDefinition extends Component |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * All field options. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | public static $FIELD_OPTIONS = array( |
||
39 | |||
40 | // Tells the `OptionsArray` to not sort the options. |
||
41 | // See the note below. |
||
42 | '_UNSORTED' => true, |
||
43 | |||
44 | 'NOT NULL' => 1, |
||
45 | 'NULL' => 1, |
||
46 | 'DEFAULT' => array(2, 'expr', array('breakOnAlias' => true)), |
||
47 | 'AUTO_INCREMENT' => 3, |
||
48 | 'PRIMARY' => 4, |
||
49 | 'PRIMARY KEY' => 4, |
||
50 | 'UNIQUE' => 4, |
||
51 | 'UNIQUE KEY' => 4, |
||
52 | 'COMMENT' => array(5, 'var'), |
||
53 | 'COLUMN_FORMAT' => array(6, 'var'), |
||
54 | 'ON UPDATE' => array(7, 'var'), |
||
55 | |||
56 | // Generated columns options. |
||
57 | 'GENERATED ALWAYS' => 8, |
||
58 | 'AS' => array(9, 'expr', array('parenthesesDelimited' => true)), |
||
59 | 'VIRTUAL' => 10, |
||
60 | 'PERSISTENT' => 11, |
||
61 | 'STORED' => 11, |
||
62 | // Common entries. |
||
63 | // |
||
64 | // NOTE: Some of the common options are not in the same order which |
||
65 | // causes troubles when checking if the options are in the right order. |
||
66 | // I should find a way to define multiple sets of options and make the |
||
67 | // parser select the right set. |
||
68 | // |
||
69 | // 'UNIQUE' => 4, |
||
70 | // 'UNIQUE KEY' => 4, |
||
71 | // 'COMMENT' => array(5, 'var'), |
||
72 | // 'NOT NULL' => 1, |
||
73 | // 'NULL' => 1, |
||
74 | // 'PRIMARY' => 4, |
||
75 | // 'PRIMARY KEY' => 4, |
||
76 | ); |
||
77 | |||
78 | /** |
||
79 | * The name of the new column. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | public $name; |
||
84 | |||
85 | /** |
||
86 | * Whether this field is a constraint or not. |
||
87 | * |
||
88 | * @var bool |
||
89 | */ |
||
90 | public $isConstraint; |
||
91 | |||
92 | /** |
||
93 | * The data type of thew new column. |
||
94 | * |
||
95 | * @var DataType |
||
96 | */ |
||
97 | public $type; |
||
98 | |||
99 | /** |
||
100 | * The key. |
||
101 | * |
||
102 | * @var Key |
||
103 | */ |
||
104 | public $key; |
||
105 | |||
106 | /** |
||
107 | * The table that is referenced. |
||
108 | * |
||
109 | * @var Reference |
||
110 | */ |
||
111 | public $references; |
||
112 | |||
113 | /** |
||
114 | * The options of this field. |
||
115 | * |
||
116 | * @var OptionsArray |
||
117 | */ |
||
118 | public $options; |
||
119 | |||
120 | /** |
||
121 | * Constructor. |
||
122 | * |
||
123 | * @param string $name The name of the field. |
||
|
|||
124 | * @param OptionsArray $options The options of this field. |
||
125 | * @param DataType|Key $type The data type of this field or the key. |
||
126 | * @param bool $isConstraint Whether this field is a constraint or not. |
||
127 | * @param Reference $references References. |
||
128 | */ |
||
129 | 23 | public function __construct( |
|
146 | |||
147 | /** |
||
148 | * @param Parser $parser The parser that serves as context. |
||
149 | * @param TokensList $list The list of tokens that are being parsed. |
||
150 | * @param array $options Parameters for parsing. |
||
151 | * |
||
152 | * @return CreateDefinition[] |
||
153 | */ |
||
154 | 23 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
281 | |||
282 | /** |
||
283 | * @param CreateDefinition|CreateDefinition[] $component The component to be built. |
||
284 | * @param array $options Parameters for building. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 6 | public static function build($component, array $options = array()) |
|
323 | } |
||
324 |
This check looks for
@param
annotations 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.