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 |
||
67 | class Cart extends ExtendCart implements |
||
68 | ChannelAwareInterface, |
||
69 | FirstNameInterface, |
||
70 | LastNameInterface, |
||
71 | OriginAwareInterface, |
||
72 | IntegrationAwareInterface |
||
73 | { |
||
74 | use IntegrationEntityTrait, OriginTrait, NamesAwareTrait, ChannelEntityTrait; |
||
75 | |||
76 | /** |
||
77 | * @var CartItem[]|Collection |
||
78 | * |
||
79 | * @ORM\OneToMany(targetEntity="Oro\Bundle\MagentoBundle\Entity\CartItem", |
||
80 | * mappedBy="cart", cascade={"all"}, orphanRemoval=true |
||
81 | * ) |
||
82 | * @ORM\OrderBy({"originId" = "DESC"}) |
||
83 | * @ConfigField( |
||
84 | * defaultValues={ |
||
85 | * "importexport"={ |
||
86 | * "full"=true |
||
87 | * } |
||
88 | * } |
||
89 | * ) |
||
90 | */ |
||
91 | protected $cartItems; |
||
92 | |||
93 | /** |
||
94 | * @ORM\ManyToOne(targetEntity="Customer", inversedBy="carts") |
||
95 | * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="CASCADE") |
||
96 | */ |
||
97 | protected $customer; |
||
98 | |||
99 | /** |
||
100 | * @var Store |
||
101 | * |
||
102 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\MagentoBundle\Entity\Store") |
||
103 | * @ORM\JoinColumn(name="store_id", referencedColumnName="id", onDelete="SET NULL") |
||
104 | * @ConfigField( |
||
105 | * defaultValues={ |
||
106 | * "importexport"={ |
||
107 | * "full"=false |
||
108 | * } |
||
109 | * } |
||
110 | * ) |
||
111 | */ |
||
112 | protected $store; |
||
113 | |||
114 | /** |
||
115 | * Total items qty |
||
116 | * |
||
117 | * @var float |
||
118 | * |
||
119 | * @ORM\Column(name="items_qty", type="float") |
||
120 | */ |
||
121 | protected $itemsQty; |
||
122 | |||
123 | /** |
||
124 | * Items count |
||
125 | * |
||
126 | * @var integer |
||
127 | * |
||
128 | * @ORM\Column(name="items_count", type="integer", options={"unsigned"=true}) |
||
129 | */ |
||
130 | protected $itemsCount; |
||
131 | |||
132 | /** |
||
133 | * @var string |
||
134 | * |
||
135 | * @ORM\Column(name="base_currency_code", type="string", length=32, nullable=false) |
||
136 | */ |
||
137 | protected $baseCurrencyCode; |
||
138 | |||
139 | /** |
||
140 | * @var string |
||
141 | * |
||
142 | * @ORM\Column(name="store_currency_code", type="string", length=32, nullable=false) |
||
143 | */ |
||
144 | protected $storeCurrencyCode; |
||
145 | |||
146 | /** |
||
147 | * @var string |
||
148 | * |
||
149 | * @ORM\Column(name="quote_currency_code", type="string", length=32, nullable=false) |
||
150 | */ |
||
151 | protected $quoteCurrencyCode; |
||
152 | |||
153 | /** |
||
154 | * @var float |
||
155 | * |
||
156 | * @ORM\Column(name="store_to_base_rate", type="float", nullable=false) |
||
157 | */ |
||
158 | protected $storeToBaseRate; |
||
159 | |||
160 | /** |
||
161 | * @var float |
||
162 | * |
||
163 | * @ORM\Column(name="store_to_quote_rate", type="float", nullable=true) |
||
164 | */ |
||
165 | protected $storeToQuoteRate; |
||
166 | |||
167 | /** |
||
168 | * @var string |
||
169 | * |
||
170 | * @ORM\Column(name="email", type="string", length=255, nullable=true) |
||
171 | * @ConfigField( |
||
172 | * defaultValues={ |
||
173 | * "entity"={ |
||
174 | * "contact_information"="email" |
||
175 | * } |
||
176 | * } |
||
177 | * ) |
||
178 | */ |
||
179 | protected $email; |
||
180 | |||
181 | /** |
||
182 | * @var string |
||
183 | * |
||
184 | * @ORM\Column(name="gift_message", type="string", length=255, nullable=true) |
||
185 | */ |
||
186 | protected $giftMessage; |
||
187 | |||
188 | /** |
||
189 | * @var float |
||
190 | * |
||
191 | * @ORM\Column(name="is_guest", type="boolean") |
||
192 | */ |
||
193 | protected $isGuest; |
||
194 | |||
195 | /** |
||
196 | * @var CartAddress $shippingAddress |
||
197 | * |
||
198 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\MagentoBundle\Entity\CartAddress", cascade={"persist", "remove"}) |
||
199 | * @ORM\JoinColumn(name="shipping_address_id", referencedColumnName="id", onDelete="SET NULL") |
||
200 | * @ConfigField( |
||
201 | * defaultValues={ |
||
202 | * "importexport"={ |
||
203 | * "full"=true |
||
204 | * } |
||
205 | * } |
||
206 | * ) |
||
207 | */ |
||
208 | protected $shippingAddress; |
||
209 | |||
210 | /** |
||
211 | * @var CartAddress $billingAddress |
||
212 | * |
||
213 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\MagentoBundle\Entity\CartAddress", cascade={"persist", "remove"}) |
||
214 | * @ORM\JoinColumn(name="billing_address_id", referencedColumnName="id", onDelete="SET NULL") |
||
215 | * @ConfigField( |
||
216 | * defaultValues={ |
||
217 | * "importexport"={ |
||
218 | * "full"=true |
||
219 | * } |
||
220 | * } |
||
221 | * ) |
||
222 | */ |
||
223 | protected $billingAddress; |
||
224 | |||
225 | /** |
||
226 | * @var string |
||
227 | * |
||
228 | * @ORM\Column(name="payment_details", type="string", length=255, nullable=true) |
||
229 | */ |
||
230 | protected $paymentDetails; |
||
231 | |||
232 | /** |
||
233 | * @var CartStatus |
||
234 | * |
||
235 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\MagentoBundle\Entity\CartStatus") |
||
236 | * @ORM\JoinColumn(name="status_name", referencedColumnName="name", onDelete="SET NULL") |
||
237 | * @ConfigField( |
||
238 | * defaultValues={ |
||
239 | * "importexport"={ |
||
240 | * "full"=false |
||
241 | * } |
||
242 | * } |
||
243 | * ) |
||
244 | */ |
||
245 | protected $status; |
||
246 | |||
247 | /** |
||
248 | * @var Opportunity |
||
249 | * |
||
250 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\SalesBundle\Entity\Opportunity") |
||
251 | * @ORM\JoinColumn(name="opportunity_id", referencedColumnName="id", onDelete="SET NULL") |
||
252 | */ |
||
253 | protected $opportunity; |
||
254 | |||
255 | /** |
||
256 | * @var string |
||
257 | * |
||
258 | * @ORM\Column(name="notes", type="text", nullable=true) |
||
259 | */ |
||
260 | protected $notes; |
||
261 | |||
262 | /** |
||
263 | * @var string |
||
264 | * |
||
265 | * @ORM\Column(name="status_message", type="string", length=255, nullable=true) |
||
266 | */ |
||
267 | protected $statusMessage; |
||
268 | |||
269 | /** |
||
270 | * @var User |
||
271 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User") |
||
272 | * @ORM\JoinColumn(name="user_owner_id", referencedColumnName="id", onDelete="SET NULL") |
||
273 | */ |
||
274 | protected $owner; |
||
275 | |||
276 | /** |
||
277 | * @var Organization |
||
278 | * |
||
279 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization") |
||
280 | * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL") |
||
281 | */ |
||
282 | protected $organization; |
||
283 | |||
284 | /** |
||
285 | * @var \DateTime |
||
286 | * |
||
287 | * @ORM\Column(type="datetime", name="imported_at", nullable=true) |
||
288 | */ |
||
289 | protected $importedAt; |
||
290 | |||
291 | /** |
||
292 | * @var \DateTime |
||
293 | * |
||
294 | * @ORM\Column(type="datetime", name="synced_at", nullable=true) |
||
295 | */ |
||
296 | protected $syncedAt; |
||
297 | |||
298 | public function __construct() |
||
305 | |||
306 | /** |
||
307 | * @return CartItem[]|Collection |
||
308 | */ |
||
309 | public function getCartItems() |
||
313 | |||
314 | /** |
||
315 | * @param CartItem[]|Collection $cartItems |
||
316 | */ |
||
317 | public function setCartItems(Collection $cartItems) |
||
321 | |||
322 | /** |
||
323 | * @param CartItem $cartItem |
||
324 | * |
||
325 | * @return $this |
||
326 | */ |
||
327 | public function addCartItem(CartItem $cartItem) |
||
336 | |||
337 | /** |
||
338 | * @param CartItem $cartItem |
||
339 | * |
||
340 | * @return $this |
||
341 | */ |
||
342 | public function removeCartItem(CartItem $cartItem) |
||
350 | |||
351 | /** |
||
352 | * @param Store $store |
||
353 | */ |
||
354 | public function setStore($store) |
||
358 | |||
359 | /** |
||
360 | * @return Store |
||
361 | */ |
||
362 | public function getStore() |
||
366 | |||
367 | /** |
||
368 | * @return Customer |
||
369 | */ |
||
370 | public function getCustomer() |
||
374 | |||
375 | /** |
||
376 | * @param Customer|null $customer |
||
377 | * |
||
378 | * @return Cart |
||
379 | */ |
||
380 | public function setCustomer(Customer $customer = null) |
||
386 | |||
387 | /** |
||
388 | * @param CartAddress $shippingAddress |
||
389 | * |
||
390 | * @return Cart |
||
391 | */ |
||
392 | public function setShippingAddress(CartAddress $shippingAddress = null) |
||
398 | |||
399 | /** |
||
400 | * @param CartAddress $billingAddress |
||
401 | * |
||
402 | * @return Cart |
||
403 | */ |
||
404 | public function setBillingAddress(CartAddress $billingAddress = null) |
||
410 | |||
411 | /** |
||
412 | * @return CartAddress |
||
413 | */ |
||
414 | public function getBillingAddress() |
||
418 | |||
419 | /** |
||
420 | * @return CartAddress |
||
421 | */ |
||
422 | public function getShippingAddress() |
||
426 | |||
427 | /** |
||
428 | * @return string |
||
429 | */ |
||
430 | public function getEmail() |
||
434 | |||
435 | /** |
||
436 | * @param string $email |
||
437 | * |
||
438 | * @return Cart |
||
439 | */ |
||
440 | public function setEmail($email) |
||
446 | |||
447 | /** |
||
448 | * @return float |
||
449 | */ |
||
450 | public function getItemsQty() |
||
454 | |||
455 | /** |
||
456 | * @param float $itemsQty |
||
457 | * |
||
458 | * @return Cart |
||
459 | */ |
||
460 | public function setItemsQty($itemsQty) |
||
466 | |||
467 | /** |
||
468 | * @return float |
||
469 | */ |
||
470 | public function getSubTotal() |
||
474 | |||
475 | /** |
||
476 | * @return string |
||
477 | */ |
||
478 | public function getQuoteCurrencyCode() |
||
482 | |||
483 | /** |
||
484 | * @param string $quoteCurrencyCode |
||
485 | * |
||
486 | * @return Cart |
||
487 | */ |
||
488 | public function setQuoteCurrencyCode($quoteCurrencyCode) |
||
494 | |||
495 | /** |
||
496 | * @param string $paymentDetails |
||
497 | */ |
||
498 | public function setPaymentDetails($paymentDetails) |
||
502 | |||
503 | /** |
||
504 | * @return string |
||
505 | */ |
||
506 | public function getPaymentDetails() |
||
510 | |||
511 | /** |
||
512 | * @param CartStatus $status |
||
513 | * |
||
514 | * @return Cart |
||
515 | */ |
||
516 | public function setStatus($status) |
||
522 | |||
523 | /** |
||
524 | * @return CartStatus |
||
525 | */ |
||
526 | public function getStatus() |
||
530 | |||
531 | /** |
||
532 | * @param string $baseCurrencyCode |
||
533 | * |
||
534 | * @return Cart |
||
535 | */ |
||
536 | public function setBaseCurrencyCode($baseCurrencyCode) |
||
542 | |||
543 | /** |
||
544 | * @return string |
||
545 | */ |
||
546 | public function getBaseCurrencyCode() |
||
550 | |||
551 | /** |
||
552 | * @param string $giftMessage |
||
553 | * |
||
554 | * @return Cart |
||
555 | */ |
||
556 | public function setGiftMessage($giftMessage) |
||
562 | |||
563 | /** |
||
564 | * @return string |
||
565 | */ |
||
566 | public function getGiftMessage() |
||
570 | |||
571 | /** |
||
572 | * @param float $isGuest |
||
573 | * |
||
574 | * @return Cart |
||
575 | */ |
||
576 | public function setIsGuest($isGuest) |
||
582 | |||
583 | /** |
||
584 | * @return float |
||
585 | */ |
||
586 | public function getIsGuest() |
||
590 | |||
591 | /** |
||
592 | * @param int $itemsCount |
||
593 | * |
||
594 | * @return Cart |
||
595 | */ |
||
596 | public function setItemsCount($itemsCount) |
||
602 | |||
603 | /** |
||
604 | * @return int |
||
605 | */ |
||
606 | public function getItemsCount() |
||
610 | |||
611 | /** |
||
612 | * @param string $storeCurrencyCode |
||
613 | * |
||
614 | * @return Cart |
||
615 | */ |
||
616 | public function setStoreCurrencyCode($storeCurrencyCode) |
||
622 | |||
623 | /** |
||
624 | * @return string |
||
625 | */ |
||
626 | public function getStoreCurrencyCode() |
||
630 | |||
631 | /** |
||
632 | * @param float $storeToBaseRate |
||
633 | * |
||
634 | * @return Cart |
||
635 | */ |
||
636 | public function setStoreToBaseRate($storeToBaseRate) |
||
642 | |||
643 | /** |
||
644 | * @return float |
||
645 | */ |
||
646 | public function getStoreToBaseRate() |
||
650 | |||
651 | /** |
||
652 | * @param float $storeToQuoteRate |
||
653 | * |
||
654 | * @return Cart |
||
655 | */ |
||
656 | public function setStoreToQuoteRate($storeToQuoteRate) |
||
662 | |||
663 | /** |
||
664 | * @return float |
||
665 | */ |
||
666 | public function getStoreToQuoteRate() |
||
670 | |||
671 | /** |
||
672 | * @param Opportunity $opportunity |
||
673 | * |
||
674 | * @return Cart |
||
675 | */ |
||
676 | public function setOpportunity($opportunity) |
||
682 | |||
683 | /** |
||
684 | * @return Opportunity |
||
685 | */ |
||
686 | public function getOpportunity() |
||
690 | |||
691 | /** |
||
692 | * @param string $notes |
||
693 | * |
||
694 | * @return Cart |
||
695 | */ |
||
696 | public function setNotes($notes) |
||
701 | |||
702 | /** |
||
703 | * @return string |
||
704 | */ |
||
705 | public function getNotes() |
||
709 | |||
710 | /** |
||
711 | * Pre persist event listener |
||
712 | * |
||
713 | * @ORM\PrePersist |
||
714 | */ |
||
715 | public function beforeSave() |
||
719 | |||
720 | /** |
||
721 | * Pre update event handler |
||
722 | * |
||
723 | * @ORM\PreUpdate |
||
724 | */ |
||
725 | public function doPreUpdate() |
||
729 | |||
730 | /** |
||
731 | * @param string $statusMessage |
||
732 | */ |
||
733 | public function setStatusMessage($statusMessage) |
||
737 | |||
738 | /** |
||
739 | * @return string |
||
740 | */ |
||
741 | public function getStatusMessage() |
||
745 | |||
746 | /** |
||
747 | * @return User |
||
748 | */ |
||
749 | public function getOwner() |
||
753 | |||
754 | /** |
||
755 | * @param User $user |
||
756 | */ |
||
757 | public function setOwner(User $user) |
||
761 | |||
762 | /** |
||
763 | * Set organization |
||
764 | * |
||
765 | * @param Organization $organization |
||
766 | * @return Cart |
||
767 | */ |
||
768 | public function setOrganization(Organization $organization = null) |
||
774 | |||
775 | /** |
||
776 | * Get organization |
||
777 | * |
||
778 | * @return Organization |
||
779 | */ |
||
780 | public function getOrganization() |
||
784 | |||
785 | /** |
||
786 | * @return \DateTime |
||
787 | */ |
||
788 | public function getSyncedAt() |
||
792 | |||
793 | /** |
||
794 | * @param \DateTime|null $syncedAt |
||
795 | * @return Cart |
||
796 | */ |
||
797 | public function setSyncedAt(\DateTime $syncedAt = null) |
||
803 | |||
804 | /** |
||
805 | * @return \DateTime |
||
806 | */ |
||
807 | public function getImportedAt() |
||
811 | |||
812 | /** |
||
813 | * @param \DateTime|null $importedAt |
||
814 | * @return Cart |
||
815 | */ |
||
816 | public function setImportedAt(\DateTime $importedAt = null) |
||
822 | |||
823 | /** |
||
824 | * @return string |
||
825 | */ |
||
826 | public function __toString() |
||
830 | } |
||
831 |