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|BaseTaxonInterface[] |
||
| 36 | */ |
||
| 37 | protected $taxons; |
||
| 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 getTaxons($rootTaxonCode = null) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function addTaxon(BaseTaxonInterface $taxon) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function removeTaxon(BaseTaxonInterface $taxon) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function hasTaxon(BaseTaxonInterface $taxon) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function getShippingCategory() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function setShippingCategory(ShippingCategoryInterface $category = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | public function getChannels() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function addChannel(BaseChannelInterface $channel) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritdoc} |
||
| 197 | */ |
||
| 198 | public function removeChannel(BaseChannelInterface $channel) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | public function hasChannel(BaseChannelInterface $channel) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public static function getVariantSelectionMethodLabels() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function getShortDescription() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function setShortDescription($shortDescription) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | public function getMainTaxon() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function setMainTaxon(TaxonInterface $mainTaxon = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | public function getReviews() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function addReview(ReviewInterface $review) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function removeReview(ReviewInterface $review) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function getAverageRating() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * {@inheritdoc} |
||
| 290 | */ |
||
| 291 | public function setAverageRating($averageRating) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | public function getPrice() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function getImages() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function getImageByCode($code) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | public function hasImages() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | public function hasImage(ImageInterface $image) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * {@inheritdoc} |
||
| 348 | */ |
||
| 349 | public function addImage(ImageInterface $image) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | public function removeImage(ImageInterface $image) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * {@inheritdoc} |
||
| 368 | */ |
||
| 369 | protected function createTranslation() |
||
| 373 | } |
||
| 374 |