Complex classes like AbstractColumn 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 AbstractColumn, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | abstract class AbstractColumn extends AbstractElement implements ColumnInterface |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * Default timestamp expression (driver specific). |
||
| 46 | */ |
||
| 47 | const DATETIME_CURRENT = 'CURRENT_TIMESTAMP'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Normalization for time and dates. |
||
| 51 | */ |
||
| 52 | const DATE_FORMAT = 'Y-m-d'; |
||
| 53 | const TIME_FORMAT = 'H:i:s'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Abstract type aliases (for consistency). |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $aliases = [ |
||
| 61 | 'int' => 'integer', |
||
| 62 | 'bigint' => 'bigInteger', |
||
| 63 | 'incremental' => 'primary', |
||
| 64 | 'bigIncremental' => 'bigPrimary', |
||
| 65 | 'bool' => 'boolean', |
||
| 66 | 'blob' => 'binary', |
||
| 67 | ]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Association list between abstract types and native PHP types. Every non listed type will be |
||
| 71 | * converted into string. |
||
| 72 | * |
||
| 73 | * @invisible |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private $phpMapping = [ |
||
| 78 | self::INT => ['primary', 'bigPrimary', 'integer', 'tinyInteger', 'bigInteger'], |
||
| 79 | self::BOOL => ['boolean'], |
||
| 80 | self::FLOAT => ['double', 'float', 'decimal'], |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Mapping between abstract type and internal database type with it's options. Multiple abstract |
||
| 85 | * types can map into one database type, this implementation allows us to equalize two columns |
||
| 86 | * if they have different abstract types but same database one. Must be declared by DBMS |
||
| 87 | * specific implementation. |
||
| 88 | * |
||
| 89 | * Example: |
||
| 90 | * integer => array('type' => 'int', 'size' => 1), |
||
| 91 | * boolean => array('type' => 'tinyint', 'size' => 1) |
||
| 92 | * |
||
| 93 | * @invisible |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $mapping = [ |
||
| 98 | //Primary sequences |
||
| 99 | 'primary' => null, |
||
| 100 | 'bigPrimary' => null, |
||
| 101 | |||
| 102 | //Enum type (mapped via method) |
||
| 103 | 'enum' => null, |
||
| 104 | |||
| 105 | //Logical types |
||
| 106 | 'boolean' => null, |
||
| 107 | |||
| 108 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
| 109 | //bigInteger |
||
| 110 | 'integer' => null, |
||
| 111 | 'tinyInteger' => null, |
||
| 112 | 'bigInteger' => null, |
||
| 113 | |||
| 114 | //String with specified length (mapped via method) |
||
| 115 | 'string' => null, |
||
| 116 | |||
| 117 | //Generic types |
||
| 118 | 'text' => null, |
||
| 119 | 'tinyText' => null, |
||
| 120 | 'longText' => null, |
||
| 121 | |||
| 122 | //Real types |
||
| 123 | 'double' => null, |
||
| 124 | 'float' => null, |
||
| 125 | |||
| 126 | //Decimal type (mapped via method) |
||
| 127 | 'decimal' => null, |
||
| 128 | |||
| 129 | //Date and Time types |
||
| 130 | 'datetime' => null, |
||
| 131 | 'date' => null, |
||
| 132 | 'time' => null, |
||
| 133 | 'timestamp' => null, |
||
| 134 | |||
| 135 | //Binary types |
||
| 136 | 'binary' => null, |
||
| 137 | 'tinyBinary' => null, |
||
| 138 | 'longBinary' => null, |
||
| 139 | |||
| 140 | //Additional types |
||
| 141 | 'json' => null, |
||
| 142 | ]; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Reverse mapping is responsible for generating abstact type based on database type and it's |
||
| 146 | * options. Multiple database types can be mapped into one abstract type. |
||
| 147 | * |
||
| 148 | * @invisible |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $reverseMapping = [ |
||
| 153 | 'primary' => [], |
||
| 154 | 'bigPrimary' => [], |
||
| 155 | 'enum' => [], |
||
| 156 | 'boolean' => [], |
||
| 157 | 'integer' => [], |
||
| 158 | 'tinyInteger' => [], |
||
| 159 | 'bigInteger' => [], |
||
| 160 | 'string' => [], |
||
| 161 | 'text' => [], |
||
| 162 | 'tinyText' => [], |
||
| 163 | 'longText' => [], |
||
| 164 | 'double' => [], |
||
| 165 | 'float' => [], |
||
| 166 | 'decimal' => [], |
||
| 167 | 'datetime' => [], |
||
| 168 | 'date' => [], |
||
| 169 | 'time' => [], |
||
| 170 | 'timestamp' => [], |
||
| 171 | 'binary' => [], |
||
| 172 | 'tinyBinary' => [], |
||
| 173 | 'longBinary' => [], |
||
| 174 | 'json' => [], |
||
| 175 | ]; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * DBMS specific column type. |
||
| 179 | * |
||
| 180 | * @var string |
||
| 181 | */ |
||
| 182 | protected $type = ''; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Indicates that column can contain null values. |
||
| 186 | * |
||
| 187 | * @var bool |
||
| 188 | */ |
||
| 189 | protected $nullable = true; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Default column value, may not be applied to some datatypes (for example to primary keys), |
||
| 193 | * should follow type size and other options. |
||
| 194 | * |
||
| 195 | * @var mixed |
||
| 196 | */ |
||
| 197 | protected $defaultValue = null; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Column type size, can have different meanings for different datatypes. |
||
| 201 | * |
||
| 202 | * @var int |
||
| 203 | */ |
||
| 204 | protected $size = 0; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Precision of column, applied only for "decimal" type. |
||
| 208 | * |
||
| 209 | * @var int |
||
| 210 | */ |
||
| 211 | protected $precision = 0; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Scale of column, applied only for "decimal" type. |
||
| 215 | * |
||
| 216 | * @var int |
||
| 217 | */ |
||
| 218 | protected $scale = 0; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * List of allowed enum values. |
||
| 222 | * |
||
| 223 | * @var array |
||
| 224 | */ |
||
| 225 | protected $enumValues = []; |
||
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | */ |
||
| 230 | public function getType(): string |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | public function phpType(): string |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | public function getSize(): int |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function getPrecision(): int |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function getScale(): int |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function isNullable(): bool |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritdoc} |
||
| 284 | */ |
||
| 285 | public function hasDefaultValue(): bool |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | public function getDefaultValue() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get every associated column constraint names. |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public function getConstraints(): array |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get allowed enum values. |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public function getEnumValues(): array |
||
| 342 | |||
| 343 | /** |
||
| 344 | * DBMS specific reverse mapping must map database specific type into limited set of abstract |
||
| 345 | * types. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function abstractType(): string |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Give column new abstract type. DBMS specific implementation must map provided type into one |
||
| 384 | * of internal database values. |
||
| 385 | * |
||
| 386 | * Attention, changing type of existed columns in some databases has a lot of restrictions like |
||
| 387 | * cross type conversions and etc. Try do not change column type without a reason. |
||
| 388 | * |
||
| 389 | * @param string $abstract Abstract or virtual type declared in mapping. |
||
| 390 | * |
||
| 391 | * @return self|$this |
||
| 392 | * |
||
| 393 | * @throws SchemaException |
||
| 394 | */ |
||
| 395 | public function setType(string $abstract): AbstractColumn |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Set column nullable/not nullable. |
||
| 425 | * |
||
| 426 | * @param bool $nullable |
||
| 427 | * |
||
| 428 | * @return self|$this |
||
| 429 | */ |
||
| 430 | public function nullable(bool $nullable = true): AbstractColumn |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Change column default value (can be forbidden for some column types). |
||
| 439 | * Use Database::TIMESTAMP_NOW to use driver specific NOW() function. |
||
| 440 | * |
||
| 441 | * @param mixed $value |
||
| 442 | * |
||
| 443 | * @return self|$this |
||
| 444 | */ |
||
| 445 | public function defaultValue($value): AbstractColumn |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Set column as enum type and specify set of allowed values. Most of drivers will emulate enums |
||
| 459 | * using column constraints. |
||
| 460 | * |
||
| 461 | * Examples: |
||
| 462 | * $table->status->enum(['active', 'disabled']); |
||
| 463 | * $table->status->enum('active', 'disabled'); |
||
| 464 | * |
||
| 465 | * @param string|array $values Enum values (array or comma separated). String values only. |
||
| 466 | * |
||
| 467 | * @return self |
||
| 468 | */ |
||
| 469 | public function enum($values): AbstractColumn |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Set column type as string with limited size. Maximum allowed size is 255 bytes, use "text" |
||
| 479 | * abstract types for longer strings. |
||
| 480 | * |
||
| 481 | * Strings are perfect type to store email addresses as it big enough to store valid address |
||
| 482 | * and |
||
| 483 | * can be covered with unique index. |
||
| 484 | * |
||
| 485 | * @link http://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address |
||
| 486 | * |
||
| 487 | * @param int $size Max string length. |
||
| 488 | * |
||
| 489 | * @return self|$this |
||
| 490 | * |
||
| 491 | * @throws SchemaException |
||
| 492 | */ |
||
| 493 | public function string(int $size = 255): AbstractColumn |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Set column type as decimal with specific precision and scale. |
||
| 512 | * |
||
| 513 | * @param int $precision |
||
| 514 | * @param int $scale |
||
| 515 | * |
||
| 516 | * @return self|$this |
||
| 517 | * |
||
| 518 | * @throws SchemaException |
||
| 519 | */ |
||
| 520 | public function decimal(int $precision, int $scale = 0): AbstractColumn |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Shortcut for AbstractColumn->type() method. |
||
| 536 | * |
||
| 537 | * @param string $type Abstract type. |
||
| 538 | * @param array $arguments Not used. |
||
| 539 | * |
||
| 540 | * @return self |
||
| 541 | */ |
||
| 542 | public function __call(string $type, array $arguments = []): AbstractColumn |
||
| 546 | |||
| 547 | /** |
||
| 548 | * {@inheritdoc} |
||
| 549 | */ |
||
| 550 | public function compare(ColumnInterface $initial): bool |
||
| 580 | |||
| 581 | /** |
||
| 582 | * {@inheritdoc} |
||
| 583 | */ |
||
| 584 | public function sqlStatement(Driver $driver): string |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Simplified way to dump information. |
||
| 610 | * |
||
| 611 | * @return array |
||
| 612 | */ |
||
| 613 | public function __debugInfo() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Get database specific enum type definition options. |
||
| 650 | * |
||
| 651 | * @param Driver $driver |
||
| 652 | * |
||
| 653 | * @return string |
||
| 654 | */ |
||
| 655 | protected function prepareEnum(Driver $driver): string |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Must return driver specific default value. |
||
| 671 | * |
||
| 672 | * @param Driver $driver |
||
| 673 | * |
||
| 674 | * @return string |
||
| 675 | */ |
||
| 676 | protected function prepareDefault(Driver $driver): string |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Ensure that datetime fields are correctly formatted. |
||
| 704 | * |
||
| 705 | * @param string $type |
||
| 706 | * @param string $value |
||
| 707 | * |
||
| 708 | * @return string|FragmentInterface|\DateTime |
||
| 709 | * |
||
| 710 | * @throws DefaultValueException |
||
| 711 | */ |
||
| 712 | protected function normalizeDatetime(string $type, $value) |
||
| 749 | } |