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 |
||
22 | class Order implements OrderInterface |
||
23 | { |
||
24 | /** |
||
25 | * @ORM\Id |
||
26 | * @ORM\GeneratedValue |
||
27 | * @ORM\Column(type="integer", name="id") |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $id; |
||
32 | |||
33 | /** |
||
34 | * @ORM\OneToMany(targetEntity="UserSubscription", mappedBy="order", orphanRemoval=true, cascade={"all"}) |
||
35 | * |
||
36 | * @var Collection |
||
37 | */ |
||
38 | protected $items; |
||
39 | |||
40 | /** |
||
41 | * @ORM\Column(type="string", name="currency", length=3) |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $currency; |
||
46 | |||
47 | /** |
||
48 | * @ORM\Column(type="integer", name="total") |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $total = 0; |
||
53 | |||
54 | /** |
||
55 | * @ORM\Column(type="integer", name="items_total") |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $itemsTotal = 0; |
||
60 | |||
61 | /** |
||
62 | * @ORM\Column(type="integer", name="discount_total") |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $discountTotal = 0; |
||
67 | |||
68 | /** |
||
69 | * @ORM\ManyToOne(targetEntity="Newscoop\Entity\User") |
||
70 | * @ORM\JoinColumn(name="user_id", referencedColumnName="Id") |
||
71 | * |
||
72 | * @var User |
||
73 | */ |
||
74 | protected $user; |
||
75 | |||
76 | /** |
||
77 | * @ORM\OneToMany(targetEntity="Modification", mappedBy="order", orphanRemoval=true, cascade={"all"}) |
||
78 | * |
||
79 | * @var Collection |
||
80 | */ |
||
81 | protected $modifications; |
||
82 | |||
83 | /** |
||
84 | * @ORM\OneToMany(targetEntity="Payment", mappedBy="order", orphanRemoval=true, cascade={"all"}) |
||
85 | * |
||
86 | * @var Collection |
||
87 | */ |
||
88 | protected $payments; |
||
89 | |||
90 | /** |
||
91 | * @ORM\ManyToMany(targetEntity="Discount", cascade={"persist"}) |
||
92 | * @ORM\JoinTable(name="plugin_paywall_order_discount", |
||
93 | * joinColumns={ |
||
94 | * @ORM\JoinColumn(name="order_id", referencedColumnName="id") |
||
95 | * }, |
||
96 | * inverseJoinColumns={ |
||
97 | * @ORM\JoinColumn(name="discount_id", referencedColumnName="id") |
||
98 | * } |
||
99 | * ) |
||
100 | * |
||
101 | * @var Collection |
||
102 | */ |
||
103 | protected $discounts; |
||
104 | |||
105 | /** |
||
106 | * @ORM\Column(type="string", name="payment_state") |
||
107 | * |
||
108 | * @var string |
||
109 | */ |
||
110 | protected $paymentState = PaymentInterface::STATE_NEW; |
||
111 | |||
112 | /** |
||
113 | * @ORM\Column(type="datetime", name="updated_at", nullable=true) |
||
114 | * |
||
115 | * @var \DateTime |
||
116 | */ |
||
117 | protected $updatedAt; |
||
118 | |||
119 | /** |
||
120 | * @ORM\Column(type="datetime", name="created_at") |
||
121 | * |
||
122 | * @var \DateTime |
||
123 | */ |
||
124 | protected $createdAt; |
||
125 | |||
126 | /** |
||
127 | * Constructor. |
||
128 | */ |
||
129 | public function __construct() |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function getItems() |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function setItems(Collection $items) |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function countItems() |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function getUser() |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function setUser(User $user) |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function addItem($item) |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function removeItem($item) |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function hasItem($item) |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function getToPay() |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function setToPay($total) |
||
233 | |||
234 | /** |
||
235 | * Gets the value of total. |
||
236 | * |
||
237 | * @return int |
||
238 | */ |
||
239 | public function getTotal() |
||
243 | |||
244 | /** |
||
245 | * Sets the value of total. |
||
246 | * |
||
247 | * @param int $total the total |
||
248 | * |
||
249 | * @return self |
||
250 | */ |
||
251 | public function setTotal($total) |
||
257 | |||
258 | /** |
||
259 | * Gets the value of id. |
||
260 | * |
||
261 | * @return int |
||
262 | */ |
||
263 | public function getId() |
||
267 | |||
268 | /** |
||
269 | * Sets the value of id. |
||
270 | * |
||
271 | * @param int $id the id |
||
272 | * |
||
273 | * @return self |
||
274 | */ |
||
275 | public function setId($id) |
||
281 | |||
282 | /** |
||
283 | * Gets the value of updatedAt. |
||
284 | * |
||
285 | * @return \DateTime |
||
286 | */ |
||
287 | public function getUpdatedAt() |
||
291 | |||
292 | /** |
||
293 | * Sets the value of updatedAt. |
||
294 | * |
||
295 | * @param \DateTime $updatedAt the updated at |
||
296 | * |
||
297 | * @return self |
||
298 | */ |
||
299 | public function setUpdatedAt(\DateTime $updatedAt) |
||
305 | |||
306 | /** |
||
307 | * Gets the value of createdAt. |
||
308 | * |
||
309 | * @return \DateTime |
||
310 | */ |
||
311 | public function getCreatedAt() |
||
315 | |||
316 | /** |
||
317 | * Sets the value of createdAt. |
||
318 | * |
||
319 | * @param \DateTime $createdAt the created at |
||
320 | * |
||
321 | * @return self |
||
322 | */ |
||
323 | public function setCreatedAt(\DateTime $createdAt) |
||
329 | |||
330 | /** |
||
331 | * Gets the value of currency. |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getCurrency() |
||
339 | |||
340 | /** |
||
341 | * Sets the value of currency. |
||
342 | * |
||
343 | * @param string $currency the currency |
||
344 | * |
||
345 | * @return self |
||
346 | */ |
||
347 | public function setCurrency($currency) |
||
353 | |||
354 | /** |
||
355 | * {@inheritdoc} |
||
356 | */ |
||
357 | public function getDiscountTotal() |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public function setDiscountTotal($discountTotal) |
||
371 | |||
372 | public function addDiscount($discount) |
||
380 | |||
381 | public function hasDiscount($discount) |
||
385 | |||
386 | public function removeDiscount($discount) |
||
394 | |||
395 | /** |
||
396 | * Gets the discounts. |
||
397 | * |
||
398 | * @return Collection |
||
399 | */ |
||
400 | public function getDiscounts() |
||
404 | |||
405 | /** |
||
406 | * Sets the discounts. |
||
407 | * |
||
408 | * @param Collection $discounts the discounts |
||
409 | * |
||
410 | * @return self |
||
411 | */ |
||
412 | public function setDiscounts(Collection $discounts) |
||
418 | |||
419 | /** |
||
420 | * Calculates total order price including |
||
421 | * all discounts. |
||
422 | * |
||
423 | * @return self |
||
424 | */ |
||
425 | public function calculateTotal() |
||
435 | |||
436 | /** |
||
437 | * Calculates all items discounts and unit prices. |
||
438 | * |
||
439 | * @return self |
||
440 | */ |
||
441 | public function calculateItemsTotal() |
||
456 | |||
457 | /** |
||
458 | * Gets the value of itemsTotal. |
||
459 | * |
||
460 | * @return int |
||
461 | */ |
||
462 | public function getItemsTotal() |
||
466 | |||
467 | /** |
||
468 | * Sets the value of itemsTotal. |
||
469 | * |
||
470 | * @param int $itemsTotal the items total |
||
471 | * |
||
472 | * @return self |
||
473 | */ |
||
474 | public function setItemsTotal($itemsTotal) |
||
480 | |||
481 | /** |
||
482 | * Gets the payments. |
||
483 | * |
||
484 | * @return Payment |
||
485 | */ |
||
486 | public function getPayments() |
||
490 | |||
491 | public function getPaymentState() |
||
495 | |||
496 | public function setPaymentState($paymentState) |
||
500 | |||
501 | public function addPayment(PaymentInterface $payment) |
||
509 | |||
510 | public function removePayment(PaymentInterface $payment) |
||
517 | |||
518 | public function hasPayment(PaymentInterface $payment) |
||
522 | |||
523 | public function getOrder() |
||
527 | } |
||
528 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.