Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Customer 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 Customer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
74 | class Customer extends ExtendCustomer implements |
||
75 | ChannelAwareInterface, |
||
76 | CustomerIdentityInterface, |
||
77 | RFMAwareInterface, |
||
78 | OriginAwareInterface, |
||
79 | IntegrationAwareInterface |
||
80 | { |
||
81 | const SYNC_TO_MAGENTO = 1; |
||
82 | const MAGENTO_REMOVED = 2; |
||
83 | |||
84 | use IntegrationEntityTrait, OriginTrait, ChannelEntityTrait, RFMAwareTrait; |
||
85 | |||
86 | /** |
||
87 | * @var int |
||
88 | * |
||
89 | * @ORM\Id |
||
90 | * @ORM\Column(type="integer", name="id") |
||
91 | * @ORM\GeneratedValue(strategy="AUTO") |
||
92 | * @ConfigField( |
||
93 | * defaultValues={ |
||
94 | * "importexport"={ |
||
95 | * "excluded"=true |
||
96 | * } |
||
97 | * } |
||
98 | * ) |
||
99 | */ |
||
100 | protected $id; |
||
101 | |||
102 | /* |
||
103 | * FIELDS are duplicated to enable dataaudit only for customer fields |
||
104 | */ |
||
105 | /** |
||
106 | * @var string |
||
107 | * |
||
108 | * @ORM\Column(name="name_prefix", type="string", length=255, nullable=true) |
||
109 | * @Oro\Versioned |
||
110 | */ |
||
111 | protected $namePrefix; |
||
112 | |||
113 | /** |
||
114 | * @var string |
||
115 | * |
||
116 | * @ORM\Column(name="first_name", type="string", length=255, nullable=true) |
||
117 | * @Oro\Versioned |
||
118 | */ |
||
119 | protected $firstName; |
||
120 | |||
121 | /** |
||
122 | * @var string |
||
123 | * |
||
124 | * @ORM\Column(name="middle_name", type="string", length=255, nullable=true) |
||
125 | * @Oro\Versioned |
||
126 | */ |
||
127 | protected $middleName; |
||
128 | |||
129 | /** |
||
130 | * @var string |
||
131 | * |
||
132 | * @ORM\Column(name="last_name", type="string", length=255, nullable=true) |
||
133 | * @Oro\Versioned |
||
134 | */ |
||
135 | protected $lastName; |
||
136 | |||
137 | /** |
||
138 | * @var string |
||
139 | * |
||
140 | * @ORM\Column(name="name_suffix", type="string", length=255, nullable=true) |
||
141 | * @Oro\Versioned |
||
142 | */ |
||
143 | protected $nameSuffix; |
||
144 | |||
145 | /** |
||
146 | * @var string |
||
147 | * |
||
148 | * @ORM\Column(name="gender", type="string", length=8, nullable=true) |
||
149 | * @Oro\Versioned |
||
150 | */ |
||
151 | protected $gender; |
||
152 | |||
153 | /** |
||
154 | * @var \DateTime |
||
155 | * |
||
156 | * @ORM\Column(name="birthday", type="date", nullable=true) |
||
157 | * @Oro\Versioned |
||
158 | */ |
||
159 | protected $birthday; |
||
160 | |||
161 | /** |
||
162 | * @var string |
||
163 | * |
||
164 | * @ORM\Column(name="email", type="string", length=255, nullable=true) |
||
165 | * @Oro\Versioned |
||
166 | * @ConfigField( |
||
167 | * defaultValues={ |
||
168 | * "entity"={ |
||
169 | * "contact_information"="email" |
||
170 | * } |
||
171 | * } |
||
172 | * ) |
||
173 | */ |
||
174 | protected $email; |
||
175 | |||
176 | /** |
||
177 | * @var \DateTime $createdAt |
||
178 | * |
||
179 | * @ORM\Column(type="datetime", name="created_at") |
||
180 | * @Oro\Versioned |
||
181 | * @ConfigField( |
||
182 | * defaultValues={ |
||
183 | * "entity"={ |
||
184 | * "label"="oro.ui.created_at" |
||
185 | * } |
||
186 | * } |
||
187 | * ) |
||
188 | */ |
||
189 | protected $createdAt; |
||
190 | |||
191 | /** |
||
192 | * @var \DateTime $updatedAt |
||
193 | * |
||
194 | * @ORM\Column(type="datetime", name="updated_at") |
||
195 | * @Oro\Versioned |
||
196 | * @ConfigField( |
||
197 | * defaultValues={ |
||
198 | * "entity"={ |
||
199 | * "label"="oro.ui.updated_at" |
||
200 | * } |
||
201 | * } |
||
202 | * ) |
||
203 | */ |
||
204 | protected $updatedAt; |
||
205 | |||
206 | /** |
||
207 | * @var \DateTime |
||
208 | * |
||
209 | * @ORM\Column(type="datetime", name="imported_at", nullable=true) |
||
210 | * @Oro\Versioned |
||
211 | */ |
||
212 | protected $importedAt; |
||
213 | |||
214 | /** |
||
215 | * @var \DateTime |
||
216 | * |
||
217 | * @ORM\Column(type="datetime", name="synced_at", nullable=true) |
||
218 | * @Oro\Versioned |
||
219 | */ |
||
220 | protected $syncedAt; |
||
221 | |||
222 | /** |
||
223 | * @var Website |
||
224 | * |
||
225 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\Website") |
||
226 | * @ORM\JoinColumn(name="website_id", referencedColumnName="id", onDelete="SET NULL") |
||
227 | * @ConfigField( |
||
228 | * defaultValues={ |
||
229 | * "importexport"={ |
||
230 | * "full"=false |
||
231 | * } |
||
232 | * } |
||
233 | * ) |
||
234 | */ |
||
235 | protected $website; |
||
236 | |||
237 | /** |
||
238 | * @var Store |
||
239 | * |
||
240 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\Store") |
||
241 | * @ORM\JoinColumn(name="store_id", referencedColumnName="id", onDelete="SET NULL") |
||
242 | * @ConfigField( |
||
243 | * defaultValues={ |
||
244 | * "importexport"={ |
||
245 | * "full"=false |
||
246 | * } |
||
247 | * } |
||
248 | * ) |
||
249 | */ |
||
250 | protected $store; |
||
251 | |||
252 | /** |
||
253 | * @var string |
||
254 | * |
||
255 | * @ORM\Column(name="created_in", type="string", length=255, nullable=true) |
||
256 | * @Oro\Versioned |
||
257 | */ |
||
258 | protected $createdIn; |
||
259 | |||
260 | /** |
||
261 | * @var bool |
||
262 | * @ORM\Column(name="is_confirmed", type="boolean", nullable=true) |
||
263 | */ |
||
264 | protected $confirmed = true; |
||
265 | |||
266 | /** |
||
267 | * @var bool |
||
268 | * @ORM\Column(name="is_guest", type="boolean", nullable=false, options={"default"=false}) |
||
269 | */ |
||
270 | protected $guest = false; |
||
271 | |||
272 | /** |
||
273 | * @var CustomerGroup |
||
274 | * |
||
275 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CustomerGroup") |
||
276 | * @ORM\JoinColumn(name="customer_group_id", referencedColumnName="id", onDelete="SET NULL") |
||
277 | * @ConfigField( |
||
278 | * defaultValues={ |
||
279 | * "importexport"={ |
||
280 | * "full"=false |
||
281 | * } |
||
282 | * } |
||
283 | * ) |
||
284 | */ |
||
285 | protected $group; |
||
286 | |||
287 | /** |
||
288 | * @var Contact |
||
289 | * |
||
290 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\ContactBundle\Entity\Contact") |
||
291 | * @ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="SET NULL") |
||
292 | * @ConfigField( |
||
293 | * defaultValues={ |
||
294 | * "importexport"={ |
||
295 | * "excluded"=true |
||
296 | * } |
||
297 | * } |
||
298 | * ) |
||
299 | */ |
||
300 | protected $contact; |
||
301 | |||
302 | /** |
||
303 | * @var Account |
||
304 | * |
||
305 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\AccountBundle\Entity\Account") |
||
306 | * @ORM\JoinColumn(name="account_id", referencedColumnName="id", onDelete="SET NULL") |
||
307 | * @ConfigField( |
||
308 | * defaultValues={ |
||
309 | * "importexport"={ |
||
310 | * "excluded"=true |
||
311 | * } |
||
312 | * } |
||
313 | * ) |
||
314 | */ |
||
315 | protected $account; |
||
316 | |||
317 | /** |
||
318 | * @var Collection |
||
319 | * |
||
320 | * @ORM\OneToMany(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\Address", |
||
321 | * mappedBy="owner", cascade={"all"}, orphanRemoval=true |
||
322 | * ) |
||
323 | * @ORM\OrderBy({"primary" = "DESC"}) |
||
324 | * @ConfigField( |
||
325 | * defaultValues={ |
||
326 | * "importexport"={ |
||
327 | * "full"=true |
||
328 | * } |
||
329 | * } |
||
330 | * ) |
||
331 | */ |
||
332 | protected $addresses; |
||
333 | |||
334 | /** |
||
335 | * @var Collection |
||
336 | * |
||
337 | * @ORM\OneToMany(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\Cart", |
||
338 | * mappedBy="customer", cascade={"remove"}, orphanRemoval=true |
||
339 | * ) |
||
340 | * @ConfigField( |
||
341 | * defaultValues={ |
||
342 | * "importexport"={ |
||
343 | * "excluded"=true |
||
344 | * } |
||
345 | * } |
||
346 | * ) |
||
347 | */ |
||
348 | protected $carts; |
||
349 | |||
350 | /** |
||
351 | * @var Collection |
||
352 | * |
||
353 | * @ORM\OneToMany(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\Order", |
||
354 | * mappedBy="customer", cascade={"remove"}, orphanRemoval=true |
||
355 | * ) |
||
356 | * @ConfigField( |
||
357 | * defaultValues={ |
||
358 | * "importexport"={ |
||
359 | * "excluded"=true |
||
360 | * } |
||
361 | * } |
||
362 | * ) |
||
363 | */ |
||
364 | protected $orders; |
||
365 | |||
366 | /** |
||
367 | * @var boolean |
||
368 | * |
||
369 | * @ORM\Column(type="boolean", name="is_active") |
||
370 | * @ConfigField( |
||
371 | * defaultValues={ |
||
372 | * "importexport"={ |
||
373 | * "excluded"=true |
||
374 | * } |
||
375 | * } |
||
376 | * ) |
||
377 | */ |
||
378 | protected $isActive = false; |
||
379 | |||
380 | /** |
||
381 | * @var float |
||
382 | * |
||
383 | * @ORM\Column(name="vat", type="string", length=255, nullable=true) |
||
384 | * @Oro\Versioned |
||
385 | */ |
||
386 | protected $vat; |
||
387 | |||
388 | /** |
||
389 | * @var float |
||
390 | * |
||
391 | * @ORM\Column(name="lifetime", type="money", nullable=true) |
||
392 | * @ConfigField( |
||
393 | * defaultValues={ |
||
394 | * "importexport"={ |
||
395 | * "excluded"=true |
||
396 | * } |
||
397 | * } |
||
398 | * ) |
||
399 | */ |
||
400 | protected $lifetime = 0; |
||
401 | |||
402 | /** |
||
403 | * @var string |
||
404 | * |
||
405 | * @ORM\Column(name="currency", type="string", length=10, nullable=true) |
||
406 | * @ConfigField( |
||
407 | * defaultValues={ |
||
408 | * "importexport"={ |
||
409 | * "excluded"=true |
||
410 | * } |
||
411 | * } |
||
412 | * ) |
||
413 | */ |
||
414 | protected $currency; |
||
415 | |||
416 | /** |
||
417 | * @var int |
||
418 | * |
||
419 | * @ORM\Column(name="sync_state", type="integer", nullable=true) |
||
420 | * @ConfigField( |
||
421 | * defaultValues={ |
||
422 | * "importexport"={ |
||
423 | * "excluded"=true |
||
424 | * } |
||
425 | * } |
||
426 | * ) |
||
427 | */ |
||
428 | protected $syncState; |
||
429 | |||
430 | /** |
||
431 | * @var User |
||
432 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User") |
||
433 | * @ORM\JoinColumn(name="user_owner_id", referencedColumnName="id", onDelete="SET NULL") |
||
434 | * @ConfigField( |
||
435 | * defaultValues={ |
||
436 | * "importexport"={ |
||
437 | * "excluded"=true |
||
438 | * } |
||
439 | * } |
||
440 | * ) |
||
441 | */ |
||
442 | protected $owner; |
||
443 | |||
444 | /** |
||
445 | * @var Organization |
||
446 | * |
||
447 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization") |
||
448 | * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL") |
||
449 | * @ConfigField( |
||
450 | * defaultValues={ |
||
451 | * "importexport"={ |
||
452 | * "excluded"=true |
||
453 | * } |
||
454 | * } |
||
455 | * ) |
||
456 | */ |
||
457 | protected $organization; |
||
458 | |||
459 | /** |
||
460 | * @var string |
||
461 | * |
||
462 | * @ORM\Column(name="password", type="string", length=32, nullable=true) |
||
463 | */ |
||
464 | protected $password; |
||
465 | |||
466 | /** |
||
467 | * @var NewsletterSubscriber[]|Collection |
||
468 | * |
||
469 | * @ORM\OneToMany(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\NewsletterSubscriber", |
||
470 | * mappedBy="customer", cascade={"remove"}, orphanRemoval=true) |
||
471 | * @ConfigField( |
||
472 | * defaultValues={ |
||
473 | * "importexport"={ |
||
474 | * "excluded"=true |
||
475 | * } |
||
476 | * } |
||
477 | * ) |
||
478 | */ |
||
479 | protected $newsletterSubscribers; |
||
480 | |||
481 | /** |
||
482 | * {@inheritdoc} |
||
483 | */ |
||
484 | public function __construct() |
||
492 | |||
493 | /** |
||
494 | * @param Website $website |
||
495 | * |
||
496 | * @return Customer |
||
497 | */ |
||
498 | public function setWebsite(Website $website) |
||
504 | |||
505 | /** |
||
506 | * @return Website |
||
507 | */ |
||
508 | public function getWebsite() |
||
512 | |||
513 | /** |
||
514 | * @return string |
||
515 | */ |
||
516 | public function getWebsiteName() |
||
520 | |||
521 | /** |
||
522 | * @param Store $store |
||
523 | * |
||
524 | * @return Customer |
||
525 | */ |
||
526 | public function setStore(Store $store) |
||
532 | |||
533 | /** |
||
534 | * @return Store |
||
535 | */ |
||
536 | public function getStore() |
||
540 | |||
541 | /** |
||
542 | * @return string|null |
||
543 | */ |
||
544 | public function getStoreName() |
||
548 | |||
549 | /** |
||
550 | * @return bool |
||
551 | */ |
||
552 | public function isConfirmed() |
||
556 | |||
557 | /** |
||
558 | * @param bool $confirmed |
||
559 | * @return Customer |
||
560 | */ |
||
561 | public function setConfirmed($confirmed) |
||
567 | |||
568 | /** |
||
569 | * @return bool |
||
570 | */ |
||
571 | public function isGuest() |
||
575 | |||
576 | /** |
||
577 | * @param bool $guest |
||
578 | * @return Customer |
||
579 | */ |
||
580 | public function setGuest($guest) |
||
586 | |||
587 | /** |
||
588 | * @return string |
||
589 | */ |
||
590 | public function getCreatedIn() |
||
594 | |||
595 | /** |
||
596 | * @param string $createdIn |
||
597 | * @return Customer |
||
598 | */ |
||
599 | public function setCreatedIn($createdIn) |
||
605 | |||
606 | /** |
||
607 | * @param CustomerGroup $group |
||
608 | * |
||
609 | * @return Customer |
||
610 | */ |
||
611 | public function setGroup(CustomerGroup $group) |
||
617 | |||
618 | /** |
||
619 | * @return CustomerGroup |
||
620 | */ |
||
621 | public function getGroup() |
||
625 | |||
626 | /** |
||
627 | * @param Contact $contact |
||
628 | * |
||
629 | * @return Customer |
||
630 | */ |
||
631 | public function setContact($contact) |
||
637 | |||
638 | /** |
||
639 | * @return Contact |
||
640 | */ |
||
641 | public function getContact() |
||
645 | |||
646 | /** |
||
647 | * @param Account $account |
||
648 | * |
||
649 | * @return Customer |
||
650 | */ |
||
651 | public function setAccount($account) |
||
657 | |||
658 | /** |
||
659 | * @return Account |
||
660 | */ |
||
661 | public function getAccount() |
||
665 | |||
666 | /** |
||
667 | * @param string $vat |
||
668 | * |
||
669 | * @return Customer |
||
670 | */ |
||
671 | public function setVat($vat) |
||
677 | |||
678 | /** |
||
679 | * @return string |
||
680 | */ |
||
681 | public function getVat() |
||
685 | |||
686 | /** |
||
687 | * @param bool $isActive |
||
688 | * |
||
689 | * @return Customer |
||
690 | */ |
||
691 | public function setIsActive($isActive) |
||
697 | |||
698 | /** |
||
699 | * @return bool |
||
700 | */ |
||
701 | public function getIsActive() |
||
705 | |||
706 | /** |
||
707 | * @param Collection $carts |
||
708 | */ |
||
709 | public function setCarts($carts) |
||
713 | |||
714 | /** |
||
715 | * @return Collection |
||
716 | */ |
||
717 | public function getCarts() |
||
721 | |||
722 | /** |
||
723 | * @return Collection |
||
724 | */ |
||
725 | public function getOrders() |
||
729 | |||
730 | /** |
||
731 | * @param int $originId |
||
732 | * |
||
733 | * @return Address|false |
||
734 | */ |
||
735 | public function getAddressByOriginId($originId) |
||
743 | |||
744 | /** |
||
745 | * @param double $lifetime |
||
746 | * |
||
747 | * @return Customer |
||
748 | */ |
||
749 | public function setLifetime($lifetime) |
||
755 | |||
756 | /** |
||
757 | * @return double |
||
758 | */ |
||
759 | public function getLifetime() |
||
763 | |||
764 | /** |
||
765 | * @param string $currency |
||
766 | * |
||
767 | * @return Customer |
||
768 | */ |
||
769 | public function setCurrency($currency) |
||
775 | |||
776 | /** |
||
777 | * @return string |
||
778 | */ |
||
779 | public function getCurrency() |
||
783 | |||
784 | /** |
||
785 | * Add address |
||
786 | * |
||
787 | * @param AbstractAddress $address |
||
788 | * @return Customer |
||
789 | */ |
||
790 | View Code Duplication | public function addAddress(AbstractAddress $address) |
|
800 | |||
801 | /** |
||
802 | * @return User |
||
803 | */ |
||
804 | public function getOwner() |
||
808 | |||
809 | /** |
||
810 | * @param User $user |
||
811 | */ |
||
812 | public function setOwner(User $user) |
||
816 | |||
817 | /** |
||
818 | * Set organization |
||
819 | * |
||
820 | * @param Organization $organization |
||
821 | * @return Customer |
||
822 | */ |
||
823 | public function setOrganization(Organization $organization = null) |
||
829 | |||
830 | /** |
||
831 | * Get organization |
||
832 | * |
||
833 | * @return Organization |
||
834 | */ |
||
835 | public function getOrganization() |
||
839 | |||
840 | /** |
||
841 | * {@inheritdoc} |
||
842 | */ |
||
843 | public function getSyncState() |
||
847 | |||
848 | /** |
||
849 | * {@inheritdoc} |
||
850 | * |
||
851 | * @return Customer |
||
852 | */ |
||
853 | public function setSyncState($syncState) |
||
859 | |||
860 | /** |
||
861 | * @return string |
||
862 | */ |
||
863 | public function getPassword() |
||
867 | |||
868 | /** |
||
869 | * @param string $password |
||
870 | * @return Customer |
||
871 | */ |
||
872 | public function setPassword($password) |
||
878 | |||
879 | /** |
||
880 | * @param string $password |
||
881 | * @return Customer |
||
882 | */ |
||
883 | public function setGeneratedPassword($password) |
||
891 | |||
892 | /** |
||
893 | * @return string |
||
894 | */ |
||
895 | public function getGeneratedPassword() |
||
899 | |||
900 | /** |
||
901 | * @return NewsletterSubscriber[]|Collection |
||
902 | */ |
||
903 | public function getNewsletterSubscribers() |
||
907 | |||
908 | /** |
||
909 | * @param Collection|NewsletterSubscriber[] $newsletterSubscribers |
||
910 | * @return Customer |
||
911 | */ |
||
912 | public function setNewsletterSubscribers($newsletterSubscribers) |
||
918 | |||
919 | /** |
||
920 | * @return bool |
||
921 | */ |
||
922 | public function isSubscribed() |
||
932 | |||
933 | /** |
||
934 | * @return \DateTime |
||
935 | */ |
||
936 | public function getSyncedAt() |
||
940 | |||
941 | /** |
||
942 | * @param \DateTime $syncedAt |
||
943 | * @return Customer |
||
944 | */ |
||
945 | public function setSyncedAt(\DateTime $syncedAt) |
||
951 | |||
952 | /** |
||
953 | * @return \DateTime |
||
954 | */ |
||
955 | public function getImportedAt() |
||
959 | |||
960 | /** |
||
961 | * @param \DateTime $importedAt |
||
962 | * @return Customer |
||
963 | */ |
||
964 | public function setImportedAt(\DateTime $importedAt) |
||
970 | |||
971 | /** |
||
972 | * @return string |
||
973 | */ |
||
974 | public function __toString() |
||
978 | } |
||
979 |
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.