| Total Complexity | 43 |
| Total Lines | 459 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Schema |
||
| 26 | { |
||
| 27 | /** @var array<Table> */ |
||
| 28 | private array $tables; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param array<Table> $tables |
||
| 32 | */ |
||
| 33 | public function __construct(array $tables = []) { |
||
| 34 | foreach ($tables as $table) { |
||
| 35 | $this->tables[$table->getName()] = $table; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return array<Table> |
||
| 41 | */ |
||
| 42 | public function getTables(): array |
||
| 43 | { |
||
| 44 | return $this->tables; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $name |
||
| 49 | * @param array<ColumnInterface|PrimaryKey|UniqueIndex|Index|ForeignKey> $components |
||
| 50 | * |
||
| 51 | * @return Table |
||
| 52 | */ |
||
| 53 | public static function table(string $name, array $components): Table |
||
| 54 | { |
||
| 55 | return new Table(name: $name, components: $components); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $name |
||
| 60 | * |
||
| 61 | * @return IntegerColumn |
||
| 62 | */ |
||
| 63 | public static function bigInteger(string $name): IntegerColumn |
||
| 64 | { |
||
| 65 | return new IntegerColumn(name: $name, bits: 64); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $name |
||
| 70 | * @param int $length |
||
| 71 | * |
||
| 72 | * @return BinaryColumn |
||
| 73 | */ |
||
| 74 | public static function binary(string $name, int $length): BinaryColumn |
||
| 75 | { |
||
| 76 | return new BinaryColumn(name: $name, length: $length, varying: false); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $name |
||
| 81 | * @param int $bits |
||
| 82 | * |
||
| 83 | * @return IntegerColumn |
||
| 84 | */ |
||
| 85 | public static function bit(string $name, int $bits = 1): IntegerColumn |
||
| 86 | { |
||
| 87 | return new IntegerColumn(name: $name, bits: $bits); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $name |
||
| 92 | * @param int $length |
||
| 93 | * |
||
| 94 | * @return BlobColumn |
||
| 95 | */ |
||
| 96 | public static function blob(string $name, int $length = 4): BlobColumn |
||
| 97 | { |
||
| 98 | return new BlobColumn(name: $name, length: $length); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $name |
||
| 103 | * |
||
| 104 | * @return BooleanColumn |
||
| 105 | */ |
||
| 106 | public static function boolean(string $name): BooleanColumn |
||
| 107 | { |
||
| 108 | return new BooleanColumn(name: $name); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $name |
||
| 113 | * @param int $length |
||
| 114 | * |
||
| 115 | * @return CharacterColumn |
||
| 116 | */ |
||
| 117 | public static function char(string $name, int $length): CharacterColumn |
||
| 118 | { |
||
| 119 | return new CharacterColumn(name: $name, length: $length, varying: false, national: false); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $name |
||
| 124 | * |
||
| 125 | * @return DateColumn |
||
| 126 | */ |
||
| 127 | public static function date(string $name): DateColumn |
||
| 128 | { |
||
| 129 | return new DateColumn(name: $name); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $name |
||
| 134 | * @param int $length |
||
| 135 | * |
||
| 136 | * @return DatetimeColumn |
||
| 137 | */ |
||
| 138 | public static function datetime(string $name, int $length = 0): DatetimeColumn |
||
| 139 | { |
||
| 140 | return new DatetimeColumn(name: $name, length: $length); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $name |
||
| 145 | * @param int $precision |
||
| 146 | * @param int $scale |
||
| 147 | * |
||
| 148 | * @return DecimalColumn |
||
| 149 | */ |
||
| 150 | public static function decimal(string $name, int $precision, int $scale): DecimalColumn |
||
| 151 | { |
||
| 152 | return new DecimalColumn(name: $name, precision: $precision, scale: $scale); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $name |
||
| 157 | * @param int $precision_bits |
||
| 158 | * |
||
| 159 | * @return FloatColumn |
||
| 160 | */ |
||
| 161 | public static function double(string $name, int $precision_bits = 53): FloatColumn |
||
| 162 | { |
||
| 163 | return new FloatColumn(name: $name, precision_bits: $precision_bits); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $name |
||
| 168 | * @param array<string> $values |
||
| 169 | * |
||
| 170 | * @return EnumColumn |
||
| 171 | */ |
||
| 172 | public static function enum(string $name, array $values): EnumColumn |
||
| 173 | { |
||
| 174 | return new EnumColumn(name: $name, values: $values); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $name |
||
| 179 | * @param int $precision_bits |
||
| 180 | * |
||
| 181 | * @return FloatColumn |
||
| 182 | */ |
||
| 183 | public static function float(string $name, int $precision_bits = 23): FloatColumn |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $name |
||
| 190 | * @param int $srid |
||
| 191 | * |
||
| 192 | * @return GeometryColumn |
||
| 193 | */ |
||
| 194 | public static function geometry(string $name, int $srid = 0): GeometryColumn |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $name |
||
| 201 | * @param int $srid |
||
| 202 | * |
||
| 203 | * @return GeometrycollectionColumn |
||
| 204 | */ |
||
| 205 | public static function geometrycollection(string $name, int $srid = 0): GeometrycollectionColumn |
||
| 206 | { |
||
| 207 | return new GeometrycollectionColumn(name: $name, srid: $srid); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $name |
||
| 212 | * @param int $bits |
||
| 213 | * |
||
| 214 | * @return IntegerColumn |
||
| 215 | */ |
||
| 216 | public static function integer(string $name, int $bits = 32): IntegerColumn |
||
| 217 | { |
||
| 218 | return new IntegerColumn(name: $name, bits: $bits); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $name |
||
| 223 | * |
||
| 224 | * @return JsonColumn |
||
| 225 | */ |
||
| 226 | public static function json(string $name): JsonColumn |
||
| 227 | { |
||
| 228 | return new JsonColumn(name: $name); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $name |
||
| 233 | * @param int $srid |
||
| 234 | * |
||
| 235 | * @return LinestringColumn |
||
| 236 | */ |
||
| 237 | public static function linestring(string $name, int $srid = 0): LinestringColumn |
||
| 238 | { |
||
| 239 | return new LinestringColumn(name: $name, srid: $srid); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $name |
||
| 244 | * |
||
| 245 | * @return IntegerColumn |
||
| 246 | */ |
||
| 247 | public static function mediumInteger(string $name): IntegerColumn |
||
| 248 | { |
||
| 249 | return new IntegerColumn(name: $name, bits: 24); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $name |
||
| 254 | * @param int $srid |
||
| 255 | * |
||
| 256 | * @return MultilinestringColumn |
||
| 257 | */ |
||
| 258 | public static function multilinestring(string $name, int $srid = 0): MultilinestringColumn |
||
| 259 | { |
||
| 260 | return new MultilinestringColumn(name: $name, srid: $srid); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $name |
||
| 265 | * @param int $srid |
||
| 266 | * |
||
| 267 | * @return MultipointColumn |
||
| 268 | */ |
||
| 269 | public static function multipoint(string $name, int $srid = 0): MultipointColumn |
||
| 270 | { |
||
| 271 | return new MultipointColumn(name: $name, srid: $srid); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $name |
||
| 276 | * @param int $srid |
||
| 277 | * |
||
| 278 | * @return MultipolygonColumn |
||
| 279 | */ |
||
| 280 | public static function multipolygon(string $name, int $srid = 0): MultipolygonColumn |
||
| 281 | { |
||
| 282 | return new MultipolygonColumn(name: $name, srid: $srid); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $name |
||
| 287 | * @param int $length |
||
| 288 | * |
||
| 289 | * @return CharacterColumn |
||
| 290 | */ |
||
| 291 | public static function nChar(string $name, int $length): CharacterColumn |
||
| 292 | { |
||
| 293 | return new CharacterColumn(name: $name, length: $length, varying: false, national: true); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $name |
||
| 298 | * @param int $length |
||
| 299 | * |
||
| 300 | * @return CharacterColumn |
||
| 301 | */ |
||
| 302 | public static function nVarchar(string $name, int $length): CharacterColumn |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $name |
||
| 309 | * @param int $srid |
||
| 310 | * |
||
| 311 | * @return PointColumn |
||
| 312 | */ |
||
| 313 | public static function point(string $name, int $srid = 0): PointColumn |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $name |
||
| 320 | * @param int $srid |
||
| 321 | * |
||
| 322 | * @return PolygonColumn |
||
| 323 | */ |
||
| 324 | public static function polygon(string $name, int $srid = 0): PolygonColumn |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $name |
||
| 331 | * @param array<string> $values |
||
| 332 | * |
||
| 333 | * @return SetColumn |
||
| 334 | */ |
||
| 335 | public static function set(string $name, array $values): SetColumn |
||
| 336 | { |
||
| 337 | return new SetColumn(name: $name, values: $values); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $name |
||
| 342 | * |
||
| 343 | * @return IntegerColumn |
||
| 344 | */ |
||
| 345 | public static function smallInteger(string $name): IntegerColumn |
||
| 346 | { |
||
| 347 | return new IntegerColumn(name: $name, bits: 16); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $name |
||
| 352 | * @param int $length |
||
| 353 | * |
||
| 354 | * @return TextColumn |
||
| 355 | */ |
||
| 356 | public static function text(string $name, int $length = 4): TextColumn |
||
| 357 | { |
||
| 358 | return new TextColumn(name: $name, length: $length); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $name |
||
| 363 | * |
||
| 364 | * @return TimeColumn |
||
| 365 | */ |
||
| 366 | public static function time(string $name): TimeColumn |
||
| 367 | { |
||
| 368 | return new TimeColumn(name: $name); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $name |
||
| 373 | * @param int $length |
||
| 374 | * |
||
| 375 | * @return TimestampColumn |
||
| 376 | */ |
||
| 377 | public static function timestamp(string $name, int $length = 0): TimestampColumn |
||
| 378 | { |
||
| 379 | return new TimestampColumn(name: $name, precision: $length); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $name |
||
| 384 | * |
||
| 385 | * @return IntegerColumn |
||
| 386 | */ |
||
| 387 | public static function tinyInteger(string $name): IntegerColumn |
||
| 388 | { |
||
| 389 | return new IntegerColumn(name: $name, bits: 8); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $name |
||
| 394 | * |
||
| 395 | * @return UuidColumn |
||
| 396 | */ |
||
| 397 | public static function uuid(string $name): UuidColumn |
||
| 398 | { |
||
| 399 | return new UuidColumn(name: $name); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param string $name |
||
| 404 | * @param int $length |
||
| 405 | * |
||
| 406 | * @return BinaryColumn |
||
| 407 | */ |
||
| 408 | public static function varBinary(string $name, int $length): BinaryColumn |
||
| 409 | { |
||
| 410 | return new BinaryColumn(name: $name, length: $length, varying: true); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $name |
||
| 415 | * @param int $length |
||
| 416 | * |
||
| 417 | * @return CharacterColumn |
||
| 418 | */ |
||
| 419 | public static function varchar(string $name, int $length): CharacterColumn |
||
| 420 | { |
||
| 421 | return new CharacterColumn(name: $name, length: $length, varying: true, national: false); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $name |
||
| 426 | * |
||
| 427 | * @return YearColumn |
||
| 428 | */ |
||
| 429 | public static function year(string $name): YearColumn |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string|array<string> $columns |
||
| 436 | * @param string $name |
||
| 437 | * |
||
| 438 | * @return PrimaryKey |
||
| 439 | */ |
||
| 440 | public static function primaryKey(string|array $columns, string $name = ''): PrimaryKey |
||
| 441 | { |
||
| 442 | return new PrimaryKey(name: $name, columns: (array) $columns); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string|array<string> $columns |
||
| 447 | * @param string $name |
||
| 448 | * |
||
| 449 | * @return Index |
||
| 450 | */ |
||
| 451 | public static function index(string|array $columns, string $name = ''): Index |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param string|array<string> $columns |
||
| 458 | * @param string $name |
||
| 459 | * |
||
| 460 | * @return UniqueIndex |
||
| 461 | */ |
||
| 462 | public static function uniqueIndex(string|array $columns, string $name = ''): UniqueIndex |
||
| 463 | { |
||
| 464 | return new UniqueIndex(name: $name, columns: (array) $columns); |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string|array<string> $local_columns |
||
| 469 | * @param string $foreign_table |
||
| 470 | * @param string|array<string> $foreign_columns |
||
| 471 | * @param string $name |
||
| 472 | * |
||
| 473 | * @return ForeignKey |
||
| 474 | */ |
||
| 475 | public static function foreignKey(string|array $local_columns, string $foreign_table, string|array $foreign_columns = null, string $name = ''): ForeignKey |
||
| 484 | } |
||
| 485 | } |
||
| 486 |