| Total Complexity | 55 |
| Total Lines | 452 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Column 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.
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 Column, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Column extends Base |
||
| 14 | { |
||
| 15 | use MapperAwareTrait; |
||
| 16 | |||
| 17 | const TYPE_VARCHAR = 'varchar'; |
||
| 18 | const TYPE_FLOAT = 'float'; |
||
| 19 | const TYPE_DECIMAL = 'decimal'; |
||
| 20 | const TYPE_INTEGER = 'integer'; |
||
| 21 | const TYPE_SMALL_INTEGER = 'small integer'; |
||
| 22 | const TYPE_TINY_INTEGER = 'tiny integer'; |
||
| 23 | const TYPE_BIG_INTEGER = 'big integer'; |
||
| 24 | const TYPE_DATE = 'date'; |
||
| 25 | const TYPE_DATETIME = 'datetime'; |
||
| 26 | const TYPE_TIMESTAMP = 'timestamp'; |
||
| 27 | const TYPE_TEXT = 'text'; |
||
| 28 | const TYPE_JSON = 'json'; |
||
| 29 | const TYPE_BOOLEAN = 'bool'; |
||
| 30 | |||
| 31 | protected $name; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Previous name of the column |
||
| 35 | * Usable by DB migration builders |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $previousName; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * This should be used in case a column does not have the same name as |
||
| 42 | * the name of the attribute in the entity |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $attributeName; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $attributeCast; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $attributeType; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Indexed column, usable by DB migration builder |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $index = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Unique index column, usable by DB migration builder |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $unique = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set column as auto incremented, usable by DB migration builder |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | protected $autoIncrement = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * For columns of type number |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $unsigned = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Default column value, usable by DB migration builder |
||
| 83 | * @var mixed |
||
| 84 | */ |
||
| 85 | protected $default; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The name of the column after which this is positioned, |
||
| 89 | * usable by DB migration builder |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $after; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Type of the column (integer, date, string etc) |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $type; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * For decimal type columns |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $digits = 14; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * For decimal type columns |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | protected $precision = 2; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * For varchar type columns |
||
| 114 | * @var int |
||
| 115 | */ |
||
| 116 | protected $length = 255; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Is the column nullable? |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | protected $nullable = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var ColumnObserver |
||
| 126 | */ |
||
| 127 | protected $observer; |
||
| 128 | |||
| 129 | public static function make(string $name) |
||
| 130 | { |
||
| 131 | return (new static)->setName($name); |
||
| 132 | } |
||
| 133 | |||
| 134 | public static function varchar(string $name, $length = 255) |
||
| 135 | { |
||
| 136 | return static::make($name) |
||
| 137 | ->setType(static::TYPE_VARCHAR) |
||
| 138 | ->setLength($length); |
||
| 139 | } |
||
| 140 | |||
| 141 | public static function bool(string $name) |
||
| 142 | { |
||
| 143 | return static::make($name) |
||
| 144 | ->setType(static::TYPE_BOOLEAN); |
||
| 145 | } |
||
| 146 | |||
| 147 | public static function string(string $name) |
||
| 148 | { |
||
| 149 | return static::make($name) |
||
| 150 | ->setType(static::TYPE_TEXT); |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function datetime(string $name) |
||
| 154 | { |
||
| 155 | return static::make($name) |
||
| 156 | ->setType(static::TYPE_DATETIME); |
||
| 157 | } |
||
| 158 | |||
| 159 | public static function date(string $name) |
||
| 160 | { |
||
| 161 | return static::make($name) |
||
| 162 | ->setType(static::TYPE_DATE); |
||
| 163 | } |
||
| 164 | |||
| 165 | public static function timestamp(string $name) |
||
| 166 | { |
||
| 167 | return static::make($name) |
||
| 168 | ->setType(static::TYPE_TIMESTAMP); |
||
| 169 | } |
||
| 170 | |||
| 171 | public static function json(string $name) |
||
| 172 | { |
||
| 173 | return static::make($name) |
||
| 174 | ->setType(static::TYPE_JSON); |
||
| 175 | } |
||
| 176 | |||
| 177 | public static function float(string $name) |
||
| 178 | { |
||
| 179 | return static::make($name) |
||
| 180 | ->setType(static::TYPE_FLOAT); |
||
| 181 | } |
||
| 182 | |||
| 183 | public static function integer(string $name, $unsigned = false) |
||
| 184 | { |
||
| 185 | return static::make($name) |
||
| 186 | ->setType(static::TYPE_INTEGER) |
||
| 187 | ->setUnsigned($unsigned); |
||
| 188 | } |
||
| 189 | |||
| 190 | public static function tinyInteger(string $name, $unsigned = false) |
||
| 191 | { |
||
| 192 | return static::make($name) |
||
| 193 | ->setType(static::TYPE_TINY_INTEGER) |
||
| 194 | ->setUnsigned($unsigned); |
||
| 195 | } |
||
| 196 | |||
| 197 | public static function smallInteger(string $name, $unsigned = false) |
||
| 198 | { |
||
| 199 | return static::make($name) |
||
| 200 | ->setType(static::TYPE_SMALL_INTEGER) |
||
| 201 | ->setUnsigned($unsigned); |
||
| 202 | } |
||
| 203 | |||
| 204 | public static function bigInteger(string $name, $unsigned = false) |
||
| 205 | { |
||
| 206 | return static::make($name) |
||
| 207 | ->setType(static::TYPE_BIG_INTEGER) |
||
| 208 | ->setUnsigned($unsigned); |
||
| 209 | } |
||
| 210 | |||
| 211 | public static function decimal(string $name, int $digits, int $precision) |
||
| 212 | { |
||
| 213 | return static::make($name) |
||
| 214 | ->setType(static::TYPE_DECIMAL) |
||
| 215 | ->setDigits($digits) |
||
| 216 | ->setPrecision($precision); |
||
| 217 | } |
||
| 218 | |||
| 219 | public function getErrors(): array |
||
| 220 | { |
||
| 221 | $errors = []; |
||
| 222 | |||
| 223 | if (! $this->name) { |
||
| 224 | $errors[] = 'Column requires a name'; |
||
| 225 | } |
||
| 226 | |||
| 227 | if (! $this->type) { |
||
| 228 | $errors[] = 'Column requires a type'; |
||
| 229 | } elseif (($type = $this->getConstantByValue($this->type)) && substr($type, 0, 5) !== 'TYPE_') { |
||
| 230 | $errors[] = sprintf('Column does not have a valid type (%s)', $this->type); |
||
| 231 | } |
||
| 232 | |||
| 233 | return $errors; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function getObservers(): array |
||
| 237 | { |
||
| 238 | $observer = $this->getObserver()->with($this); |
||
| 239 | |||
| 240 | return [ |
||
| 241 | $this->mapper->getName() . '_mapper_config' => [$observer], |
||
| 242 | $this->mapper->getName() . '_base_entity' => [$observer], |
||
| 243 | ]; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function getName(): string |
||
| 247 | { |
||
| 248 | return $this->name; |
||
| 249 | } |
||
| 250 | |||
| 251 | public function setName(string $name): Column |
||
| 252 | { |
||
| 253 | $this->name = $name; |
||
| 254 | |||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function getPreviousName(): ?string |
||
| 259 | { |
||
| 260 | return $this->previousName; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function setPreviousName($previousName): Column |
||
| 264 | { |
||
| 265 | $this->previousName = $previousName; |
||
| 266 | |||
| 267 | return $this; |
||
| 268 | } |
||
| 269 | |||
| 270 | public function getAttributeName(): ?string |
||
| 271 | { |
||
| 272 | return $this->attributeName; |
||
| 273 | } |
||
| 274 | |||
| 275 | public function setAttributeName(string $attributeName): Column |
||
| 276 | { |
||
| 277 | $this->attributeName = $attributeName; |
||
| 278 | |||
| 279 | return $this; |
||
| 280 | } |
||
| 281 | |||
| 282 | public function getAttributeCast(): ?string |
||
| 283 | { |
||
| 284 | return $this->attributeCast; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set the cast type (string/integer/decimal:2/etc) of the attribute. |
||
| 289 | * If not provided it is inferred from the column type |
||
| 290 | */ |
||
| 291 | public function setAttributeCast(string $attributeCast): Column |
||
| 292 | { |
||
| 293 | $this->attributeCast = $attributeCast; |
||
| 294 | |||
| 295 | return $this; |
||
| 296 | } |
||
| 297 | |||
| 298 | public function getAttributeType(): ?string |
||
| 299 | { |
||
| 300 | return $this->attributeType; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set the type of the attribute (int/float/string etc) for the entity. |
||
| 305 | * If not provided it is inferred from the column type |
||
| 306 | */ |
||
| 307 | public function setAttributeType(string $attributeType): Column |
||
| 308 | { |
||
| 309 | $this->attributeType = $attributeType; |
||
| 310 | |||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | public function getIndex(): bool |
||
| 315 | { |
||
| 316 | return $this->index; |
||
| 317 | } |
||
| 318 | |||
| 319 | public function setIndex(bool $index): Column |
||
| 320 | { |
||
| 321 | $this->index = $index; |
||
| 322 | |||
| 323 | return $this; |
||
| 324 | } |
||
| 325 | |||
| 326 | public function getUnique(): bool |
||
| 327 | { |
||
| 328 | return $this->unique; |
||
| 329 | } |
||
| 330 | |||
| 331 | public function setUnique(bool $unique): Column |
||
| 332 | { |
||
| 333 | $this->unique = $unique; |
||
| 334 | |||
| 335 | return $this; |
||
| 336 | } |
||
| 337 | |||
| 338 | public function getAutoIncrement(): bool |
||
| 339 | { |
||
| 340 | return $this->autoIncrement; |
||
| 341 | } |
||
| 342 | |||
| 343 | public function setAutoIncrement(bool $autoIncrement): Column |
||
| 344 | { |
||
| 345 | $this->autoIncrement = $autoIncrement; |
||
| 346 | |||
| 347 | return $this; |
||
| 348 | } |
||
| 349 | |||
| 350 | public function getUnsigned(): bool |
||
| 351 | { |
||
| 352 | return $this->unsigned; |
||
| 353 | } |
||
| 354 | |||
| 355 | public function setUnsigned(bool $unsigned): Column |
||
| 356 | { |
||
| 357 | $this->unsigned = $unsigned; |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | public function getDefault() |
||
| 363 | { |
||
| 364 | return $this->default; |
||
| 365 | } |
||
| 366 | |||
| 367 | public function setDefault($default) |
||
| 368 | { |
||
| 369 | $this->default = $default; |
||
| 370 | |||
| 371 | return $this; |
||
| 372 | } |
||
| 373 | |||
| 374 | public function getAfter(): ?string |
||
| 375 | { |
||
| 376 | return $this->after; |
||
| 377 | } |
||
| 378 | |||
| 379 | public function setAfter(string $after): Column |
||
| 380 | { |
||
| 381 | $this->after = $after; |
||
| 382 | |||
| 383 | return $this; |
||
| 384 | } |
||
| 385 | |||
| 386 | public function getType(): ?string |
||
| 387 | { |
||
| 388 | return $this->type; |
||
| 389 | } |
||
| 390 | |||
| 391 | public function setType(string $type): Column |
||
| 392 | { |
||
| 393 | $this->type = $type; |
||
| 394 | |||
| 395 | return $this; |
||
| 396 | } |
||
| 397 | |||
| 398 | public function getDigits(): int |
||
| 399 | { |
||
| 400 | return $this->digits; |
||
| 401 | } |
||
| 402 | |||
| 403 | public function setDigits(int $digits): Column |
||
| 404 | { |
||
| 405 | $this->digits = $digits; |
||
| 406 | |||
| 407 | return $this; |
||
| 408 | } |
||
| 409 | |||
| 410 | public function getPrecision(): int |
||
| 411 | { |
||
| 412 | return $this->precision; |
||
| 413 | } |
||
| 414 | |||
| 415 | public function setPrecision(int $precision): Column |
||
| 416 | { |
||
| 417 | $this->precision = $precision; |
||
| 418 | $this->setAttributeCast('decimal:' . $precision); |
||
| 419 | |||
| 420 | return $this; |
||
| 421 | } |
||
| 422 | |||
| 423 | public function getLength(): int |
||
| 424 | { |
||
| 425 | return $this->length; |
||
| 426 | } |
||
| 427 | |||
| 428 | public function setLength(int $length): Column |
||
| 429 | { |
||
| 430 | $this->length = $length; |
||
| 431 | |||
| 432 | return $this; |
||
| 433 | } |
||
| 434 | |||
| 435 | public function getNullable(): bool |
||
| 436 | { |
||
| 437 | return $this->nullable; |
||
| 438 | } |
||
| 439 | |||
| 440 | public function setNullable(bool $nullable): Column |
||
| 441 | { |
||
| 442 | $this->nullable = $nullable; |
||
| 443 | |||
| 444 | return $this; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return ColumnObserver |
||
| 449 | */ |
||
| 450 | public function getObserver(): ColumnObserver |
||
| 451 | { |
||
| 452 | return $this->observer ?: new ColumnObserver(); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param ColumnObserver $observer |
||
| 457 | * |
||
| 458 | * @return Column |
||
| 459 | */ |
||
| 460 | public function setObserver(ColumnObserver $observer): Column |
||
| 461 | { |
||
| 462 | $this->observer = $observer; |
||
| 463 | |||
| 464 | return $this; |
||
| 465 | } |
||
| 467 |