Complex classes like Product 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 Product, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Product extends BaseProduct implements ProductInterface, ReviewableProductInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $variantSelectionMethod; |
||
|
|
|||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Collection|ProductTaxonInterface[] |
||
| 36 | */ |
||
| 37 | protected $productTaxons; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ShippingCategoryInterface |
||
| 41 | */ |
||
| 42 | protected $shippingCategory; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ChannelInterface[]|Collection |
||
| 46 | */ |
||
| 47 | protected $channels; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var BaseTaxonInterface |
||
| 51 | */ |
||
| 52 | protected $mainTaxon; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Collection|ReviewInterface[] |
||
| 56 | */ |
||
| 57 | protected $reviews; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var float |
||
| 61 | */ |
||
| 62 | protected $averageRating = 0; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Collection|ImageInterface[] |
||
| 66 | */ |
||
| 67 | protected $images; |
||
| 68 | |||
| 69 | public function __construct() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function getVariantSelectionMethod() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function setVariantSelectionMethod($variantSelectionMethod) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function isVariantSelectionMethodChoice() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function getVariantSelectionMethodLabel() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function getProductTaxons() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | public function addProductTaxon(ProductTaxonInterface $productTaxon) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function removeProductTaxon(ProductTaxonInterface $productTaxon) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function hasProductTaxon(ProductTaxonInterface $productTaxon) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function filterProductTaxonsByTaxon(TaxonInterface $taxon) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function getShippingCategory() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | public function setShippingCategory(ShippingCategoryInterface $category = null) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | public function getChannels() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | public function addChannel(BaseChannelInterface $channel) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | public function removeChannel(BaseChannelInterface $channel) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function hasChannel(BaseChannelInterface $channel) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public static function getVariantSelectionMethodLabels() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function getShortDescription() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function setShortDescription($shortDescription) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function getMainTaxon() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function setMainTaxon(TaxonInterface $mainTaxon = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | public function getReviews() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function addReview(ReviewInterface $review) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | public function removeReview(ReviewInterface $review) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function getAverageRating() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function setAverageRating($averageRating) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | public function getPrice() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | public function getImages() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * {@inheritdoc} |
||
| 322 | */ |
||
| 323 | public function getImageByCode($code) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritdoc} |
||
| 336 | */ |
||
| 337 | public function hasImages() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * {@inheritdoc} |
||
| 344 | */ |
||
| 345 | public function hasImage(ImageInterface $image) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | public function addImage(ImageInterface $image) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * {@inheritdoc} |
||
| 361 | */ |
||
| 362 | public function removeImage(ImageInterface $image) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * {@inheritdoc} |
||
| 372 | */ |
||
| 373 | protected function createTranslation() |
||
| 377 | } |
||
| 378 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.