Complex classes like Order 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 Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Order extends BaseOrder implements OrderInterface |
||
31 | { |
||
32 | /** |
||
33 | * @var CustomerInterface |
||
34 | */ |
||
35 | protected $customer; |
||
36 | |||
37 | /** |
||
38 | * @var ChannelInterface |
||
39 | */ |
||
40 | protected $channel; |
||
41 | |||
42 | /** |
||
43 | * @var AddressInterface |
||
44 | */ |
||
45 | protected $shippingAddress; |
||
46 | |||
47 | /** |
||
48 | * @var AddressInterface |
||
49 | */ |
||
50 | protected $billingAddress; |
||
51 | |||
52 | /** |
||
53 | * @var Collection|BasePaymentInterface[] |
||
54 | */ |
||
55 | protected $payments; |
||
56 | |||
57 | /** |
||
58 | * @var Collection|ShipmentInterface[] |
||
59 | */ |
||
60 | protected $shipments; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $currencyCode; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $localeCode; |
||
71 | |||
72 | /** |
||
73 | * @var BaseCouponInterface |
||
74 | */ |
||
75 | protected $promotionCoupon; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $checkoutState = OrderCheckoutStates::STATE_CART; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $paymentState = OrderPaymentStates::STATE_CART; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $shippingState = OrderShippingStates::STATE_CART; |
||
91 | |||
92 | /** |
||
93 | * @var Collection|BasePromotionInterface[] |
||
94 | */ |
||
95 | protected $promotions; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $tokenValue; |
||
101 | |||
102 | /** |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $customerIp; |
||
106 | |||
107 | public function __construct() |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function getCustomer(): ?BaseCustomerInterface |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function setCustomer(?BaseCustomerInterface $customer): void |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function getChannel(): ?BaseChannelInterface |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function setChannel(?BaseChannelInterface $channel): void |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function getUser(): ?BaseUserInterface |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function getShippingAddress(): ?AddressInterface |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function setShippingAddress(?AddressInterface $address): void |
||
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | public function getBillingAddress(): ?AddressInterface |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function setBillingAddress(?AddressInterface $address): void |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function getCheckoutState(): ?string |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function setCheckoutState(?string $checkoutState): void |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function getPaymentState(): ?string |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | public function setPaymentState(?string $paymentState): void |
||
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | public function getItemUnits(): Collection |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function getItemUnitsByVariant(ProductVariantInterface $variant): Collection |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function getPayments(): Collection |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function hasPayments(): bool |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function addPayment(BasePaymentInterface $payment): void |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function removePayment(BasePaymentInterface $payment): void |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | */ |
||
300 | public function hasPayment(BasePaymentInterface $payment): bool |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | public function getLastPayment(?string $state = null): ?PaymentInterface |
||
320 | |||
321 | /** |
||
322 | * @return bool |
||
323 | */ |
||
324 | public function isShippingRequired(): bool |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | */ |
||
338 | public function getShipments(): Collection |
||
342 | |||
343 | /** |
||
344 | * {@inheritdoc} |
||
345 | */ |
||
346 | public function hasShipments(): bool |
||
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | public function addShipment(ShipmentInterface $shipment): void |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public function removeShipment(ShipmentInterface $shipment): void |
||
372 | |||
373 | public function removeShipments(): void |
||
377 | |||
378 | /** |
||
379 | * {@inheritdoc} |
||
380 | */ |
||
381 | public function hasShipment(ShipmentInterface $shipment): bool |
||
385 | |||
386 | /** |
||
387 | * {@inheritdoc} |
||
388 | */ |
||
389 | public function getPromotionCoupon(): ?BaseCouponInterface |
||
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | public function setPromotionCoupon(?BaseCouponInterface $coupon): void |
||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | public function getPromotionSubjectTotal(): int |
||
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | public function getPromotionSubjectCount(): int |
||
417 | |||
418 | /** |
||
419 | * {@inheritdoc} |
||
420 | */ |
||
421 | public function getCurrencyCode(): ?string |
||
425 | |||
426 | /** |
||
427 | * {@inheritdoc} |
||
428 | */ |
||
429 | public function setCurrencyCode(?string $currencyCode): void |
||
433 | |||
434 | /** |
||
435 | * {@inheritdoc} |
||
436 | */ |
||
437 | public function getLocaleCode(): ?string |
||
441 | |||
442 | /** |
||
443 | * {@inheritdoc} |
||
444 | */ |
||
445 | public function setLocaleCode(?string $localeCode): void |
||
451 | |||
452 | /** |
||
453 | * {@inheritdoc} |
||
454 | */ |
||
455 | public function getShippingState(): ?string |
||
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | public function setShippingState(?string $state): void |
||
467 | |||
468 | /** |
||
469 | * {@inheritdoc} |
||
470 | */ |
||
471 | public function hasPromotion(BasePromotionInterface $promotion): bool |
||
475 | |||
476 | /** |
||
477 | * {@inheritdoc} |
||
478 | */ |
||
479 | public function addPromotion(BasePromotionInterface $promotion): void |
||
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | public function removePromotion(BasePromotionInterface $promotion): void |
||
495 | |||
496 | /** |
||
497 | * {@inheritdoc} |
||
498 | */ |
||
499 | public function getPromotions(): Collection |
||
503 | |||
504 | /** |
||
505 | * Returns sum of neutral and non neutral tax adjustments on order and total tax of order items. |
||
506 | * |
||
507 | * {@inheritdoc} |
||
508 | */ |
||
509 | public function getTaxTotal(): int |
||
522 | |||
523 | /** |
||
524 | * Returns shipping fee together with taxes decreased by shipping discount. |
||
525 | * |
||
526 | * {@inheritdoc} |
||
527 | */ |
||
528 | public function getShippingTotal(): int |
||
536 | |||
537 | /** |
||
538 | * Returns amount of order discount. Does not include order item and shipping discounts. |
||
539 | * |
||
540 | * {@inheritdoc} |
||
541 | */ |
||
542 | public function getOrderPromotionTotal(): int |
||
552 | |||
553 | /** |
||
554 | * {@inheritdoc} |
||
555 | */ |
||
556 | public function getTokenValue(): ?string |
||
560 | |||
561 | /** |
||
562 | * {@inheritdoc} |
||
563 | */ |
||
564 | public function setTokenValue(?string $tokenValue): void |
||
568 | |||
569 | /** |
||
570 | * {@inheritdoc} |
||
571 | */ |
||
572 | public function getCustomerIp(): ?string |
||
576 | |||
577 | /** |
||
578 | * {@inheritdoc} |
||
579 | */ |
||
580 | public function setCustomerIp(?string $customerIp): void |
||
584 | } |
||
585 |
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..