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