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 |
||
29 | class Order extends Cart implements OrderInterface |
||
30 | { |
||
31 | /** |
||
32 | * @var BaseCustomerInterface |
||
33 | */ |
||
34 | protected $customer; |
||
35 | |||
36 | /** |
||
37 | * @var ChannelInterface |
||
38 | */ |
||
39 | protected $channel; |
||
40 | |||
41 | /** |
||
42 | * @var AddressInterface |
||
43 | */ |
||
44 | protected $shippingAddress; |
||
45 | |||
46 | /** |
||
47 | * @var AddressInterface |
||
48 | */ |
||
49 | protected $billingAddress; |
||
50 | |||
51 | /** |
||
52 | * @var Collection|BasePaymentInterface[] |
||
53 | */ |
||
54 | protected $payments; |
||
55 | |||
56 | /** |
||
57 | * @var Collection|ShipmentInterface[] |
||
58 | */ |
||
59 | protected $shipments; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $currency; |
||
65 | |||
66 | /** |
||
67 | * @var float |
||
68 | */ |
||
69 | protected $exchangeRate = 1.0; |
||
70 | |||
71 | /** |
||
72 | * @var BaseCouponInterface |
||
73 | */ |
||
74 | protected $promotionCoupon; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $checkoutState = OrderCheckoutStates::STATE_CART; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $paymentState = BasePaymentInterface::STATE_NEW; |
||
85 | |||
86 | /** |
||
87 | * It depends on the status of all order shipments. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $shippingState = OrderShippingStates::CHECKOUT; |
||
92 | |||
93 | /** |
||
94 | * @var Collection|BasePromotionInterface[] |
||
95 | */ |
||
96 | protected $promotions; |
||
97 | |||
98 | public function __construct() |
||
99 | { |
||
100 | parent::__construct(); |
||
101 | |||
102 | $this->payments = new ArrayCollection(); |
||
103 | $this->shipments = new ArrayCollection(); |
||
104 | $this->promotions = new ArrayCollection(); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function getCustomer() |
||
111 | { |
||
112 | return $this->customer; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function setCustomer(BaseCustomerInterface $customer = null) |
||
119 | { |
||
120 | $this->customer = $customer; |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function getChannel() |
||
129 | { |
||
130 | return $this->channel; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function setChannel(BaseChannelInterface $channel = null) |
||
137 | { |
||
138 | $this->channel = $channel; |
||
|
|||
139 | } |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function getUser() |
||
145 | { |
||
146 | if (null === $this->customer) { |
||
147 | return null; |
||
148 | } |
||
149 | |||
150 | return $this->customer->getUser(); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function getShippingAddress() |
||
157 | { |
||
158 | return $this->shippingAddress; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function setShippingAddress(AddressInterface $address) |
||
165 | { |
||
166 | $this->shippingAddress = $address; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function getBillingAddress() |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function setBillingAddress(AddressInterface $address) |
||
181 | { |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function getCheckoutState() |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function setCheckoutState($checkoutState) |
||
197 | { |
||
198 | $this->checkoutState = $checkoutState; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function getPaymentState() |
||
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | public function setPaymentState($paymentState) |
||
213 | { |
||
214 | $this->paymentState = $paymentState; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function getItemUnits() |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function getItemUnitsByVariant(ProductVariantInterface $variant) |
||
238 | { |
||
239 | return $this->getItemUnits()->filter(function (OrderItemUnitInterface $itemUnit) use ($variant) { |
||
240 | return $variant === $itemUnit->getStockable(); |
||
241 | }); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function getPayments() |
||
248 | { |
||
249 | return $this->payments; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function hasPayments() |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function addPayment(BasePaymentInterface $payment) |
||
264 | { |
||
265 | /** @var $payment PaymentInterface */ |
||
266 | if (!$this->hasPayment($payment)) { |
||
267 | $this->payments->add($payment); |
||
268 | $payment->setOrder($this); |
||
269 | |||
270 | $this->setPaymentState($payment->getState()); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function removePayment(BasePaymentInterface $payment) |
||
278 | { |
||
279 | /** @var $payment PaymentInterface */ |
||
280 | if ($this->hasPayment($payment)) { |
||
281 | $this->payments->removeElement($payment); |
||
282 | $payment->setOrder(null); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function hasPayment(BasePaymentInterface $payment) |
||
293 | |||
294 | /** |
||
295 | * {@inheritdoc} |
||
296 | */ |
||
297 | public function getLastPayment($state = BasePaymentInterface::STATE_NEW) |
||
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | public function getShipments() |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function hasShipments() |
||
323 | |||
324 | /** |
||
325 | * {@inheritdoc} |
||
326 | */ |
||
327 | public function addShipment(ShipmentInterface $shipment) |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | */ |
||
338 | public function removeShipment(ShipmentInterface $shipment) |
||
345 | |||
346 | /** |
||
347 | * {@inheritdoc} |
||
348 | */ |
||
349 | public function hasShipment(ShipmentInterface $shipment) |
||
353 | |||
354 | /** |
||
355 | * @return null|BaseCouponInterface |
||
356 | */ |
||
357 | public function getPromotionCoupon() |
||
358 | { |
||
359 | return $this->promotionCoupon; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public function setPromotionCoupon(BaseCouponInterface $coupon = null) |
||
369 | |||
370 | /** |
||
371 | * {@inheritdoc} |
||
372 | */ |
||
373 | public function getPromotionSubjectTotal() |
||
377 | |||
378 | /** |
||
379 | * {@inheritdoc} |
||
380 | */ |
||
381 | public function getPromotionSubjectCount() |
||
385 | |||
386 | /** |
||
387 | * {@inheritdoc} |
||
388 | */ |
||
389 | public function getCurrency() |
||
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | public function setCurrency($currency) |
||
403 | |||
404 | /** |
||
405 | * {@inheritdoc} |
||
406 | */ |
||
407 | public function getExchangeRate() |
||
411 | |||
412 | /** |
||
413 | * {@inheritdoc} |
||
414 | */ |
||
415 | public function setExchangeRate($exchangeRate) |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function getShippingState() |
||
429 | |||
430 | /** |
||
431 | * {@inheritdoc} |
||
432 | */ |
||
433 | public function setShippingState($state) |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function isBackorder() |
||
453 | |||
454 | /** |
||
455 | * Gets the last updated shipment of the order |
||
456 | * |
||
457 | * @return false|ShipmentInterface |
||
458 | */ |
||
459 | public function getLastShipment() |
||
474 | |||
475 | /** |
||
476 | * {@inheritdoc} |
||
477 | */ |
||
478 | public function isInvoiceAvailable() |
||
486 | |||
487 | /** |
||
488 | * {@inheritdoc} |
||
489 | */ |
||
490 | public function hasPromotion(BasePromotionInterface $promotion) |
||
494 | |||
495 | /** |
||
496 | * {@inheritdoc} |
||
497 | */ |
||
498 | public function addPromotion(BasePromotionInterface $promotion) |
||
506 | |||
507 | /** |
||
508 | * {@inheritdoc} |
||
509 | */ |
||
510 | public function removePromotion(BasePromotionInterface $promotion) |
||
518 | |||
519 | /** |
||
520 | * {@inheritdoc} |
||
521 | */ |
||
522 | public function getPromotions() |
||
526 | } |
||
527 |
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.