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 PostgresColumn 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 PostgresColumn, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class PostgresColumn extends AbstractColumn |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * {@inheritdoc} |
||
| 17 | */ |
||
| 18 | protected $mapping = [ |
||
| 19 | //Primary sequences |
||
| 20 | 'primary' => ['type' => 'serial', 'autoIncrement' => true, 'nullable' => false], |
||
| 21 | 'bigPrimary' => ['type' => 'bigserial', 'autoIncrement' => true, 'nullable' => false], |
||
| 22 | |||
| 23 | //Enum type (mapped via method) |
||
| 24 | 'enum' => 'enum', |
||
| 25 | |||
| 26 | //Logical types |
||
| 27 | 'boolean' => 'boolean', |
||
| 28 | |||
| 29 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
| 30 | //bigInteger |
||
| 31 | 'integer' => 'integer', |
||
| 32 | 'tinyInteger' => 'smallint', |
||
| 33 | 'bigInteger' => 'bigint', |
||
| 34 | |||
| 35 | //String with specified length (mapped via method) |
||
| 36 | 'string' => 'character varying', |
||
| 37 | |||
| 38 | //Generic types |
||
| 39 | 'text' => 'text', |
||
| 40 | 'tinyText' => 'text', |
||
| 41 | 'longText' => 'text', |
||
| 42 | |||
| 43 | //Real types |
||
| 44 | 'double' => 'double precision', |
||
| 45 | 'float' => 'real', |
||
| 46 | |||
| 47 | //Decimal type (mapped via method) |
||
| 48 | 'decimal' => 'numeric', |
||
| 49 | |||
| 50 | //Date and Time types |
||
| 51 | 'datetime' => 'timestamp without time zone', |
||
| 52 | 'date' => 'date', |
||
| 53 | 'time' => 'time', |
||
| 54 | 'timestamp' => 'timestamp without time zone', |
||
| 55 | |||
| 56 | //Binary types |
||
| 57 | 'binary' => 'bytea', |
||
| 58 | 'tinyBinary' => 'bytea', |
||
| 59 | 'longBinary' => 'bytea', |
||
| 60 | |||
| 61 | //Additional types |
||
| 62 | 'json' => 'json', |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | protected $reverseMapping = [ |
||
| 69 | 'primary' => ['serial'], |
||
| 70 | 'bigPrimary' => ['bigserial'], |
||
| 71 | 'enum' => ['enum'], |
||
| 72 | 'boolean' => ['boolean'], |
||
| 73 | 'integer' => ['int', 'integer', 'int4'], |
||
| 74 | 'tinyInteger' => ['smallint'], |
||
| 75 | 'bigInteger' => ['bigint', 'int8'], |
||
| 76 | 'string' => ['character varying', 'character'], |
||
| 77 | 'text' => ['text'], |
||
| 78 | 'double' => ['double precision'], |
||
| 79 | 'float' => ['real', 'money'], |
||
| 80 | 'decimal' => ['numeric'], |
||
| 81 | 'date' => ['date'], |
||
| 82 | 'time' => ['time', 'time with time zone', 'time without time zone'], |
||
| 83 | 'timestamp' => ['timestamp', 'timestamp with time zone', 'timestamp without time zone'], |
||
| 84 | 'binary' => ['bytea'], |
||
| 85 | 'json' => ['json'], |
||
| 86 | ]; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Field is auto incremental. |
||
| 90 | * |
||
| 91 | * @var bool |
||
| 92 | */ |
||
| 93 | protected $autoIncrement = false; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Indication that column have enum constrain. |
||
| 97 | * |
||
| 98 | * @var bool |
||
| 99 | */ |
||
| 100 | protected $constrained = false; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Name of enum constraint associated with field. |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $constrainName = ''; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function getConstraints(): array |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function abstractType(): string |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Overriding default magically happen method for custom logic. |
||
| 137 | * |
||
| 138 | * @return $this|AbstractColumn |
||
| 139 | */ |
||
| 140 | public function primary(): AbstractColumn |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Overriding default magically happen method for custom logic. |
||
| 154 | * |
||
| 155 | * @return $this|AbstractColumn |
||
| 156 | */ |
||
| 157 | public function bigPrimary(): AbstractColumn |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Postgres requires custom logic for enum values. |
||
| 171 | * |
||
| 172 | * @param array|string $values |
||
| 173 | * |
||
| 174 | * @return AbstractColumn |
||
| 175 | */ |
||
| 176 | public function enum($values): AbstractColumn |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function sqlStatement(Driver $driver): string |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Generate set of operations need to change column. |
||
| 215 | * |
||
| 216 | * @param Driver $driver |
||
| 217 | * @param AbstractColumn $initial |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function alterOperations(Driver $driver, AbstractColumn $initial): array |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | protected function prepareEnum(Driver $driver): string |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get/generate name for enum constraint. |
||
| 301 | * |
||
| 302 | * @todo investigate potential issue with entity non handling enum correctly when multiple |
||
| 303 | * @todo column changes happen in one session (who the hell will do that?) |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | private function enumConstraint(): string |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Normalize default value. |
||
| 318 | */ |
||
| 319 | private function normalizeDefault() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $table Table name. |
||
| 337 | * @param array $schema |
||
| 338 | * @param Driver $driver Postgres columns are bit more complex. |
||
| 339 | * |
||
| 340 | * @return PostgresColumn |
||
| 341 | */ |
||
| 342 | public static function createInstance( |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Resolving enum constrain and converting it into proper enum values set. |
||
| 396 | * |
||
| 397 | * @param Driver $driver |
||
| 398 | * @param string|int $tableOID |
||
| 399 | * @param PostgresColumn $column |
||
| 400 | */ |
||
| 401 | private static function resolveConstrains(Driver $driver, $tableOID, PostgresColumn $column) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Resolve native ENUM type if presented. |
||
| 432 | * |
||
| 433 | * @param Driver $driver |
||
| 434 | * @param PostgresColumn $column |
||
| 435 | */ |
||
| 436 | private static function resolveEnum(Driver $driver, PostgresColumn $column) |
||
| 451 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.