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 |
||
| 38 | abstract class AbstractColumn extends AbstractElement implements ColumnInterface |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Default datetime value. |
||
| 42 | */ |
||
| 43 | const DATETIME_DEFAULT = '1970-01-01 00:00:00'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Default timestamp expression (driver specific). |
||
| 47 | */ |
||
| 48 | const DATETIME_CURRENT = 'CURRENT_TIMESTAMP'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Abstract type aliases (for consistency). |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $aliases = [ |
||
| 56 | 'int' => 'integer', |
||
| 57 | 'bigint' => 'bigInteger', |
||
| 58 | 'incremental' => 'primary', |
||
| 59 | 'bigIncremental' => 'bigPrimary', |
||
| 60 | 'bool' => 'boolean', |
||
| 61 | 'blob' => 'binary', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Association list between abstract types and native PHP types. Every non listed type will be |
||
| 66 | * converted into string. |
||
| 67 | * |
||
| 68 | * @invisible |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $phpMapping = [ |
||
| 73 | self::INT => ['primary', 'bigPrimary', 'integer', 'tinyInteger', 'bigInteger'], |
||
| 74 | self::BOOL => ['boolean'], |
||
| 75 | self::FLOAT => ['double', 'float', 'decimal'], |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Mapping between abstract type and internal database type with it's options. Multiple abstract |
||
| 80 | * types can map into one database type, this implementation allows us to equalize two columns |
||
| 81 | * if they have different abstract types but same database one. Must be declared by DBMS |
||
| 82 | * specific implementation. |
||
| 83 | * |
||
| 84 | * Example: |
||
| 85 | * integer => array('type' => 'int', 'size' => 1), |
||
| 86 | * boolean => array('type' => 'tinyint', 'size' => 1) |
||
| 87 | * |
||
| 88 | * @invisible |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $mapping = [ |
||
| 93 | //Primary sequences |
||
| 94 | 'primary' => null, |
||
| 95 | 'bigPrimary' => null, |
||
| 96 | |||
| 97 | //Enum type (mapped via method) |
||
| 98 | 'enum' => null, |
||
| 99 | |||
| 100 | //Logical types |
||
| 101 | 'boolean' => null, |
||
| 102 | |||
| 103 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
| 104 | //bigInteger |
||
| 105 | 'integer' => null, |
||
| 106 | 'tinyInteger' => null, |
||
| 107 | 'bigInteger' => null, |
||
| 108 | |||
| 109 | //String with specified length (mapped via method) |
||
| 110 | 'string' => null, |
||
| 111 | |||
| 112 | //Generic types |
||
| 113 | 'text' => null, |
||
| 114 | 'tinyText' => null, |
||
| 115 | 'longText' => null, |
||
| 116 | |||
| 117 | //Real types |
||
| 118 | 'double' => null, |
||
| 119 | 'float' => null, |
||
| 120 | |||
| 121 | //Decimal type (mapped via method) |
||
| 122 | 'decimal' => null, |
||
| 123 | |||
| 124 | //Date and Time types |
||
| 125 | 'datetime' => null, |
||
| 126 | 'date' => null, |
||
| 127 | 'time' => null, |
||
| 128 | 'timestamp' => null, |
||
| 129 | |||
| 130 | //Binary types |
||
| 131 | 'binary' => null, |
||
| 132 | 'tinyBinary' => null, |
||
| 133 | 'longBinary' => null, |
||
| 134 | |||
| 135 | //Additional types |
||
| 136 | 'json' => null, |
||
| 137 | ]; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Reverse mapping is responsible for generating abstact type based on database type and it's |
||
| 141 | * options. Multiple database types can be mapped into one abstract type. |
||
| 142 | * |
||
| 143 | * @invisible |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | protected $reverseMapping = [ |
||
| 148 | 'primary' => [], |
||
| 149 | 'bigPrimary' => [], |
||
| 150 | 'enum' => [], |
||
| 151 | 'boolean' => [], |
||
| 152 | 'integer' => [], |
||
| 153 | 'tinyInteger' => [], |
||
| 154 | 'bigInteger' => [], |
||
| 155 | 'string' => [], |
||
| 156 | 'text' => [], |
||
| 157 | 'tinyText' => [], |
||
| 158 | 'longText' => [], |
||
| 159 | 'double' => [], |
||
| 160 | 'float' => [], |
||
| 161 | 'decimal' => [], |
||
| 162 | 'datetime' => [], |
||
| 163 | 'date' => [], |
||
| 164 | 'time' => [], |
||
| 165 | 'timestamp' => [], |
||
| 166 | 'binary' => [], |
||
| 167 | 'tinyBinary' => [], |
||
| 168 | 'longBinary' => [], |
||
| 169 | 'json' => [], |
||
| 170 | ]; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * DBMS specific column type. |
||
| 174 | * |
||
| 175 | * @var string |
||
| 176 | */ |
||
| 177 | protected $type = ''; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Indicates that column can contain null values. |
||
| 181 | * |
||
| 182 | * @var bool |
||
| 183 | */ |
||
| 184 | protected $nullable = true; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Default column value, may not be applied to some datatypes (for example to primary keys), |
||
| 188 | * should follow type size and other options. |
||
| 189 | * |
||
| 190 | * @var mixed |
||
| 191 | */ |
||
| 192 | protected $defaultValue = null; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Column type size, can have different meanings for different datatypes. |
||
| 196 | * |
||
| 197 | * @var int |
||
| 198 | */ |
||
| 199 | protected $size = 0; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Precision of column, applied only for "decimal" type. |
||
| 203 | * |
||
| 204 | * @var int |
||
| 205 | */ |
||
| 206 | protected $precision = 0; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Scale of column, applied only for "decimal" type. |
||
| 210 | * |
||
| 211 | * @var int |
||
| 212 | */ |
||
| 213 | protected $scale = 0; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * List of allowed enum values. |
||
| 217 | * |
||
| 218 | * @var array |
||
| 219 | */ |
||
| 220 | protected $enumValues = []; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function getType(): string |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function phpType(): string |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | public function getSize(): int |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function getPrecision(): int |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function getScale(): int |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function isNullable(): bool |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | public function hasDefaultValue(): bool |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function getDefaultValue() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get every associated column constraint names. |
||
| 323 | * |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | public function getConstraints(): array |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get allowed enum values. |
||
| 333 | * |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | public function getEnumValues(): array |
||
| 340 | |||
| 341 | /** |
||
| 342 | * DBMS specific reverse mapping must map database specific type into limited set of abstract |
||
| 343 | * types. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function abstractType(): string |
||
| 379 | |||
| 380 | //--- MODIFICATIONS IS HERE |
||
| 381 | |||
| 382 | /** |
||
| 383 | * {@inheritdoc} |
||
| 384 | */ |
||
| 385 | public function compare(ColumnInterface $initial): bool |
||
| 415 | |||
| 416 | /** |
||
| 417 | * {@inheritdoc} |
||
| 418 | */ |
||
| 419 | public function sqlStatement(Driver $driver): string |
||
| 442 | |||
| 443 | |||
| 444 | /** |
||
| 445 | * Simplified way to dump information. |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | public function __debugInfo() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get database specific enum type definition options. |
||
| 486 | * |
||
| 487 | * @param Driver $driver |
||
| 488 | * |
||
| 489 | * @return string. |
||
|
|
|||
| 490 | */ |
||
| 491 | protected function prepareEnum(Driver $driver): string |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Must return driver specific default value. |
||
| 507 | * |
||
| 508 | * @param Driver $driver |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | protected function prepareDefault(Driver $driver): string |
||
| 536 | } |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.