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 |
||
31 | class Order extends BaseOrder implements OrderInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var BaseCustomerInterface |
||
35 | */ |
||
36 | protected $customer; |
||
37 | |||
38 | /** |
||
39 | * @var ChannelInterface |
||
40 | */ |
||
41 | protected $channel; |
||
42 | |||
43 | /** |
||
44 | * @var AddressInterface |
||
45 | */ |
||
46 | protected $shippingAddress; |
||
47 | |||
48 | /** |
||
49 | * @var AddressInterface |
||
50 | */ |
||
51 | protected $billingAddress; |
||
52 | |||
53 | /** |
||
54 | * @var Collection|BasePaymentInterface[] |
||
55 | */ |
||
56 | protected $payments; |
||
57 | |||
58 | /** |
||
59 | * @var Collection|ShipmentInterface[] |
||
60 | */ |
||
61 | protected $shipments; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $currencyCode; |
||
67 | |||
68 | /** |
||
69 | * @var float |
||
70 | */ |
||
71 | protected $exchangeRate = 1.0; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $localeCode; |
||
77 | |||
78 | /** |
||
79 | * @var BaseCouponInterface |
||
80 | */ |
||
81 | protected $promotionCoupon; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $checkoutState = OrderCheckoutStates::STATE_CART; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $paymentState = OrderPaymentStates::STATE_CART; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $shippingState = OrderShippingStates::STATE_CART; |
||
97 | |||
98 | /** |
||
99 | * @var Collection|BasePromotionInterface[] |
||
100 | */ |
||
101 | protected $promotions; |
||
102 | |||
103 | /** |
||
104 | * @var string |
||
105 | */ |
||
106 | protected $tokenValue; |
||
107 | |||
108 | public function __construct() |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function getCustomer() |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function setCustomer(BaseCustomerInterface $customer = null) |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function getChannel() |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function setChannel(BaseChannelInterface $channel = null) |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function getUser() |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function getShippingAddress() |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function setShippingAddress(AddressInterface $address) |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function getBillingAddress() |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function setBillingAddress(AddressInterface $address) |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function getCheckoutState() |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function setCheckoutState($checkoutState) |
||
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | public function getPaymentState() |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function setPaymentState($paymentState) |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function getItemUnits() |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function getItemUnitsByVariant(ProductVariantInterface $variant) |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function getPayments() |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function hasPayments() |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function addPayment(BasePaymentInterface $payment) |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function removePayment(BasePaymentInterface $payment) |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function hasPayment(BasePaymentInterface $payment) |
||
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | public function getLastNewPayment() |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function getShipments() |
||
323 | |||
324 | /** |
||
325 | * {@inheritdoc} |
||
326 | */ |
||
327 | public function hasShipments() |
||
331 | |||
332 | /** |
||
333 | * {@inheritdoc} |
||
334 | */ |
||
335 | public function addShipment(ShipmentInterface $shipment) |
||
342 | |||
343 | /** |
||
344 | * {@inheritdoc} |
||
345 | */ |
||
346 | public function removeShipment(ShipmentInterface $shipment) |
||
353 | |||
354 | /** |
||
355 | * {@inheritdoc} |
||
356 | */ |
||
357 | public function removeShipments() |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public function hasShipment(ShipmentInterface $shipment) |
||
369 | |||
370 | /** |
||
371 | * {@inheritdoc} |
||
372 | */ |
||
373 | public function getPromotionCoupon() |
||
377 | |||
378 | /** |
||
379 | * {@inheritdoc} |
||
380 | */ |
||
381 | public function setPromotionCoupon(BaseCouponInterface $coupon = null) |
||
385 | |||
386 | /** |
||
387 | * {@inheritdoc} |
||
388 | */ |
||
389 | public function getPromotionSubjectTotal() |
||
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | public function getPromotionSubjectCount() |
||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | public function getCurrencyCode() |
||
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | public function setCurrencyCode($currencyCode) |
||
419 | |||
420 | /** |
||
421 | * {@inheritdoc} |
||
422 | */ |
||
423 | public function getExchangeRate() |
||
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | */ |
||
431 | public function setExchangeRate($exchangeRate) |
||
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | */ |
||
439 | public function getLocaleCode() |
||
443 | |||
444 | /** |
||
445 | * {@inheritdoc} |
||
446 | */ |
||
447 | public function setLocaleCode($localeCode) |
||
453 | |||
454 | /** |
||
455 | * {@inheritdoc} |
||
456 | */ |
||
457 | public function getShippingState() |
||
461 | |||
462 | /** |
||
463 | * {@inheritdoc} |
||
464 | */ |
||
465 | public function setShippingState($state) |
||
469 | |||
470 | /** |
||
471 | * {@inheritdoc} |
||
472 | */ |
||
473 | public function hasPromotion(BasePromotionInterface $promotion) |
||
477 | |||
478 | /** |
||
479 | * {@inheritdoc} |
||
480 | */ |
||
481 | public function addPromotion(BasePromotionInterface $promotion) |
||
487 | |||
488 | /** |
||
489 | * {@inheritdoc} |
||
490 | */ |
||
491 | public function removePromotion(BasePromotionInterface $promotion) |
||
497 | |||
498 | /** |
||
499 | * {@inheritdoc} |
||
500 | */ |
||
501 | public function getPromotions() |
||
505 | |||
506 | /** |
||
507 | * Returns sum of neutral and non neutral tax adjustments on order and total tax of order items. |
||
508 | * |
||
509 | * {@inheritdoc} |
||
510 | */ |
||
511 | public function getTaxTotal() |
||
524 | |||
525 | /** |
||
526 | * Returns shipping fee together with taxes decreased by shipping discount. |
||
527 | * |
||
528 | * @return int |
||
529 | */ |
||
530 | public function getShippingTotal() |
||
538 | |||
539 | /** |
||
540 | * Returns amount of order discount. Does not include order item and shipping discounts. |
||
541 | * |
||
542 | * @return int |
||
543 | */ |
||
544 | public function getOrderPromotionTotal() |
||
554 | |||
555 | /** |
||
556 | * {@inheritdoc} |
||
557 | */ |
||
558 | public function setTokenValue($tokenValue) |
||
562 | |||
563 | /** |
||
564 | * {@inheritdoc} |
||
565 | */ |
||
566 | public function getTokenValue() |
||
570 | } |
||
571 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.