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 | 'AUTO_INCREMENT' => 3, |
||
| 42 | 'PRIMARY' => 4, |
||
| 43 | 'PRIMARY KEY' => 4, |
||
| 44 | 'UNIQUE' => 4, |
||
| 45 | 'UNIQUE KEY' => 4, |
||
| 46 | 'COMMENT' => array(5, 'var'), |
||
| 47 | 'COLUMN_FORMAT' => array(6, 'var'), |
||
| 48 | 'ON UPDATE' => array(7, 'expr'), |
||
| 49 | |||
| 50 | // Generated columns options. |
||
| 51 | 'GENERATED ALWAYS' => 8, |
||
| 52 | 'AS' => array(9, 'expr', array('parenthesesDelimited' => true)), |
||
| 53 | 'VIRTUAL' => 10, |
||
| 54 | 'PERSISTENT' => 11, |
||
| 55 | 'STORED' => 11, |
||
| 56 | // Common entries. |
||
| 57 | // |
||
| 58 | // NOTE: Some of the common options are not in the same order which |
||
| 59 | // causes troubles when checking if the options are in the right order. |
||
| 60 | // I should find a way to define multiple sets of options and make the |
||
| 61 | // parser select the right set. |
||
| 62 | // |
||
| 63 | // 'UNIQUE' => 4, |
||
| 64 | // 'UNIQUE KEY' => 4, |
||
| 65 | // 'COMMENT' => array(5, 'var'), |
||
| 66 | // 'NOT NULL' => 1, |
||
| 67 | // 'NULL' => 1, |
||
| 68 | // 'PRIMARY' => 4, |
||
| 69 | // 'PRIMARY KEY' => 4, |
||
| 70 | ); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The name of the new column. |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | public $name; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Whether this field is a constraint or not. |
||
| 81 | * |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | public $isConstraint; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The data type of thew new column. |
||
| 88 | * |
||
| 89 | * @var DataType |
||
| 90 | */ |
||
| 91 | public $type; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The key. |
||
| 95 | * |
||
| 96 | * @var Key |
||
| 97 | */ |
||
| 98 | public $key; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The table that is referenced. |
||
| 102 | * |
||
| 103 | * @var Reference |
||
| 104 | */ |
||
| 105 | public $references; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The options of this field. |
||
| 109 | * |
||
| 110 | * @var OptionsArray |
||
| 111 | */ |
||
| 112 | public $options; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Constructor. |
||
| 116 | * |
||
| 117 | * @param string $name the name of the field |
||
|
|
|||
| 118 | * @param OptionsArray $options the options of this field |
||
| 119 | * @param DataType|Key $type the data type of this field or the key |
||
| 120 | * @param bool $isConstraint whether this field is a constraint or not |
||
| 121 | * @param Reference $references references |
||
| 122 | */ |
||
| 123 | 29 | public function __construct( |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @param Parser $parser the parser that serves as context |
||
| 143 | * @param TokensList $list the list of tokens that are being parsed |
||
| 144 | * @param array $options parameters for parsing |
||
| 145 | * |
||
| 146 | * @return CreateDefinition[] |
||
| 147 | */ |
||
| 148 | 29 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param CreateDefinition|CreateDefinition[] $component the component to be built |
||
| 297 | * @param array $options parameters for building |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | 6 | public static function build($component, array $options = array()) |
|
| 336 | } |
||
| 337 |
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.