Complex classes like Variant 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 Variant, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Variant extends Model |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $productId; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $title; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var float |
||
| 30 | */ |
||
| 31 | protected $price; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $sku; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $position; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | protected $grams; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $inventoryPolicy; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $compareAtPrice; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $fulfillmentService; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $inventoryManagement; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $option1; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $option2; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $option3; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $createdAt; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $updatedAt; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | protected $taxable; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $barcode; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | protected $imageId; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var int |
||
| 110 | */ |
||
| 111 | protected $inventoryQuantity; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var float |
||
| 115 | */ |
||
| 116 | protected $weight; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | protected $weightUnit; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var int |
||
| 125 | */ |
||
| 126 | protected $inventoryItemId; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var int |
||
| 130 | */ |
||
| 131 | protected $oldInventoryQuantity; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var bool |
||
| 135 | */ |
||
| 136 | protected $requiresShipping; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var string |
||
| 140 | */ |
||
| 141 | protected $adminGraphqlApiId; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return int |
||
| 145 | */ |
||
| 146 | public function getProductId() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param int $productId |
||
| 153 | * |
||
| 154 | * @return Variant |
||
| 155 | */ |
||
| 156 | public function setProductId($productId) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getTitle() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $title |
||
| 173 | * |
||
| 174 | * @return Variant |
||
| 175 | */ |
||
| 176 | public function setTitle($title) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return float |
||
| 185 | */ |
||
| 186 | public function getPrice() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param float $price |
||
| 193 | * |
||
| 194 | * @return Variant |
||
| 195 | */ |
||
| 196 | public function setPrice($price) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getSku() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $sku |
||
| 213 | * |
||
| 214 | * @return Variant |
||
| 215 | */ |
||
| 216 | public function setSku($sku) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | public function getPosition() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param int $position |
||
| 233 | * |
||
| 234 | * @return Variant |
||
| 235 | */ |
||
| 236 | public function setPosition($position) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function getGrams() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param int $grams |
||
| 253 | * |
||
| 254 | * @return Variant |
||
| 255 | */ |
||
| 256 | public function setGrams($grams) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function getInventoryPolicy() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $inventoryPolicy |
||
| 273 | * |
||
| 274 | * @return Variant |
||
| 275 | */ |
||
| 276 | public function setInventoryPolicy($inventoryPolicy) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getCompareAtPrice() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $compareAtPrice |
||
| 293 | * |
||
| 294 | * @return Variant |
||
| 295 | */ |
||
| 296 | public function setCompareAtPrice($compareAtPrice) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getFulfillmentService() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $fulfillmentService |
||
| 313 | * |
||
| 314 | * @return Variant |
||
| 315 | */ |
||
| 316 | public function setFulfillmentService($fulfillmentService) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | public function getInventoryManagement() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $inventoryManagement |
||
| 333 | * |
||
| 334 | * @return Variant |
||
| 335 | */ |
||
| 336 | public function setInventoryManagement($inventoryManagement) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function getOption1() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $option1 |
||
| 353 | * |
||
| 354 | * @return Variant |
||
| 355 | */ |
||
| 356 | public function setOption1($option1) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | public function getOption2() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $option2 |
||
| 373 | * |
||
| 374 | * @return Variant |
||
| 375 | */ |
||
| 376 | public function setOption2($option2) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function getOption3() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $option3 |
||
| 393 | * |
||
| 394 | * @return Variant |
||
| 395 | */ |
||
| 396 | public function setOption3($option3) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getCreatedAt() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $createdAt |
||
| 413 | * |
||
| 414 | * @return Variant |
||
| 415 | */ |
||
| 416 | public function setCreatedAt($createdAt) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getUpdatedAt() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $updatedAt |
||
| 433 | * |
||
| 434 | * @return Variant |
||
| 435 | */ |
||
| 436 | public function setUpdatedAt($updatedAt) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function isTaxable() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param bool $taxable |
||
| 453 | * |
||
| 454 | * @return Variant |
||
| 455 | */ |
||
| 456 | public function setTaxable($taxable) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | public function getBarcode() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param string $barcode |
||
| 473 | * |
||
| 474 | * @return Variant |
||
| 475 | */ |
||
| 476 | public function setBarcode($barcode) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @return int |
||
| 485 | */ |
||
| 486 | public function getImageId() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param int $imageId |
||
| 493 | * |
||
| 494 | * @return Variant |
||
| 495 | */ |
||
| 496 | public function setImageId($imageId) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @return int |
||
| 505 | */ |
||
| 506 | public function getInventoryQuantity() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param int $inventoryQuantity |
||
| 513 | * |
||
| 514 | * @return Variant |
||
| 515 | */ |
||
| 516 | public function setInventoryQuantity($inventoryQuantity) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return float |
||
| 525 | */ |
||
| 526 | public function getWeight() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param float $weight |
||
| 533 | * |
||
| 534 | * @return Variant |
||
| 535 | */ |
||
| 536 | public function setWeight($weight) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | public function getWeightUnit() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param string $weightUnit |
||
| 553 | * |
||
| 554 | * @return Variant |
||
| 555 | */ |
||
| 556 | public function setWeightUnit($weightUnit) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param int $inventoryItemId |
||
| 565 | */ |
||
| 566 | public function setInventoryItemId($inventoryItemId) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @return int |
||
| 573 | */ |
||
| 574 | public function getInventoryItemId() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @return int |
||
| 581 | */ |
||
| 582 | public function getOldInventoryQuantity() |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @param int $oldInventoryQuantity |
||
| 589 | * |
||
| 590 | * @return Variant |
||
| 591 | */ |
||
| 592 | public function setOldInventoryQuantity($oldInventoryQuantity) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | public function isRequiresShipping() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @param bool $requiresShipping |
||
| 609 | * |
||
| 610 | * @return Variant |
||
| 611 | */ |
||
| 612 | public function setRequiresShipping($requiresShipping) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @param string $adminGraphqlApiId |
||
| 621 | */ |
||
| 622 | public function setAdminGraphqlApiId($adminGraphqlApiId) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | public function getAdminGraphqlApiId() |
||
| 634 | } |