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_NOW = 'CURRENT_TIMESTAMP'; |
||
48 | |||
49 | /** |
||
50 | * Value to be excluded from comparation. |
||
51 | */ |
||
52 | const EXCLUDE_FROM_COMPARE = ['timezone']; |
||
53 | |||
54 | /** |
||
55 | * Normalization for time and dates. |
||
56 | */ |
||
57 | const DATE_FORMAT = 'Y-m-d'; |
||
58 | const TIME_FORMAT = 'H:i:s'; |
||
59 | |||
60 | /** |
||
61 | * Abstract type aliases (for consistency). |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | private $aliases = [ |
||
66 | 'int' => 'integer', |
||
67 | 'bigint' => 'bigInteger', |
||
68 | 'incremental' => 'primary', |
||
69 | 'bigIncremental' => 'bigPrimary', |
||
70 | 'bool' => 'boolean', |
||
71 | 'blob' => 'binary', |
||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * Association list between abstract types and native PHP types. Every non listed type will be |
||
76 | * converted into string. |
||
77 | * |
||
78 | * @invisible |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | private $phpMapping = [ |
||
83 | self::INT => ['primary', 'bigPrimary', 'integer', 'tinyInteger', 'bigInteger'], |
||
84 | self::BOOL => ['boolean'], |
||
85 | self::FLOAT => ['double', 'float', 'decimal'], |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * Mapping between abstract type and internal database type with it's options. Multiple abstract |
||
90 | * types can map into one database type, this implementation allows us to equalize two columns |
||
91 | * if they have different abstract types but same database one. Must be declared by DBMS |
||
92 | * specific implementation. |
||
93 | * |
||
94 | * Example: |
||
95 | * integer => array('type' => 'int', 'size' => 1), |
||
96 | * boolean => array('type' => 'tinyint', 'size' => 1) |
||
97 | * |
||
98 | * @invisible |
||
99 | * |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $mapping = [ |
||
103 | //Primary sequences |
||
104 | 'primary' => null, |
||
105 | 'bigPrimary' => null, |
||
106 | |||
107 | //Enum type (mapped via method) |
||
108 | 'enum' => null, |
||
109 | |||
110 | //Logical types |
||
111 | 'boolean' => null, |
||
112 | |||
113 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
114 | //bigInteger |
||
115 | 'integer' => null, |
||
116 | 'tinyInteger' => null, |
||
117 | 'bigInteger' => null, |
||
118 | |||
119 | //String with specified length (mapped via method) |
||
120 | 'string' => null, |
||
121 | |||
122 | //Generic types |
||
123 | 'text' => null, |
||
124 | 'tinyText' => null, |
||
125 | 'longText' => null, |
||
126 | |||
127 | //Real types |
||
128 | 'double' => null, |
||
129 | 'float' => null, |
||
130 | |||
131 | //Decimal type (mapped via method) |
||
132 | 'decimal' => null, |
||
133 | |||
134 | //Date and Time types |
||
135 | 'datetime' => null, |
||
136 | 'date' => null, |
||
137 | 'time' => null, |
||
138 | 'timestamp' => null, |
||
139 | |||
140 | //Binary types |
||
141 | 'binary' => null, |
||
142 | 'tinyBinary' => null, |
||
143 | 'longBinary' => null, |
||
144 | |||
145 | //Additional types |
||
146 | 'json' => null, |
||
147 | ]; |
||
148 | |||
149 | /** |
||
150 | * Reverse mapping is responsible for generating abstact type based on database type and it's |
||
151 | * options. Multiple database types can be mapped into one abstract type. |
||
152 | * |
||
153 | * @invisible |
||
154 | * |
||
155 | * @var array |
||
156 | */ |
||
157 | protected $reverseMapping = [ |
||
158 | 'primary' => [], |
||
159 | 'bigPrimary' => [], |
||
160 | 'enum' => [], |
||
161 | 'boolean' => [], |
||
162 | 'integer' => [], |
||
163 | 'tinyInteger' => [], |
||
164 | 'bigInteger' => [], |
||
165 | 'string' => [], |
||
166 | 'text' => [], |
||
167 | 'tinyText' => [], |
||
168 | 'longText' => [], |
||
169 | 'double' => [], |
||
170 | 'float' => [], |
||
171 | 'decimal' => [], |
||
172 | 'datetime' => [], |
||
173 | 'date' => [], |
||
174 | 'time' => [], |
||
175 | 'timestamp' => [], |
||
176 | 'binary' => [], |
||
177 | 'tinyBinary' => [], |
||
178 | 'longBinary' => [], |
||
179 | 'json' => [], |
||
180 | ]; |
||
181 | |||
182 | /** |
||
183 | * DBMS specific column type. |
||
184 | * |
||
185 | * @var string |
||
186 | */ |
||
187 | protected $type = ''; |
||
188 | |||
189 | /** |
||
190 | * @var \DateTimeZone |
||
191 | */ |
||
192 | protected $timezone = null; |
||
193 | |||
194 | /** |
||
195 | * Indicates that column can contain null values. |
||
196 | * |
||
197 | * @var bool |
||
198 | */ |
||
199 | protected $nullable = true; |
||
200 | |||
201 | /** |
||
202 | * Default column value, may not be applied to some datatypes (for example to primary keys), |
||
203 | * should follow type size and other options. |
||
204 | * |
||
205 | * @var mixed |
||
206 | */ |
||
207 | protected $defaultValue = null; |
||
208 | |||
209 | /** |
||
210 | * Column type size, can have different meanings for different datatypes. |
||
211 | * |
||
212 | * @var int |
||
213 | */ |
||
214 | protected $size = 0; |
||
215 | |||
216 | /** |
||
217 | * Precision of column, applied only for "decimal" type. |
||
218 | * |
||
219 | * @var int |
||
220 | */ |
||
221 | protected $precision = 0; |
||
222 | |||
223 | /** |
||
224 | * Scale of column, applied only for "decimal" type. |
||
225 | * |
||
226 | * @var int |
||
227 | */ |
||
228 | protected $scale = 0; |
||
229 | |||
230 | /** |
||
231 | * List of allowed enum values. |
||
232 | * |
||
233 | * @var array |
||
234 | */ |
||
235 | protected $enumValues = []; |
||
236 | |||
237 | /** |
||
238 | * @param string $table |
||
239 | * @param string $name |
||
240 | * @param \DateTimeZone $timezone |
||
241 | */ |
||
242 | public function __construct(string $table, string $name, \DateTimeZone $timezone) |
||
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | */ |
||
251 | public function getType(): string |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function phpType(): string |
||
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | public function getSize(): int |
||
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | */ |
||
282 | public function getPrecision(): int |
||
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | public function getScale(): int |
||
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | public function isNullable(): bool |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function hasDefaultValue(): bool |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | * |
||
314 | * @throws DefaultValueException |
||
315 | */ |
||
316 | public function getDefaultValue() |
||
346 | |||
347 | /** |
||
348 | * Get every associated column constraint names. |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | public function getConstraints(): array |
||
356 | |||
357 | /** |
||
358 | * Get allowed enum values. |
||
359 | * |
||
360 | * @return array |
||
361 | */ |
||
362 | public function getEnumValues(): array |
||
366 | |||
367 | /** |
||
368 | * DBMS specific reverse mapping must map database specific type into limited set of abstract |
||
369 | * types. |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | public function abstractType(): string |
||
405 | |||
406 | /** |
||
407 | * Give column new abstract type. DBMS specific implementation must map provided type into one |
||
408 | * of internal database values. |
||
409 | * |
||
410 | * Attention, changing type of existed columns in some databases has a lot of restrictions like |
||
411 | * cross type conversions and etc. Try do not change column type without a reason. |
||
412 | * |
||
413 | * @param string $abstract Abstract or virtual type declared in mapping. |
||
414 | * |
||
415 | * @return self|$this |
||
416 | * |
||
417 | * @throws SchemaException |
||
418 | */ |
||
419 | public function setType(string $abstract): AbstractColumn |
||
446 | |||
447 | /** |
||
448 | * Set column nullable/not nullable. |
||
449 | * |
||
450 | * @param bool $nullable |
||
451 | * |
||
452 | * @return self|$this |
||
453 | */ |
||
454 | public function nullable(bool $nullable = true): AbstractColumn |
||
460 | |||
461 | /** |
||
462 | * Change column default value (can be forbidden for some column types). |
||
463 | * Use Database::TIMESTAMP_NOW to use driver specific NOW() function. |
||
464 | * |
||
465 | * @param mixed $value |
||
466 | * |
||
467 | * @return self|$this |
||
468 | */ |
||
469 | public function defaultValue($value): AbstractColumn |
||
480 | |||
481 | /** |
||
482 | * Set column as enum type and specify set of allowed values. Most of drivers will emulate enums |
||
483 | * using column constraints. |
||
484 | * |
||
485 | * Examples: |
||
486 | * $table->status->enum(['active', 'disabled']); |
||
487 | * $table->status->enum('active', 'disabled'); |
||
488 | * |
||
489 | * @param string|array $values Enum values (array or comma separated). String values only. |
||
490 | * |
||
491 | * @return self |
||
492 | */ |
||
493 | public function enum($values): AbstractColumn |
||
500 | |||
501 | /** |
||
502 | * Set column type as string with limited size. Maximum allowed size is 255 bytes, use "text" |
||
503 | * abstract types for longer strings. |
||
504 | * |
||
505 | * Strings are perfect type to store email addresses as it big enough to store valid address |
||
506 | * and |
||
507 | * can be covered with unique index. |
||
508 | * |
||
509 | * @link http://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address |
||
510 | * |
||
511 | * @param int $size Max string length. |
||
512 | * |
||
513 | * @return self|$this |
||
514 | * |
||
515 | * @throws SchemaException |
||
516 | */ |
||
517 | public function string(int $size = 255): AbstractColumn |
||
533 | |||
534 | /** |
||
535 | * Set column type as decimal with specific precision and scale. |
||
536 | * |
||
537 | * @param int $precision |
||
538 | * @param int $scale |
||
539 | * |
||
540 | * @return self|$this |
||
541 | * |
||
542 | * @throws SchemaException |
||
543 | */ |
||
544 | public function decimal(int $precision, int $scale = 0): AbstractColumn |
||
557 | |||
558 | /** |
||
559 | * Shortcut for AbstractColumn->type() method. |
||
560 | * |
||
561 | * @param string $type Abstract type. |
||
562 | * @param array $arguments Not used. |
||
563 | * |
||
564 | * @return self |
||
565 | */ |
||
566 | public function __call(string $type, array $arguments = []): AbstractColumn |
||
570 | |||
571 | /** |
||
572 | * {@inheritdoc} |
||
573 | */ |
||
574 | public function compare(ColumnInterface $initial): bool |
||
607 | |||
608 | /** |
||
609 | * {@inheritdoc} |
||
610 | */ |
||
611 | public function sqlStatement(Driver $driver): string |
||
634 | |||
635 | /** |
||
636 | * @return string |
||
637 | */ |
||
638 | public function __toString() |
||
642 | |||
643 | /** |
||
644 | * Simplified way to dump information. |
||
645 | * |
||
646 | * @return array |
||
647 | */ |
||
648 | public function __debugInfo() |
||
682 | |||
683 | /** |
||
684 | * Get database specific enum type definition options. |
||
685 | * |
||
686 | * @param Driver $driver |
||
687 | * |
||
688 | * @return string |
||
689 | */ |
||
690 | protected function quoteEnum(Driver $driver): string |
||
703 | |||
704 | /** |
||
705 | * Must return driver specific default value. |
||
706 | * |
||
707 | * @param Driver $driver |
||
708 | * |
||
709 | * @return string |
||
710 | */ |
||
711 | protected function quoteDefault(Driver $driver): string |
||
736 | |||
737 | /** |
||
738 | * Ensure that datetime fields are correctly formatted. |
||
739 | * |
||
740 | * @param string $type |
||
741 | * @param string $value |
||
742 | * |
||
743 | * @return string|FragmentInterface|\DateTime |
||
744 | * |
||
745 | * @throws DefaultValueException |
||
746 | */ |
||
747 | protected function normalizeDatetime(string $type, $value) |
||
780 | } |