Complex classes like Cart 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 Cart, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
71 | class Cart extends ExtendCart implements |
||
72 | ChannelAwareInterface, |
||
73 | FirstNameInterface, |
||
74 | LastNameInterface, |
||
75 | OriginAwareInterface, |
||
76 | IntegrationAwareInterface |
||
77 | { |
||
78 | use IntegrationEntityTrait, OriginTrait, NamesAwareTrait, ChannelEntityTrait; |
||
79 | |||
80 | /** |
||
81 | * @var CartItem[]|Collection |
||
82 | * |
||
83 | * @ORM\OneToMany(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CartItem", |
||
84 | * mappedBy="cart", cascade={"all"}, orphanRemoval=true |
||
85 | * ) |
||
86 | * @ORM\OrderBy({"originId" = "DESC"}) |
||
87 | * @ConfigField( |
||
88 | * defaultValues={ |
||
89 | * "importexport"={ |
||
90 | * "full"=true |
||
91 | * } |
||
92 | * } |
||
93 | * ) |
||
94 | */ |
||
95 | protected $cartItems; |
||
96 | |||
97 | /** |
||
98 | * @ORM\ManyToOne(targetEntity="Customer", inversedBy="carts") |
||
99 | * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="CASCADE") |
||
100 | */ |
||
101 | protected $customer; |
||
102 | |||
103 | /** |
||
104 | * @var Store |
||
105 | * |
||
106 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\Store") |
||
107 | * @ORM\JoinColumn(name="store_id", referencedColumnName="id", onDelete="SET NULL") |
||
108 | * @ConfigField( |
||
109 | * defaultValues={ |
||
110 | * "importexport"={ |
||
111 | * "full"=false |
||
112 | * } |
||
113 | * } |
||
114 | * ) |
||
115 | */ |
||
116 | protected $store; |
||
117 | |||
118 | /** |
||
119 | * Total items qty |
||
120 | * |
||
121 | * @var float |
||
122 | * |
||
123 | * @ORM\Column(name="items_qty", type="float") |
||
124 | */ |
||
125 | protected $itemsQty; |
||
126 | |||
127 | /** |
||
128 | * Items count |
||
129 | * |
||
130 | * @var integer |
||
131 | * |
||
132 | * @ORM\Column(name="items_count", type="integer", options={"unsigned"=true}) |
||
133 | */ |
||
134 | protected $itemsCount; |
||
135 | |||
136 | /** |
||
137 | * @var string |
||
138 | * |
||
139 | * @ORM\Column(name="base_currency_code", type="string", length=32, nullable=false) |
||
140 | */ |
||
141 | protected $baseCurrencyCode; |
||
142 | |||
143 | /** |
||
144 | * @var string |
||
145 | * |
||
146 | * @ORM\Column(name="store_currency_code", type="string", length=32, nullable=false) |
||
147 | */ |
||
148 | protected $storeCurrencyCode; |
||
149 | |||
150 | /** |
||
151 | * @var string |
||
152 | * |
||
153 | * @ORM\Column(name="quote_currency_code", type="string", length=32, nullable=false) |
||
154 | */ |
||
155 | protected $quoteCurrencyCode; |
||
156 | |||
157 | /** |
||
158 | * @var float |
||
159 | * |
||
160 | * @ORM\Column(name="store_to_base_rate", type="float", nullable=false) |
||
161 | */ |
||
162 | protected $storeToBaseRate; |
||
163 | |||
164 | /** |
||
165 | * @var float |
||
166 | * |
||
167 | * @ORM\Column(name="store_to_quote_rate", type="float", nullable=true) |
||
168 | */ |
||
169 | protected $storeToQuoteRate; |
||
170 | |||
171 | /** |
||
172 | * @var string |
||
173 | * |
||
174 | * @ORM\Column(name="email", type="string", length=255, nullable=true) |
||
175 | * @ConfigField( |
||
176 | * defaultValues={ |
||
177 | * "entity"={ |
||
178 | * "contact_information"="email" |
||
179 | * } |
||
180 | * } |
||
181 | * ) |
||
182 | */ |
||
183 | protected $email; |
||
184 | |||
185 | /** |
||
186 | * @var string |
||
187 | * |
||
188 | * @ORM\Column(name="gift_message", type="string", length=255, nullable=true) |
||
189 | */ |
||
190 | protected $giftMessage; |
||
191 | |||
192 | /** |
||
193 | * @var float |
||
194 | * |
||
195 | * @ORM\Column(name="is_guest", type="boolean") |
||
196 | */ |
||
197 | protected $isGuest; |
||
198 | |||
199 | /** |
||
200 | * @var CartAddress $shippingAddress |
||
201 | * |
||
202 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CartAddress", cascade={"persist", "remove"}) |
||
203 | * @ORM\JoinColumn(name="shipping_address_id", referencedColumnName="id", onDelete="SET NULL") |
||
204 | * @ConfigField( |
||
205 | * defaultValues={ |
||
206 | * "importexport"={ |
||
207 | * "full"=true |
||
208 | * } |
||
209 | * } |
||
210 | * ) |
||
211 | */ |
||
212 | protected $shippingAddress; |
||
213 | |||
214 | /** |
||
215 | * @var CartAddress $billingAddress |
||
216 | * |
||
217 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CartAddress", cascade={"persist", "remove"}) |
||
218 | * @ORM\JoinColumn(name="billing_address_id", referencedColumnName="id", onDelete="SET NULL") |
||
219 | * @ConfigField( |
||
220 | * defaultValues={ |
||
221 | * "importexport"={ |
||
222 | * "full"=true |
||
223 | * } |
||
224 | * } |
||
225 | * ) |
||
226 | */ |
||
227 | protected $billingAddress; |
||
228 | |||
229 | /** |
||
230 | * @var string |
||
231 | * |
||
232 | * @ORM\Column(name="payment_details", type="string", length=255, nullable=true) |
||
233 | */ |
||
234 | protected $paymentDetails; |
||
235 | |||
236 | /** |
||
237 | * @var CartStatus |
||
238 | * |
||
239 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CartStatus") |
||
240 | * @ORM\JoinColumn(name="status_name", referencedColumnName="name", onDelete="SET NULL") |
||
241 | * @ConfigField( |
||
242 | * defaultValues={ |
||
243 | * "importexport"={ |
||
244 | * "full"=false |
||
245 | * } |
||
246 | * } |
||
247 | * ) |
||
248 | */ |
||
249 | protected $status; |
||
250 | |||
251 | /** |
||
252 | * @var Opportunity |
||
253 | * |
||
254 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\SalesBundle\Entity\Opportunity") |
||
255 | * @ORM\JoinColumn(name="opportunity_id", referencedColumnName="id", onDelete="SET NULL") |
||
256 | */ |
||
257 | protected $opportunity; |
||
258 | |||
259 | /** |
||
260 | * @var string |
||
261 | * |
||
262 | * @ORM\Column(name="notes", type="text", nullable=true) |
||
263 | */ |
||
264 | protected $notes; |
||
265 | |||
266 | /** |
||
267 | * @var string |
||
268 | * |
||
269 | * @ORM\Column(name="status_message", type="string", length=255, nullable=true) |
||
270 | */ |
||
271 | protected $statusMessage; |
||
272 | |||
273 | /** |
||
274 | * @var User |
||
275 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User") |
||
276 | * @ORM\JoinColumn(name="user_owner_id", referencedColumnName="id", onDelete="SET NULL") |
||
277 | */ |
||
278 | protected $owner; |
||
279 | |||
280 | /** |
||
281 | * @var Organization |
||
282 | * |
||
283 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization") |
||
284 | * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL") |
||
285 | */ |
||
286 | protected $organization; |
||
287 | |||
288 | /** |
||
289 | * @var \DateTime |
||
290 | * |
||
291 | * @ORM\Column(type="datetime", name="imported_at", nullable=true) |
||
292 | */ |
||
293 | protected $importedAt; |
||
294 | |||
295 | /** |
||
296 | * @var \DateTime |
||
297 | * |
||
298 | * @ORM\Column(type="datetime", name="synced_at", nullable=true) |
||
299 | */ |
||
300 | protected $syncedAt; |
||
301 | |||
302 | public function __construct() |
||
309 | |||
310 | /** |
||
311 | * @return CartItem[]|Collection |
||
312 | */ |
||
313 | public function getCartItems() |
||
317 | |||
318 | /** |
||
319 | * @param CartItem[]|Collection $cartItems |
||
320 | */ |
||
321 | public function setCartItems(Collection $cartItems) |
||
325 | |||
326 | /** |
||
327 | * @param Store $store |
||
328 | */ |
||
329 | public function setStore($store) |
||
333 | |||
334 | /** |
||
335 | * @return Store |
||
336 | */ |
||
337 | public function getStore() |
||
341 | |||
342 | /** |
||
343 | * @return Customer |
||
344 | */ |
||
345 | public function getCustomer() |
||
349 | |||
350 | /** |
||
351 | * @param Customer|null $customer |
||
352 | * |
||
353 | * @return Cart |
||
354 | */ |
||
355 | public function setCustomer(Customer $customer = null) |
||
361 | |||
362 | /** |
||
363 | * @param CartAddress $shippingAddress |
||
364 | * |
||
365 | * @return Cart |
||
366 | */ |
||
367 | public function setShippingAddress(CartAddress $shippingAddress = null) |
||
373 | |||
374 | /** |
||
375 | * @param CartAddress $billingAddress |
||
376 | * |
||
377 | * @return Cart |
||
378 | */ |
||
379 | public function setBillingAddress(CartAddress $billingAddress = null) |
||
385 | |||
386 | /** |
||
387 | * @return CartAddress |
||
388 | */ |
||
389 | public function getBillingAddress() |
||
393 | |||
394 | /** |
||
395 | * @return CartAddress |
||
396 | */ |
||
397 | public function getShippingAddress() |
||
401 | |||
402 | /** |
||
403 | * @return string |
||
404 | */ |
||
405 | public function getEmail() |
||
409 | |||
410 | /** |
||
411 | * @param string $email |
||
412 | * |
||
413 | * @return Cart |
||
414 | */ |
||
415 | public function setEmail($email) |
||
421 | |||
422 | /** |
||
423 | * @return float |
||
424 | */ |
||
425 | public function getItemsQty() |
||
429 | |||
430 | /** |
||
431 | * @param float $itemsQty |
||
432 | * |
||
433 | * @return Cart |
||
434 | */ |
||
435 | public function setItemsQty($itemsQty) |
||
441 | |||
442 | /** |
||
443 | * @return float |
||
444 | */ |
||
445 | public function getSubTotal() |
||
449 | |||
450 | /** |
||
451 | * @return string |
||
452 | */ |
||
453 | public function getQuoteCurrencyCode() |
||
457 | |||
458 | /** |
||
459 | * @param string $quoteCurrencyCode |
||
460 | * |
||
461 | * @return Cart |
||
462 | */ |
||
463 | public function setQuoteCurrencyCode($quoteCurrencyCode) |
||
469 | |||
470 | /** |
||
471 | * @param string $paymentDetails |
||
472 | */ |
||
473 | public function setPaymentDetails($paymentDetails) |
||
477 | |||
478 | /** |
||
479 | * @return string |
||
480 | */ |
||
481 | public function getPaymentDetails() |
||
485 | |||
486 | /** |
||
487 | * @param CartStatus $status |
||
488 | * |
||
489 | * @return Cart |
||
490 | */ |
||
491 | public function setStatus($status) |
||
497 | |||
498 | /** |
||
499 | * @return CartStatus |
||
500 | */ |
||
501 | public function getStatus() |
||
505 | |||
506 | /** |
||
507 | * @param string $baseCurrencyCode |
||
508 | * |
||
509 | * @return Cart |
||
510 | */ |
||
511 | public function setBaseCurrencyCode($baseCurrencyCode) |
||
517 | |||
518 | /** |
||
519 | * @return string |
||
520 | */ |
||
521 | public function getBaseCurrencyCode() |
||
525 | |||
526 | /** |
||
527 | * @param string $giftMessage |
||
528 | * |
||
529 | * @return Cart |
||
530 | */ |
||
531 | public function setGiftMessage($giftMessage) |
||
537 | |||
538 | /** |
||
539 | * @return string |
||
540 | */ |
||
541 | public function getGiftMessage() |
||
545 | |||
546 | /** |
||
547 | * @param float $isGuest |
||
548 | * |
||
549 | * @return Cart |
||
550 | */ |
||
551 | public function setIsGuest($isGuest) |
||
557 | |||
558 | /** |
||
559 | * @return float |
||
560 | */ |
||
561 | public function getIsGuest() |
||
565 | |||
566 | /** |
||
567 | * @param int $itemsCount |
||
568 | * |
||
569 | * @return Cart |
||
570 | */ |
||
571 | public function setItemsCount($itemsCount) |
||
577 | |||
578 | /** |
||
579 | * @return int |
||
580 | */ |
||
581 | public function getItemsCount() |
||
585 | |||
586 | /** |
||
587 | * @param string $storeCurrencyCode |
||
588 | * |
||
589 | * @return Cart |
||
590 | */ |
||
591 | public function setStoreCurrencyCode($storeCurrencyCode) |
||
597 | |||
598 | /** |
||
599 | * @return string |
||
600 | */ |
||
601 | public function getStoreCurrencyCode() |
||
605 | |||
606 | /** |
||
607 | * @param float $storeToBaseRate |
||
608 | * |
||
609 | * @return Cart |
||
610 | */ |
||
611 | public function setStoreToBaseRate($storeToBaseRate) |
||
617 | |||
618 | /** |
||
619 | * @return float |
||
620 | */ |
||
621 | public function getStoreToBaseRate() |
||
625 | |||
626 | /** |
||
627 | * @param float $storeToQuoteRate |
||
628 | * |
||
629 | * @return Cart |
||
630 | */ |
||
631 | public function setStoreToQuoteRate($storeToQuoteRate) |
||
637 | |||
638 | /** |
||
639 | * @return float |
||
640 | */ |
||
641 | public function getStoreToQuoteRate() |
||
645 | |||
646 | /** |
||
647 | * @param Opportunity $opportunity |
||
648 | * |
||
649 | * @return Cart |
||
650 | */ |
||
651 | public function setOpportunity($opportunity) |
||
657 | |||
658 | /** |
||
659 | * @return Opportunity |
||
660 | */ |
||
661 | public function getOpportunity() |
||
665 | |||
666 | /** |
||
667 | * @param string $notes |
||
668 | * |
||
669 | * @return Cart |
||
670 | */ |
||
671 | public function setNotes($notes) |
||
676 | |||
677 | /** |
||
678 | * @return string |
||
679 | */ |
||
680 | public function getNotes() |
||
684 | |||
685 | /** |
||
686 | * Pre persist event listener |
||
687 | * |
||
688 | * @ORM\PrePersist |
||
689 | */ |
||
690 | public function beforeSave() |
||
694 | |||
695 | /** |
||
696 | * Pre update event handler |
||
697 | * |
||
698 | * @ORM\PreUpdate |
||
699 | */ |
||
700 | public function doPreUpdate() |
||
704 | |||
705 | /** |
||
706 | * @param string $statusMessage |
||
707 | */ |
||
708 | public function setStatusMessage($statusMessage) |
||
712 | |||
713 | /** |
||
714 | * @return string |
||
715 | */ |
||
716 | public function getStatusMessage() |
||
720 | |||
721 | /** |
||
722 | * @return User |
||
723 | */ |
||
724 | public function getOwner() |
||
728 | |||
729 | /** |
||
730 | * @param User $user |
||
731 | */ |
||
732 | public function setOwner(User $user) |
||
736 | |||
737 | /** |
||
738 | * Set organization |
||
739 | * |
||
740 | * @param Organization $organization |
||
741 | * @return Cart |
||
742 | */ |
||
743 | public function setOrganization(Organization $organization = null) |
||
749 | |||
750 | /** |
||
751 | * Get organization |
||
752 | * |
||
753 | * @return Organization |
||
754 | */ |
||
755 | public function getOrganization() |
||
759 | |||
760 | /** |
||
761 | * @return \DateTime |
||
762 | */ |
||
763 | public function getSyncedAt() |
||
767 | |||
768 | /** |
||
769 | * @param \DateTime $syncedAt |
||
770 | * @return Customer |
||
771 | */ |
||
772 | public function setSyncedAt(\DateTime $syncedAt) |
||
778 | |||
779 | /** |
||
780 | * @return \DateTime |
||
781 | */ |
||
782 | public function getImportedAt() |
||
786 | |||
787 | /** |
||
788 | * @param \DateTime $importedAt |
||
789 | * @return Customer |
||
790 | */ |
||
791 | public function setImportedAt(\DateTime $importedAt) |
||
797 | |||
798 | /** |
||
799 | * @return string |
||
800 | */ |
||
801 | public function __toString() |
||
805 | } |
||
806 |