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 |
||
20 | class Order implements OrderInterface |
||
21 | { |
||
22 | use TimestampableTrait; |
||
23 | |||
24 | /** |
||
25 | * @var mixed |
||
26 | */ |
||
27 | protected $id; |
||
28 | |||
29 | /** |
||
30 | * @var \DateTimeInterface|null |
||
31 | */ |
||
32 | protected $checkoutCompletedAt; |
||
33 | |||
34 | /** |
||
35 | * @var string|null |
||
36 | */ |
||
37 | protected $number; |
||
38 | |||
39 | /** |
||
40 | * @var string|null |
||
41 | */ |
||
42 | protected $notes; |
||
43 | |||
44 | /** |
||
45 | * @var Collection|OrderItemInterface[] |
||
46 | */ |
||
47 | protected $items; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $itemsTotal = 0; |
||
53 | |||
54 | /** |
||
55 | * @var Collection|AdjustmentInterface[] |
||
56 | */ |
||
57 | protected $adjustments; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | protected $adjustmentsTotal = 0; |
||
63 | |||
64 | /** |
||
65 | * Items total + adjustments total. |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $total = 0; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $state = OrderInterface::STATE_CART; |
||
75 | |||
76 | public function __construct() |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function getId() |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function getCheckoutCompletedAt(): ?\DateTimeInterface |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function setCheckoutCompletedAt(?\DateTimeInterface $checkoutCompletedAt): void |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function isCheckoutCompleted(): bool |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function completeCheckout(): void |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function getNumber(): ?string |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function setNumber(?string $number): void |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function getNotes(): ?string |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function setNotes(?string $notes): void |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function getItems(): Collection |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function clearItems(): void |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function countItems(): int |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function addItem(OrderItemInterface $item): void |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function removeItem(OrderItemInterface $item): void |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function hasItem(OrderItemInterface $item): bool |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | public function getItemsTotal(): int |
||
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | public function recalculateItemsTotal(): void |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function getTotal(): int |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | public function getTotalQuantity(): int |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function getState(): string |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function setState(string $state): void |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | public function isEmpty(): bool |
||
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | public function getAdjustments(?string $type = null): Collection |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function getAdjustmentsRecursively(?string $type = null): Collection |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function addAdjustment(AdjustmentInterface $adjustment): void |
||
325 | |||
326 | /** |
||
327 | * {@inheritdoc} |
||
328 | */ |
||
329 | public function removeAdjustment(AdjustmentInterface $adjustment): void |
||
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | public function hasAdjustment(AdjustmentInterface $adjustment): bool |
||
345 | |||
346 | /** |
||
347 | * {@inheritdoc} |
||
348 | */ |
||
349 | public function getAdjustmentsTotal(?string $type = null): int |
||
364 | |||
365 | /** |
||
366 | * {@inheritdoc} |
||
367 | */ |
||
368 | public function getAdjustmentsTotalRecursively(?string $type = null): int |
||
379 | |||
380 | /** |
||
381 | * {@inheritdoc} |
||
382 | */ |
||
383 | public function removeAdjustments(?string $type = null): void |
||
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | public function removeAdjustmentsRecursively(?string $type = null): void |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function recalculateAdjustmentsTotal(): void |
||
420 | |||
421 | /** |
||
422 | * Items total + Adjustments total. |
||
423 | */ |
||
424 | protected function recalculateTotal(): void |
||
432 | |||
433 | /** |
||
434 | * @param AdjustmentInterface $adjustment |
||
435 | */ |
||
436 | protected function addToAdjustmentsTotal(AdjustmentInterface $adjustment): void |
||
443 | |||
444 | /** |
||
445 | * @param AdjustmentInterface $adjustment |
||
446 | */ |
||
447 | protected function subtractFromAdjustmentsTotal(AdjustmentInterface $adjustment): void |
||
454 | } |
||
455 |
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..