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 int |
||
| 28 | */ |
||
| 29 | protected $version = 1; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $onHold = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $onHand = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $tracked = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var float |
||
| 48 | */ |
||
| 49 | protected $weight; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var float |
||
| 53 | */ |
||
| 54 | protected $width; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var float |
||
| 58 | */ |
||
| 59 | protected $height; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var float |
||
| 63 | */ |
||
| 64 | protected $depth; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var TaxCategoryInterface |
||
| 68 | */ |
||
| 69 | protected $taxCategory; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var ShippingCategoryInterface |
||
| 73 | */ |
||
| 74 | protected $shippingCategory; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var Collection |
||
| 78 | */ |
||
| 79 | protected $channelPricings; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | protected $shippingRequired = true; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var Collection|ProductImageInterface[] |
||
| 88 | */ |
||
| 89 | protected $images; |
||
| 90 | |||
| 91 | public function __construct() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function __toString() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function getVersion() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | public function setVersion($version) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | public function isInStock() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function getOnHold() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | public function setOnHold($onHold) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | public function getOnHand() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritdoc} |
||
| 169 | */ |
||
| 170 | public function setOnHand($onHand) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | public function isTracked() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function setTracked($tracked) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function getInventoryName() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public function getShippingCategory() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function setShippingCategory(ShippingCategoryInterface $category = null) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public function getWeight() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function setWeight($weight) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function getWidth() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public function setWidth($width) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function getHeight() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function setHeight($height) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function getDepth() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function setDepth($depth) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | public function getShippingWeight() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function getShippingWidth() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | public function getShippingHeight() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * {@inheritdoc} |
||
| 307 | */ |
||
| 308 | public function getShippingDepth() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | public function getShippingVolume() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * {@inheritdoc} |
||
| 323 | */ |
||
| 324 | public function getTaxCategory() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | public function setTaxCategory(TaxCategoryInterface $category = null) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | public function getChannelPricings() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function getChannelPricingForChannel(ChannelInterface $channel) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * {@inheritdoc} |
||
| 359 | */ |
||
| 360 | public function hasChannelPricingForChannel(ChannelInterface $channel) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * {@inheritdoc} |
||
| 367 | */ |
||
| 368 | public function hasChannelPricing(ChannelPricingInterface $channelPricing) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritdoc} |
||
| 375 | */ |
||
| 376 | public function addChannelPricing(ChannelPricingInterface $channelPricing) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * {@inheritdoc} |
||
| 386 | */ |
||
| 387 | public function removeChannelPricing(ChannelPricingInterface $channelPricing) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * {@inheritdoc} |
||
| 397 | */ |
||
| 398 | public function isShippingRequired() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * {@inheritdoc} |
||
| 405 | */ |
||
| 406 | public function setShippingRequired($shippingRequired) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * {@inheritdoc} |
||
| 413 | */ |
||
| 414 | public function getImages() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritdoc} |
||
| 421 | */ |
||
| 422 | public function getImagesByType($type) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * {@inheritdoc} |
||
| 431 | */ |
||
| 432 | public function hasImages() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * {@inheritdoc} |
||
| 439 | */ |
||
| 440 | public function hasImage(ProductImageInterface $image) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * {@inheritdoc} |
||
| 447 | */ |
||
| 448 | public function addImage(ProductImageInterface $image) |
||
| 449 | { |
||
| 450 | if ($this->hasImage($image)) { |
||
| 451 | return; |
||
| 452 | } |
||
| 453 | $image->setOwner($this->getProduct()); |
||
| 454 | $image->addProductVariant($this); |
||
| 455 | $this->images->add($image); |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * {@inheritdoc} |
||
| 460 | */ |
||
| 461 | public function removeImage(ProductImageInterface $image) |
||
| 467 | } |
||
| 468 |