| Total Complexity | 41 |
| Total Lines | 443 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CreateTable 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 CreateTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class CreateTable |
||
| 54 | { |
||
|
|
|||
| 55 | |||
| 56 | /** |
||
| 57 | * The list of CreateColumn |
||
| 58 | * @var array<string, CreateColumn> |
||
| 59 | */ |
||
| 60 | protected array $columns = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The primary or list of primary key |
||
| 64 | * @var string|array<string, mixed> |
||
| 65 | */ |
||
| 66 | protected $primaryKey; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The list of unique keys |
||
| 70 | * @var array<string, array<int, mixed>> |
||
| 71 | */ |
||
| 72 | protected array $uniqueKeys = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The list of indexes keys |
||
| 76 | * @var array<string, array<int, mixed>> |
||
| 77 | */ |
||
| 78 | protected array $indexes = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The list of ForeignKey |
||
| 82 | * @var array<string, ForeignKey> |
||
| 83 | */ |
||
| 84 | protected array $foreignKeys = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The name of the table |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected string $table; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The engine for the table |
||
| 94 | * @var string|null |
||
| 95 | */ |
||
| 96 | protected ?string $engine = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The auto increment name |
||
| 100 | * @var bool|null |
||
| 101 | */ |
||
| 102 | protected ?bool $autoincrement = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Class constructor |
||
| 106 | * @param string $table |
||
| 107 | */ |
||
| 108 | public function __construct(string $table) |
||
| 109 | { |
||
| 110 | $this->table = $table; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getTableName(): string |
||
| 118 | { |
||
| 119 | return $this->table; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * |
||
| 124 | * @return array<string, CreateColumn> |
||
| 125 | */ |
||
| 126 | public function getColumns(): array |
||
| 127 | { |
||
| 128 | return $this->columns; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * |
||
| 133 | * @return mixed |
||
| 134 | */ |
||
| 135 | public function getPrimaryKey() |
||
| 136 | { |
||
| 137 | return $this->primaryKey; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * |
||
| 142 | * @return array<string, array<int, mixed>> |
||
| 143 | */ |
||
| 144 | public function getUniqueKeys(): array |
||
| 145 | { |
||
| 146 | return $this->uniqueKeys; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * |
||
| 151 | * @return array<string, array<int, mixed>> |
||
| 152 | */ |
||
| 153 | public function getIndexes(): array |
||
| 154 | { |
||
| 155 | return $this->indexes; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * |
||
| 160 | * @return array<string, ForeignKey> |
||
| 161 | */ |
||
| 162 | public function getForeignKeys(): array |
||
| 163 | { |
||
| 164 | return $this->foreignKeys; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * |
||
| 169 | * @return string|null |
||
| 170 | */ |
||
| 171 | public function getEngine(): ?string |
||
| 172 | { |
||
| 173 | return $this->engine; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * |
||
| 178 | * @return bool|null |
||
| 179 | */ |
||
| 180 | public function getAutoincrement(): ?bool |
||
| 181 | { |
||
| 182 | return $this->autoincrement; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * |
||
| 187 | * @param string|null $name |
||
| 188 | * @return self |
||
| 189 | */ |
||
| 190 | public function engine(?string $name): self |
||
| 191 | { |
||
| 192 | $this->engine = $name; |
||
| 193 | |||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * |
||
| 199 | * @param string|array<int, string> $columns |
||
| 200 | * @param string|null $name |
||
| 201 | * @return self |
||
| 202 | */ |
||
| 203 | public function primary($columns, ?string $name = null): self |
||
| 204 | { |
||
| 205 | if (!is_array($columns)) { |
||
| 206 | $columns = [$columns]; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($name === null) { |
||
| 210 | $name = $this->table . '_pk_' . implode('_', $columns); |
||
| 211 | } |
||
| 212 | |||
| 213 | $this->primaryKey = [ |
||
| 214 | 'name' => $name, |
||
| 215 | 'columns' => $columns |
||
| 216 | ]; |
||
| 217 | |||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * |
||
| 223 | * @param string|array<int, string> $columns |
||
| 224 | * @param string|null $name |
||
| 225 | * @return self |
||
| 226 | */ |
||
| 227 | public function unique($columns, ?string $name = null): self |
||
| 228 | { |
||
| 229 | if (!is_array($columns)) { |
||
| 230 | $columns = [$columns]; |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($name === null) { |
||
| 234 | $name = $this->table . '_uk_' . implode('_', $columns); |
||
| 235 | } |
||
| 236 | |||
| 237 | $this->uniqueKeys[$name] = $columns; |
||
| 238 | |||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * |
||
| 244 | * @param string|array<int, string> $columns |
||
| 245 | * @param string|null $name |
||
| 246 | * @return self |
||
| 247 | */ |
||
| 248 | public function index($columns, ?string $name = null): self |
||
| 249 | { |
||
| 250 | if (!is_array($columns)) { |
||
| 251 | $columns = [$columns]; |
||
| 252 | } |
||
| 253 | |||
| 254 | if ($name === null) { |
||
| 255 | $name = $this->table . '_ik_' . implode('_', $columns); |
||
| 256 | } |
||
| 257 | |||
| 258 | $this->indexes[$name] = $columns; |
||
| 259 | |||
| 260 | return $this; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * |
||
| 265 | * @param string|array<int, string> $columns |
||
| 266 | * @param string|null $name |
||
| 267 | * @return ForeignKey |
||
| 268 | */ |
||
| 269 | public function foreign($columns, ?string $name = null): ForeignKey |
||
| 270 | { |
||
| 271 | if (!is_array($columns)) { |
||
| 272 | $columns = [$columns]; |
||
| 273 | } |
||
| 274 | |||
| 275 | if ($name === null) { |
||
| 276 | $name = $this->table . '_fk_' . implode('_', $columns); |
||
| 277 | } |
||
| 278 | |||
| 279 | return $this->foreignKeys[$name] = new ForeignKey($columns); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * |
||
| 284 | * @param CreateColumn $column |
||
| 285 | * @param string|null $name |
||
| 286 | * @return self |
||
| 287 | */ |
||
| 288 | public function autoincrement(CreateColumn $column, ?string $name = null): self |
||
| 289 | { |
||
| 290 | if ($column->getType() !== 'integer') { |
||
| 291 | return $this; |
||
| 292 | } |
||
| 293 | |||
| 294 | $this->autoincrement = true; |
||
| 295 | |||
| 296 | $column->set('autoincrement', true); |
||
| 297 | |||
| 298 | return $this->primary($column->getName(), $name); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * |
||
| 303 | * @param string $name |
||
| 304 | * @return CreateColumn |
||
| 305 | */ |
||
| 306 | public function integer(string $name): CreateColumn |
||
| 307 | { |
||
| 308 | return $this->addColumn($name, 'integer'); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * |
||
| 313 | * @param string $name |
||
| 314 | * @return CreateColumn |
||
| 315 | */ |
||
| 316 | public function float(string $name): CreateColumn |
||
| 317 | { |
||
| 318 | return $this->addColumn($name, 'float'); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * |
||
| 323 | * @param string $name |
||
| 324 | * @return CreateColumn |
||
| 325 | */ |
||
| 326 | public function double(string $name): CreateColumn |
||
| 327 | { |
||
| 328 | return $this->addColumn($name, 'double'); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * |
||
| 333 | * @param string $name |
||
| 334 | * @param int|null $length |
||
| 335 | * @param int|null $precision |
||
| 336 | * @return CreateColumn |
||
| 337 | */ |
||
| 338 | public function decimal( |
||
| 339 | string $name, |
||
| 340 | ?int $length = null, |
||
| 341 | ?int $precision = null |
||
| 342 | ): CreateColumn { |
||
| 343 | return $this->addColumn($name, 'decimal') |
||
| 344 | ->length($length) |
||
| 345 | ->set('precision', $precision); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * |
||
| 350 | * @param string $name |
||
| 351 | * @return CreateColumn |
||
| 352 | */ |
||
| 353 | public function boolean(string $name): CreateColumn |
||
| 354 | { |
||
| 355 | return $this->addColumn($name, 'boolean'); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * |
||
| 360 | * @param string $name |
||
| 361 | * @return CreateColumn |
||
| 362 | */ |
||
| 363 | public function binary(string $name): CreateColumn |
||
| 364 | { |
||
| 365 | return $this->addColumn($name, 'binary'); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * |
||
| 370 | * @param string $name |
||
| 371 | * @param int $length |
||
| 372 | * @return CreateColumn |
||
| 373 | */ |
||
| 374 | public function string(string $name, int $length = 255): CreateColumn |
||
| 375 | { |
||
| 376 | return $this->addColumn($name, 'string') |
||
| 377 | ->length($length); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * |
||
| 382 | * @param string $name |
||
| 383 | * @param int $length |
||
| 384 | * @return CreateColumn |
||
| 385 | */ |
||
| 386 | public function fixed(string $name, int $length = 255): CreateColumn |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * |
||
| 394 | * @param string $name |
||
| 395 | * @param array<mixed> $values |
||
| 396 | * @return CreateColumn |
||
| 397 | */ |
||
| 398 | public function enum( |
||
| 399 | string $name, |
||
| 400 | array $values |
||
| 401 | ): CreateColumn { |
||
| 402 | return $this->addColumn($name, 'enum') |
||
| 403 | ->set('values', $values); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * |
||
| 408 | * @param string $name |
||
| 409 | * @return CreateColumn |
||
| 410 | */ |
||
| 411 | public function text(string $name): CreateColumn |
||
| 412 | { |
||
| 413 | return $this->addColumn($name, 'text'); |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * |
||
| 418 | * @param string $name |
||
| 419 | * @return CreateColumn |
||
| 420 | */ |
||
| 421 | public function time(string $name): CreateColumn |
||
| 422 | { |
||
| 423 | return $this->addColumn($name, 'time'); |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * |
||
| 428 | * @param string $name |
||
| 429 | * @return CreateColumn |
||
| 430 | */ |
||
| 431 | public function timestamp(string $name): CreateColumn |
||
| 432 | { |
||
| 433 | return $this->addColumn($name, 'timestamp'); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * |
||
| 438 | * @param string $name |
||
| 439 | * @return CreateColumn |
||
| 440 | */ |
||
| 441 | public function date(string $name): CreateColumn |
||
| 442 | { |
||
| 443 | return $this->addColumn($name, 'date'); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * |
||
| 448 | * @param string $name |
||
| 449 | * @return CreateColumn |
||
| 450 | */ |
||
| 451 | public function datetime(string $name): CreateColumn |
||
| 452 | { |
||
| 453 | return $this->addColumn($name, 'datetime'); |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * |
||
| 458 | * @param string $column |
||
| 459 | * @return self |
||
| 460 | */ |
||
| 461 | public function softDelete(string $column = 'deleted_at'): self |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * |
||
| 470 | * @param string $createColumn |
||
| 471 | * @param string $updateColumn |
||
| 472 | * @return self |
||
| 473 | */ |
||
| 474 | public function timestamps( |
||
| 475 | string $createColumn = 'created_at', |
||
| 476 | string $updateColumn = 'updated_at' |
||
| 477 | ): self { |
||
| 478 | $this->datetime($createColumn)->notNull(); |
||
| 479 | $this->datetime($updateColumn); |
||
| 480 | |||
| 481 | return $this; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * |
||
| 486 | * @param string $name |
||
| 487 | * @param string $type |
||
| 488 | * @return CreateColumn |
||
| 489 | */ |
||
| 490 | protected function addColumn(string $name, string $type): CreateColumn |
||
| 496 | } |
||
| 497 | } |
||
| 498 |