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 |
||
23 | class ProductVariant extends BaseVariant implements ProductVariantInterface, Comparable |
||
24 | { |
||
25 | /** @var int */ |
||
26 | protected $version = 1; |
||
27 | |||
28 | /** @var int */ |
||
29 | protected $onHold = 0; |
||
30 | |||
31 | /** @var int */ |
||
32 | protected $onHand = 0; |
||
33 | |||
34 | /** @var bool */ |
||
35 | protected $tracked = false; |
||
36 | |||
37 | /** @var float */ |
||
38 | protected $weight; |
||
39 | |||
40 | /** @var float */ |
||
41 | protected $width; |
||
42 | |||
43 | /** @var float */ |
||
44 | protected $height; |
||
45 | |||
46 | /** @var float */ |
||
47 | protected $depth; |
||
48 | |||
49 | /** @var TaxCategoryInterface */ |
||
50 | protected $taxCategory; |
||
51 | |||
52 | /** @var ShippingCategoryInterface */ |
||
53 | protected $shippingCategory; |
||
54 | |||
55 | /** @var Collection */ |
||
56 | protected $channelPricings; |
||
57 | |||
58 | /** @var bool */ |
||
59 | protected $shippingRequired = true; |
||
60 | |||
61 | /** |
||
62 | * @var Collection|ProductImageInterface[] |
||
63 | * |
||
64 | * @psalm-var Collection<array-key, ProductImageInterface> |
||
65 | */ |
||
66 | protected $images; |
||
67 | |||
68 | public function __construct() |
||
78 | |||
79 | public function __toString(): string |
||
95 | |||
96 | public function getVersion(): ?int |
||
100 | |||
101 | public function setVersion(?int $version): void |
||
105 | |||
106 | public function isInStock(): bool |
||
110 | |||
111 | public function getOnHold(): ?int |
||
115 | |||
116 | public function setOnHold(?int $onHold): void |
||
120 | |||
121 | public function getOnHand(): ?int |
||
125 | |||
126 | public function setOnHand(?int $onHand): void |
||
130 | |||
131 | public function isTracked(): bool |
||
135 | |||
136 | public function setTracked(bool $tracked): void |
||
140 | |||
141 | public function getInventoryName(): ?string |
||
145 | |||
146 | public function getShippingCategory(): ?ShippingCategoryInterface |
||
150 | |||
151 | public function setShippingCategory(?ShippingCategoryInterface $category): void |
||
155 | |||
156 | public function getWeight(): ?float |
||
160 | |||
161 | public function setWeight(?float $weight): void |
||
165 | |||
166 | public function getWidth(): ?float |
||
170 | |||
171 | public function setWidth(?float $width): void |
||
175 | |||
176 | public function getHeight(): ?float |
||
180 | |||
181 | public function setHeight(?float $height): void |
||
185 | |||
186 | public function getDepth(): ?float |
||
190 | |||
191 | public function setDepth(?float $depth): void |
||
195 | |||
196 | public function getShippingWeight(): ?float |
||
200 | |||
201 | public function getShippingWidth(): ?float |
||
205 | |||
206 | public function getShippingHeight(): ?float |
||
210 | |||
211 | public function getShippingDepth(): ?float |
||
215 | |||
216 | public function getShippingVolume(): ?float |
||
220 | |||
221 | public function getTaxCategory(): ?TaxCategoryInterface |
||
225 | |||
226 | public function setTaxCategory(?TaxCategoryInterface $category): void |
||
230 | |||
231 | public function getChannelPricings(): Collection |
||
235 | |||
236 | public function getChannelPricingForChannel(ChannelInterface $channel): ?ChannelPricingInterface |
||
244 | |||
245 | public function hasChannelPricingForChannel(ChannelInterface $channel): bool |
||
249 | |||
250 | public function hasChannelPricing(ChannelPricingInterface $channelPricing): bool |
||
254 | |||
255 | public function addChannelPricing(ChannelPricingInterface $channelPricing): void |
||
262 | |||
263 | public function removeChannelPricing(ChannelPricingInterface $channelPricing): void |
||
270 | |||
271 | public function isShippingRequired(): bool |
||
275 | |||
276 | public function setShippingRequired(bool $shippingRequired): void |
||
280 | |||
281 | /** |
||
282 | * @psalm-suppress InvalidReturnType https://github.com/doctrine/collections/pull/220 |
||
283 | * @psalm-suppress InvalidReturnStatement https://github.com/doctrine/collections/pull/220 |
||
284 | */ |
||
285 | public function getImages(): Collection |
||
289 | |||
290 | /** |
||
291 | * @psalm-suppress InvalidReturnType https://github.com/doctrine/collections/pull/220 |
||
292 | * @psalm-suppress InvalidReturnStatement https://github.com/doctrine/collections/pull/220 |
||
293 | */ |
||
294 | public function getImagesByType(string $type): Collection |
||
300 | |||
301 | public function hasImages(): bool |
||
305 | |||
306 | public function hasImage(ProductImageInterface $image): bool |
||
310 | |||
311 | public function addImage(ProductImageInterface $image): void |
||
320 | |||
321 | public function removeImage(ProductImageInterface $image): void |
||
327 | |||
328 | /** |
||
329 | * @inheritDoc |
||
330 | */ |
||
331 | public function compareTo($other): int |
||
335 | } |
||
336 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..