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 |
||
41 | abstract class AbstractColumn extends AbstractElement implements ColumnInterface |
||
42 | { |
||
43 | /** |
||
44 | * Default datetime value. |
||
45 | */ |
||
46 | const DATETIME_DEFAULT = '1970-01-01 00:00:00'; |
||
47 | |||
48 | /** |
||
49 | * Default timestamp expression (driver specific). |
||
50 | */ |
||
51 | const DATETIME_CURRENT = 'CURRENT_TIMESTAMP'; |
||
52 | |||
53 | /** |
||
54 | * Abstract type aliases (for consistency). |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | private $aliases = [ |
||
59 | 'int' => 'integer', |
||
60 | 'bigint' => 'bigInteger', |
||
61 | 'incremental' => 'primary', |
||
62 | 'bigIncremental' => 'bigPrimary', |
||
63 | 'bool' => 'boolean', |
||
64 | 'blob' => 'binary', |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Association list between abstract types and native PHP types. Every non listed type will be |
||
69 | * converted into string. |
||
70 | * |
||
71 | * @invisible |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | private $phpMapping = [ |
||
76 | self::INT => ['primary', 'bigPrimary', 'integer', 'tinyInteger', 'bigInteger'], |
||
77 | self::BOOL => ['boolean'], |
||
78 | self::FLOAT => ['double', 'float', 'decimal'], |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * Mapping between abstract type and internal database type with it's options. Multiple abstract |
||
83 | * types can map into one database type, this implementation allows us to equalize two columns |
||
84 | * if they have different abstract types but same database one. Must be declared by DBMS |
||
85 | * specific implementation. |
||
86 | * |
||
87 | * Example: |
||
88 | * integer => array('type' => 'int', 'size' => 1), |
||
89 | * boolean => array('type' => 'tinyint', 'size' => 1) |
||
90 | * |
||
91 | * @invisible |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $mapping = [ |
||
96 | //Primary sequences |
||
97 | 'primary' => null, |
||
98 | 'bigPrimary' => null, |
||
99 | |||
100 | //Enum type (mapped via method) |
||
101 | 'enum' => null, |
||
102 | |||
103 | //Logical types |
||
104 | 'boolean' => null, |
||
105 | |||
106 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
107 | //bigInteger |
||
108 | 'integer' => null, |
||
109 | 'tinyInteger' => null, |
||
110 | 'bigInteger' => null, |
||
111 | |||
112 | //String with specified length (mapped via method) |
||
113 | 'string' => null, |
||
114 | |||
115 | //Generic types |
||
116 | 'text' => null, |
||
117 | 'tinyText' => null, |
||
118 | 'longText' => null, |
||
119 | |||
120 | //Real types |
||
121 | 'double' => null, |
||
122 | 'float' => null, |
||
123 | |||
124 | //Decimal type (mapped via method) |
||
125 | 'decimal' => null, |
||
126 | |||
127 | //Date and Time types |
||
128 | 'datetime' => null, |
||
129 | 'date' => null, |
||
130 | 'time' => null, |
||
131 | 'timestamp' => null, |
||
132 | |||
133 | //Binary types |
||
134 | 'binary' => null, |
||
135 | 'tinyBinary' => null, |
||
136 | 'longBinary' => null, |
||
137 | |||
138 | //Additional types |
||
139 | 'json' => null, |
||
140 | ]; |
||
141 | |||
142 | /** |
||
143 | * Reverse mapping is responsible for generating abstact type based on database type and it's |
||
144 | * options. Multiple database types can be mapped into one abstract type. |
||
145 | * |
||
146 | * @invisible |
||
147 | * |
||
148 | * @var array |
||
149 | */ |
||
150 | protected $reverseMapping = [ |
||
151 | 'primary' => [], |
||
152 | 'bigPrimary' => [], |
||
153 | 'enum' => [], |
||
154 | 'boolean' => [], |
||
155 | 'integer' => [], |
||
156 | 'tinyInteger' => [], |
||
157 | 'bigInteger' => [], |
||
158 | 'string' => [], |
||
159 | 'text' => [], |
||
160 | 'tinyText' => [], |
||
161 | 'longText' => [], |
||
162 | 'double' => [], |
||
163 | 'float' => [], |
||
164 | 'decimal' => [], |
||
165 | 'datetime' => [], |
||
166 | 'date' => [], |
||
167 | 'time' => [], |
||
168 | 'timestamp' => [], |
||
169 | 'binary' => [], |
||
170 | 'tinyBinary' => [], |
||
171 | 'longBinary' => [], |
||
172 | 'json' => [], |
||
173 | ]; |
||
174 | |||
175 | /** |
||
176 | * DBMS specific column type. |
||
177 | * |
||
178 | * @var string |
||
179 | */ |
||
180 | protected $type = ''; |
||
181 | |||
182 | /** |
||
183 | * Indicates that column can contain null values. |
||
184 | * |
||
185 | * @var bool |
||
186 | */ |
||
187 | protected $nullable = true; |
||
188 | |||
189 | /** |
||
190 | * Default column value, may not be applied to some datatypes (for example to primary keys), |
||
191 | * should follow type size and other options. |
||
192 | * |
||
193 | * @var mixed |
||
194 | */ |
||
195 | protected $defaultValue = null; |
||
196 | |||
197 | /** |
||
198 | * Column type size, can have different meanings for different datatypes. |
||
199 | * |
||
200 | * @var int |
||
201 | */ |
||
202 | protected $size = 0; |
||
203 | |||
204 | /** |
||
205 | * Precision of column, applied only for "decimal" type. |
||
206 | * |
||
207 | * @var int |
||
208 | */ |
||
209 | protected $precision = 0; |
||
210 | |||
211 | /** |
||
212 | * Scale of column, applied only for "decimal" type. |
||
213 | * |
||
214 | * @var int |
||
215 | */ |
||
216 | protected $scale = 0; |
||
217 | |||
218 | /** |
||
219 | * List of allowed enum values. |
||
220 | * |
||
221 | * @var array |
||
222 | */ |
||
223 | protected $enumValues = []; |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function getType(): string |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function phpType(): string |
||
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | */ |
||
251 | public function getSize(): int |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function getPrecision(): int |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function getScale(): int |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | public function isNullable(): bool |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function hasDefaultValue(): bool |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function getDefaultValue() |
||
323 | |||
324 | /** |
||
325 | * Get every associated column constraint names. |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | public function getConstraints(): array |
||
333 | |||
334 | /** |
||
335 | * Get allowed enum values. |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | public function getEnumValues(): array |
||
343 | |||
344 | /** |
||
345 | * DBMS specific reverse mapping must map database specific type into limited set of abstract |
||
346 | * types. |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | public function abstractType(): string |
||
382 | |||
383 | //--- MODIFICATIONS IS HERE |
||
384 | |||
385 | |||
386 | /** |
||
387 | * Give column new abstract type. DBMS specific implementation must map provided type into one |
||
388 | * of internal database values. |
||
389 | * |
||
390 | * Attention, changing type of existed columns in some databases has a lot of restrictions like |
||
391 | * cross type conversions and etc. Try do not change column type without a reason. |
||
392 | * |
||
393 | * @param string $abstract Abstract or virtual type declared in mapping. |
||
394 | * |
||
395 | * @return self|$this |
||
396 | * |
||
397 | * @throws SchemaException |
||
398 | */ |
||
399 | public function setType(string $abstract): AbstractColumn |
||
426 | |||
427 | /** |
||
428 | * Set column nullable/not nullable. |
||
429 | * |
||
430 | * @param bool $nullable |
||
431 | * |
||
432 | * @return self|$this |
||
433 | */ |
||
434 | public function nullable(bool $nullable = true): AbstractColumn |
||
440 | |||
441 | /** |
||
442 | * Change column default value (can be forbidden for some column types). |
||
443 | * Use Database::TIMESTAMP_NOW to use driver specific NOW() function. |
||
444 | * |
||
445 | * @param mixed $value |
||
446 | * |
||
447 | * @return self|$this |
||
448 | */ |
||
449 | public function defaultValue($value): AbstractColumn |
||
461 | |||
462 | /** |
||
463 | * Set column as enum type and specify set of allowed values. Most of drivers will emulate enums |
||
464 | * using column constraints. |
||
465 | * |
||
466 | * Examples: |
||
467 | * $table->status->enum(['active', 'disabled']); |
||
468 | * $table->status->enum('active', 'disabled'); |
||
469 | * |
||
470 | * @param string|array $values Enum values (array or comma separated). String values only. |
||
471 | * |
||
472 | * @return self |
||
473 | */ |
||
474 | public function enum($values): AbstractColumn |
||
481 | |||
482 | /** |
||
483 | * Set column type as string with limited size. Maximum allowed size is 255 bytes, use "text" |
||
484 | * abstract types for longer strings. |
||
485 | * |
||
486 | * Strings are perfect type to store email addresses as it big enough to store valid address |
||
487 | * and |
||
488 | * can be covered with unique index. |
||
489 | * |
||
490 | * @link http://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address |
||
491 | * |
||
492 | * @param int $size Max string length. |
||
493 | * |
||
494 | * @return self|$this |
||
495 | * |
||
496 | * @throws SchemaException |
||
497 | */ |
||
498 | public function string(int $size = 255): AbstractColumn |
||
514 | |||
515 | /** |
||
516 | * Set column type as decimal with specific precision and scale. |
||
517 | * |
||
518 | * @param int $precision |
||
519 | * @param int $scale |
||
520 | * |
||
521 | * @return self|$this |
||
522 | * |
||
523 | * @throws SchemaException |
||
524 | */ |
||
525 | public function decimal(int $precision, int $scale = 0): AbstractColumn |
||
538 | |||
539 | /** |
||
540 | * Shortcut for AbstractColumn->type() method. |
||
541 | * |
||
542 | * @param string $type Abstract type. |
||
543 | * @param array $arguments Not used. |
||
544 | * |
||
545 | * @return self |
||
546 | */ |
||
547 | public function __call(string $type, array $arguments = []): AbstractColumn |
||
551 | |||
552 | /** |
||
553 | * {@inheritdoc} |
||
554 | */ |
||
555 | public function compare(ColumnInterface $initial): bool |
||
585 | |||
586 | /** |
||
587 | * {@inheritdoc} |
||
588 | */ |
||
589 | public function sqlStatement(Driver $driver): string |
||
612 | |||
613 | |||
614 | /** |
||
615 | * Simplified way to dump information. |
||
616 | * |
||
617 | * @return array |
||
618 | */ |
||
619 | public function __debugInfo() |
||
653 | |||
654 | /** |
||
655 | * Get database specific enum type definition options. |
||
656 | * |
||
657 | * @param Driver $driver |
||
658 | * |
||
659 | * @return string. |
||
660 | */ |
||
661 | protected function prepareEnum(Driver $driver): string |
||
674 | |||
675 | /** |
||
676 | * Must return driver specific default value. |
||
677 | * |
||
678 | * @param Driver $driver |
||
679 | * |
||
680 | * @return string |
||
681 | */ |
||
682 | protected function prepareDefault(Driver $driver): string |
||
706 | } |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.