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