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 | * PHP types for phpType() method. |
||
| 42 | */ |
||
| 43 | const INT = 'int'; |
||
| 44 | const BOOL = 'bool'; |
||
| 45 | const STRING = 'string'; |
||
| 46 | const FLOAT = 'float'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Default datetime value. |
||
| 50 | */ |
||
| 51 | const DATETIME_DEFAULT = '1970-01-01 00:00:00'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Default timestamp expression (driver specific). |
||
| 55 | */ |
||
| 56 | const DATETIME_CURRENT = 'CURRENT_TIMESTAMP'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Abstract type aliases (for consistency). |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $aliases = [ |
||
| 64 | 'int' => 'integer', |
||
| 65 | 'bigint' => 'bigInteger', |
||
| 66 | 'incremental' => 'primary', |
||
| 67 | 'bigIncremental' => 'bigPrimary', |
||
| 68 | 'bool' => 'boolean', |
||
| 69 | 'blob' => 'binary', |
||
| 70 | ]; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Association list between abstract types and native PHP types. Every non listed type will be |
||
| 74 | * converted into string. |
||
| 75 | * |
||
| 76 | * @invisible |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private $phpMapping = [ |
||
| 81 | self::INT => ['primary', 'bigPrimary', 'integer', 'tinyInteger', 'bigInteger'], |
||
| 82 | self::BOOL => ['boolean'], |
||
| 83 | self::FLOAT => ['double', 'float', 'decimal'], |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Mapping between abstract type and internal database type with it's options. Multiple abstract |
||
| 88 | * types can map into one database type, this implementation allows us to equalize two columns |
||
| 89 | * if they have different abstract types but same database one. Must be declared by DBMS |
||
| 90 | * specific implementation. |
||
| 91 | * |
||
| 92 | * Example: |
||
| 93 | * integer => array('type' => 'int', 'size' => 1), |
||
| 94 | * boolean => array('type' => 'tinyint', 'size' => 1) |
||
| 95 | * |
||
| 96 | * @invisible |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $mapping = [ |
||
| 101 | //Primary sequences |
||
| 102 | 'primary' => null, |
||
| 103 | 'bigPrimary' => null, |
||
| 104 | |||
| 105 | //Enum type (mapped via method) |
||
| 106 | 'enum' => null, |
||
| 107 | |||
| 108 | //Logical types |
||
| 109 | 'boolean' => null, |
||
| 110 | |||
| 111 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
| 112 | //bigInteger |
||
| 113 | 'integer' => null, |
||
| 114 | 'tinyInteger' => null, |
||
| 115 | 'bigInteger' => null, |
||
| 116 | |||
| 117 | //String with specified length (mapped via method) |
||
| 118 | 'string' => null, |
||
| 119 | |||
| 120 | //Generic types |
||
| 121 | 'text' => null, |
||
| 122 | 'tinyText' => null, |
||
| 123 | 'longText' => null, |
||
| 124 | |||
| 125 | //Real types |
||
| 126 | 'double' => null, |
||
| 127 | 'float' => null, |
||
| 128 | |||
| 129 | //Decimal type (mapped via method) |
||
| 130 | 'decimal' => null, |
||
| 131 | |||
| 132 | //Date and Time types |
||
| 133 | 'datetime' => null, |
||
| 134 | 'date' => null, |
||
| 135 | 'time' => null, |
||
| 136 | 'timestamp' => null, |
||
| 137 | |||
| 138 | //Binary types |
||
| 139 | 'binary' => null, |
||
| 140 | 'tinyBinary' => null, |
||
| 141 | 'longBinary' => null, |
||
| 142 | |||
| 143 | //Additional types |
||
| 144 | 'json' => null, |
||
| 145 | ]; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Reverse mapping is responsible for generating abstact type based on database type and it's |
||
| 149 | * options. Multiple database types can be mapped into one abstract type. |
||
| 150 | * |
||
| 151 | * @invisible |
||
| 152 | * |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | protected $reverseMapping = [ |
||
| 156 | 'primary' => [], |
||
| 157 | 'bigPrimary' => [], |
||
| 158 | 'enum' => [], |
||
| 159 | 'boolean' => [], |
||
| 160 | 'integer' => [], |
||
| 161 | 'tinyInteger' => [], |
||
| 162 | 'bigInteger' => [], |
||
| 163 | 'string' => [], |
||
| 164 | 'text' => [], |
||
| 165 | 'tinyText' => [], |
||
| 166 | 'longText' => [], |
||
| 167 | 'double' => [], |
||
| 168 | 'float' => [], |
||
| 169 | 'decimal' => [], |
||
| 170 | 'datetime' => [], |
||
| 171 | 'date' => [], |
||
| 172 | 'time' => [], |
||
| 173 | 'timestamp' => [], |
||
| 174 | 'binary' => [], |
||
| 175 | 'tinyBinary' => [], |
||
| 176 | 'longBinary' => [], |
||
| 177 | 'json' => [], |
||
| 178 | ]; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * DBMS specific column type. |
||
| 182 | * |
||
| 183 | * @var string |
||
| 184 | */ |
||
| 185 | protected $type = ''; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Indicates that column can contain null values. |
||
| 189 | * |
||
| 190 | * @var bool |
||
| 191 | */ |
||
| 192 | protected $nullable = true; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Default column value, may not be applied to some datatypes (for example to primary keys), |
||
| 196 | * should follow type size and other options. |
||
| 197 | * |
||
| 198 | * @var mixed |
||
| 199 | */ |
||
| 200 | protected $defaultValue = null; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Column type size, can have different meanings for different datatypes. |
||
| 204 | * |
||
| 205 | * @var int |
||
| 206 | */ |
||
| 207 | protected $size = 0; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Precision of column, applied only for "decimal" type. |
||
| 211 | * |
||
| 212 | * @var int |
||
| 213 | */ |
||
| 214 | protected $precision = 0; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Scale of column, applied only for "decimal" type. |
||
| 218 | * |
||
| 219 | * @var int |
||
| 220 | */ |
||
| 221 | protected $scale = 0; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * List of allowed enum values. |
||
| 225 | * |
||
| 226 | * @var array |
||
| 227 | */ |
||
| 228 | protected $enumValues = []; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function getType(): string |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function phpType(): string |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function getSize(): int |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function getPrecision(): int |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function getScale(): int |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | public function isNullable(): bool |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function hasDefaultValue(): bool |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function getDefaultValue() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get every associated column constraint names. |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function getConstraints(): array |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get allowed enum values. |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public function getEnumValues(): array |
||
| 349 | |||
| 350 | /** |
||
| 351 | * DBMS specific reverse mapping must map database specific type into limited set of abstract |
||
| 352 | * types. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function abstractType(): string |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Must compare two instances of AbstractColumn. |
||
| 391 | * |
||
| 392 | * @param ColumnInterface $initial |
||
| 393 | * |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | public function compare(ColumnInterface $initial): bool |
||
| 426 | |||
| 427 | /** |
||
| 428 | * {@inheritdoc} |
||
| 429 | */ |
||
| 430 | public function sqlStatement(Driver $driver): string |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get database specific enum type definition options. |
||
| 456 | * |
||
| 457 | * @param Driver $driver |
||
| 458 | * |
||
| 459 | * @return string. |
||
|
|
|||
| 460 | */ |
||
| 461 | protected function prepareEnum(Driver $driver): string |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Must return driver specific default value. |
||
| 477 | * |
||
| 478 | * @param Driver $driver |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | protected function prepareDefault(Driver $driver): string |
||
| 506 | } |
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.