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 |
||
37 | class Product implements EntityInterface |
||
38 | { |
||
39 | use Identifiable; |
||
40 | use Enableable; |
||
41 | use Sortable; |
||
42 | use Translatable; |
||
43 | use Timestampable; |
||
44 | use Blameable; |
||
45 | use ShopCollectionAwareTrait; |
||
46 | use ProductExtraTrait; |
||
47 | |||
48 | protected $sku = ''; |
||
49 | protected $barcode = ''; |
||
50 | protected $stock = 0; |
||
51 | protected $trackStock = true; |
||
52 | protected $weight = 0.00; |
||
53 | protected $packageSize = 1.00; |
||
54 | protected $popularity = 0; |
||
55 | |||
56 | /** |
||
57 | * @var Price |
||
58 | */ |
||
59 | protected $buyPrice; |
||
60 | |||
61 | /** |
||
62 | * @var Tax |
||
63 | */ |
||
64 | protected $buyPriceTax; |
||
65 | |||
66 | /** |
||
67 | * @var DiscountablePrice |
||
68 | */ |
||
69 | protected $sellPrice; |
||
70 | |||
71 | /** |
||
72 | * @var Tax |
||
73 | */ |
||
74 | protected $sellPriceTax; |
||
75 | |||
76 | /** |
||
77 | * @var Dimension |
||
78 | */ |
||
79 | protected $dimension; |
||
80 | |||
81 | /** |
||
82 | * @var AttributeGroup |
||
83 | */ |
||
84 | protected $attributeGroup; |
||
85 | |||
86 | /** |
||
87 | * @var Collection |
||
88 | */ |
||
89 | protected $variants; |
||
90 | |||
91 | /** |
||
92 | * @var Collection |
||
93 | */ |
||
94 | protected $categories; |
||
95 | |||
96 | /** |
||
97 | * @var Collection |
||
98 | */ |
||
99 | protected $distinctions; |
||
100 | |||
101 | /** |
||
102 | * @var Media |
||
103 | */ |
||
104 | protected $photo; |
||
105 | |||
106 | /** |
||
107 | * @var Collection |
||
108 | */ |
||
109 | protected $productPhotos; |
||
110 | |||
111 | /** |
||
112 | * @var null|Availability |
||
113 | */ |
||
114 | protected $availability; |
||
115 | |||
116 | /** |
||
117 | * @var Producer |
||
118 | */ |
||
119 | protected $producer; |
||
120 | |||
121 | /** |
||
122 | * @var Unit |
||
123 | */ |
||
124 | protected $unit; |
||
125 | |||
126 | /** |
||
127 | * @var ProducerCollection |
||
128 | */ |
||
129 | protected $producerCollection; |
||
130 | |||
131 | public function __construct() |
||
142 | |||
143 | public function getSku(): string |
||
147 | |||
148 | public function setSku(string $sku) |
||
152 | |||
153 | public function getStock(): int |
||
157 | |||
158 | public function getBarcode(): string |
||
162 | |||
163 | public function setBarcode(string $barcode) |
||
167 | |||
168 | public function setStock(int $stock) |
||
172 | |||
173 | public function getTrackStock(): bool |
||
177 | |||
178 | public function setTrackStock(bool $trackStock) |
||
182 | |||
183 | public function getDistinctions(): Collection |
||
187 | |||
188 | public function setDistinctions(Collection $distinctions) |
||
198 | |||
199 | public function getPhoto() |
||
203 | |||
204 | public function setPhoto(Media $photo = null) |
||
208 | |||
209 | public function getProductPhotos(): Collection |
||
213 | |||
214 | public function setProductPhotos(Collection $photos) |
||
218 | |||
219 | public function addProductPhoto(ProductPhoto $photo) |
||
223 | |||
224 | public function getCategories(): Collection |
||
228 | |||
229 | public function setCategories(Collection $collection) |
||
233 | |||
234 | public function addCategory(Category $category) |
||
238 | |||
239 | public function getSellPrice(): DiscountablePrice |
||
243 | |||
244 | public function setSellPrice(DiscountablePrice $sellPrice) |
||
248 | |||
249 | public function getBuyPrice(): Price |
||
253 | |||
254 | public function setBuyPrice(Price $buyPrice) |
||
258 | |||
259 | public function getWeight(): float |
||
263 | |||
264 | public function setWeight(float $weight) |
||
268 | |||
269 | public function getDimension(): Dimension |
||
273 | |||
274 | public function setDimension(Dimension $dimension) |
||
278 | |||
279 | public function getPackageSize(): float |
||
283 | |||
284 | public function setPackageSize(float $packageSize) |
||
288 | |||
289 | public function getAttributeGroup() |
||
293 | |||
294 | public function setAttributeGroup(AttributeGroup $group = null) |
||
298 | |||
299 | public function getVariants(): Collection |
||
303 | |||
304 | public function setVariants(Collection $variants) |
||
314 | |||
315 | public function getBuyPriceTax() |
||
319 | |||
320 | public function setBuyPriceTax(Tax $tax = null) |
||
324 | |||
325 | public function getSellPriceTax() |
||
329 | |||
330 | public function setSellPriceTax(Tax $tax = null) |
||
334 | |||
335 | public function getAvailability() |
||
339 | |||
340 | public function setAvailability(Availability $availability = null) |
||
344 | |||
345 | public function getProducer() |
||
349 | |||
350 | public function setProducer(Producer $producer) |
||
354 | |||
355 | public function getUnit() |
||
359 | |||
360 | public function setUnit(Unit $unit = null) |
||
364 | |||
365 | public function getProducerCollection() |
||
369 | |||
370 | public function setProducerCollection(ProducerCollection $producerCollection = null) |
||
374 | |||
375 | public function getPopularity(): int |
||
379 | |||
380 | public function setPopularity(int $popularity) |
||
384 | |||
385 | public function increasePopularity(int $increase = 1) |
||
389 | } |
||
390 |