Complex classes like ProductVariant 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 ProductVariant, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class ProductVariant extends BaseVariant implements ProductVariantInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $sku; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $price; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $originalPrice; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $pricingCalculator = Calculators::STANDARD; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $pricingConfiguration = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $onHold = 0; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | protected $onHand = 0; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | protected $sold = 0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | protected $availableOnDemand = true; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Collection|ProductVariantImageInterface[] |
||
| 73 | */ |
||
| 74 | protected $images; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var float |
||
| 78 | */ |
||
| 79 | protected $weight; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var float |
||
| 83 | */ |
||
| 84 | protected $width; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var float |
||
| 88 | */ |
||
| 89 | protected $height; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var float |
||
| 93 | */ |
||
| 94 | protected $depth; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var TaxCategoryInterface |
||
| 98 | */ |
||
| 99 | protected $taxCategory; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Override constructor to set on hand stock. |
||
| 103 | */ |
||
| 104 | public function __construct() |
||
| 110 | |||
| 111 | public function __toString() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | */ |
||
| 131 | public function getMetadataClassIdentifier() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | public function getMetadataIdentifier() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | */ |
||
| 147 | public function getSku() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function setSku($sku) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | public function getPrice() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | public function setPrice($price) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function setOriginalPrice($originalPrice) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public function getOriginalPrice() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public function getPricingCalculator() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | public function setPricingCalculator($calculator) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | public function getPricingConfiguration() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function setPricingConfiguration(array $configuration) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function isInStock() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | public function getOnHold() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function setOnHold($onHold) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function getOnHand() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function setOnHand($onHand) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function getSold() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function setSold($sold) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function getInventoryName() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | public function isAvailableOnDemand() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritdoc} |
||
| 320 | */ |
||
| 321 | public function setAvailableOnDemand($availableOnDemand) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | */ |
||
| 331 | public function setDefaults(BaseVariantInterface $masterVariant) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | public function getShippingCategory() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | public function hasImage(ProductVariantImageInterface $image) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * {@inheritdoc} |
||
| 358 | */ |
||
| 359 | public function getImages() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | public function getImage() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | */ |
||
| 379 | public function addImage(ProductVariantImageInterface $image) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | public function removeImage(ProductVariantImageInterface $image) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc} |
||
| 402 | */ |
||
| 403 | public function getWeight() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | */ |
||
| 411 | public function setWeight($weight) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * {@inheritdoc} |
||
| 420 | */ |
||
| 421 | public function getWidth() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | */ |
||
| 429 | public function setWidth($width) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * {@inheritdoc} |
||
| 438 | */ |
||
| 439 | public function getHeight() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * {@inheritdoc} |
||
| 446 | */ |
||
| 447 | public function setHeight($height) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritdoc} |
||
| 456 | */ |
||
| 457 | public function getDepth() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * {@inheritdoc} |
||
| 464 | */ |
||
| 465 | public function setDepth($depth) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * {@inheritdoc} |
||
| 474 | */ |
||
| 475 | public function getShippingWeight() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * {@inheritdoc} |
||
| 482 | */ |
||
| 483 | public function getShippingWidth() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * {@inheritdoc} |
||
| 490 | */ |
||
| 491 | public function getShippingHeight() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * {@inheritdoc} |
||
| 498 | */ |
||
| 499 | public function getShippingDepth() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * {@inheritdoc} |
||
| 506 | */ |
||
| 507 | public function getShippingVolume() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * {@inheritdoc} |
||
| 514 | */ |
||
| 515 | public function isPriceReduced() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * {@inheritdoc} |
||
| 522 | */ |
||
| 523 | public function getTaxCategory() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * {@inheritdoc} |
||
| 530 | */ |
||
| 531 | public function setTaxCategory(TaxCategoryInterface $category = null) |
||
| 535 | } |
||
| 536 |