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 |
||
72 | class Customer extends ExtendCustomer implements |
||
73 | ChannelAwareInterface, |
||
74 | CustomerIdentityInterface, |
||
75 | RFMAwareInterface, |
||
76 | OriginAwareInterface, |
||
77 | IntegrationAwareInterface |
||
78 | { |
||
79 | const SYNC_TO_MAGENTO = 1; |
||
80 | const MAGENTO_REMOVED = 2; |
||
81 | |||
82 | use IntegrationEntityTrait, OriginTrait, ChannelEntityTrait, RFMAwareTrait; |
||
83 | |||
84 | /** |
||
85 | * @var int |
||
86 | * |
||
87 | * @ORM\Id |
||
88 | * @ORM\Column(type="integer", name="id") |
||
89 | * @ORM\GeneratedValue(strategy="AUTO") |
||
90 | * @ConfigField( |
||
91 | * defaultValues={ |
||
92 | * "importexport"={ |
||
93 | * "excluded"=true |
||
94 | * } |
||
95 | * } |
||
96 | * ) |
||
97 | */ |
||
98 | protected $id; |
||
99 | |||
100 | /* |
||
101 | * FIELDS are duplicated to enable dataaudit only for customer fields |
||
102 | */ |
||
103 | /** |
||
104 | * @var string |
||
105 | * |
||
106 | * @ORM\Column(name="name_prefix", type="string", length=255, nullable=true) |
||
107 | */ |
||
108 | protected $namePrefix; |
||
109 | |||
110 | /** |
||
111 | * @var string |
||
112 | * |
||
113 | * @ORM\Column(name="first_name", type="string", length=255, nullable=true) |
||
114 | */ |
||
115 | protected $firstName; |
||
116 | |||
117 | /** |
||
118 | * @var string |
||
119 | * |
||
120 | * @ORM\Column(name="middle_name", type="string", length=255, nullable=true) |
||
121 | */ |
||
122 | protected $middleName; |
||
123 | |||
124 | /** |
||
125 | * @var string |
||
126 | * |
||
127 | * @ORM\Column(name="last_name", type="string", length=255, nullable=true) |
||
128 | */ |
||
129 | protected $lastName; |
||
130 | |||
131 | /** |
||
132 | * @var string |
||
133 | * |
||
134 | * @ORM\Column(name="name_suffix", type="string", length=255, nullable=true) |
||
135 | */ |
||
136 | protected $nameSuffix; |
||
137 | |||
138 | /** |
||
139 | * @var string |
||
140 | * |
||
141 | * @ORM\Column(name="gender", type="string", length=8, nullable=true) |
||
142 | */ |
||
143 | protected $gender; |
||
144 | |||
145 | /** |
||
146 | * @var \DateTime |
||
147 | * |
||
148 | * @ORM\Column(name="birthday", type="date", nullable=true) |
||
149 | */ |
||
150 | protected $birthday; |
||
151 | |||
152 | /** |
||
153 | * @var string |
||
154 | * |
||
155 | * @ORM\Column(name="email", type="string", length=255, nullable=true) |
||
156 | * @ConfigField( |
||
157 | * defaultValues={ |
||
158 | * "entity"={ |
||
159 | * "contact_information"="email" |
||
160 | * } |
||
161 | * } |
||
162 | * ) |
||
163 | */ |
||
164 | protected $email; |
||
165 | |||
166 | /** |
||
167 | * @var \DateTime $createdAt |
||
168 | * |
||
169 | * @ORM\Column(type="datetime", name="created_at") |
||
170 | * @ConfigField( |
||
171 | * defaultValues={ |
||
172 | * "entity"={ |
||
173 | * "label"="oro.ui.created_at" |
||
174 | * } |
||
175 | * } |
||
176 | * ) |
||
177 | */ |
||
178 | protected $createdAt; |
||
179 | |||
180 | /** |
||
181 | * @var \DateTime $updatedAt |
||
182 | * |
||
183 | * @ORM\Column(type="datetime", name="updated_at") |
||
184 | * @ConfigField( |
||
185 | * defaultValues={ |
||
186 | * "entity"={ |
||
187 | * "label"="oro.ui.updated_at" |
||
188 | * } |
||
189 | * } |
||
190 | * ) |
||
191 | */ |
||
192 | protected $updatedAt; |
||
193 | |||
194 | /** |
||
195 | * @var \DateTime |
||
196 | * |
||
197 | * @ORM\Column(type="datetime", name="imported_at", nullable=true) |
||
198 | */ |
||
199 | protected $importedAt; |
||
200 | |||
201 | /** |
||
202 | * @var \DateTime |
||
203 | * |
||
204 | * @ORM\Column(type="datetime", name="synced_at", nullable=true) |
||
205 | */ |
||
206 | protected $syncedAt; |
||
207 | |||
208 | /** |
||
209 | * @var Website |
||
210 | * |
||
211 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\MagentoBundle\Entity\Website") |
||
212 | * @ORM\JoinColumn(name="website_id", referencedColumnName="id", onDelete="SET NULL") |
||
213 | * @ConfigField( |
||
214 | * defaultValues={ |
||
215 | * "importexport"={ |
||
216 | * "full"=false |
||
217 | * } |
||
218 | * } |
||
219 | * ) |
||
220 | */ |
||
221 | protected $website; |
||
222 | |||
223 | /** |
||
224 | * @var Store |
||
225 | * |
||
226 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\MagentoBundle\Entity\Store") |
||
227 | * @ORM\JoinColumn(name="store_id", referencedColumnName="id", onDelete="SET NULL") |
||
228 | * @ConfigField( |
||
229 | * defaultValues={ |
||
230 | * "importexport"={ |
||
231 | * "full"=false |
||
232 | * } |
||
233 | * } |
||
234 | * ) |
||
235 | */ |
||
236 | protected $store; |
||
237 | |||
238 | /** |
||
239 | * @var string |
||
240 | * |
||
241 | * @ORM\Column(name="created_in", type="string", length=255, nullable=true) |
||
242 | */ |
||
243 | protected $createdIn; |
||
244 | |||
245 | /** |
||
246 | * @var bool |
||
247 | * @ORM\Column(name="is_confirmed", type="boolean", nullable=true) |
||
248 | */ |
||
249 | protected $confirmed = true; |
||
250 | |||
251 | /** |
||
252 | * @var bool |
||
253 | * @ORM\Column(name="is_guest", type="boolean", nullable=false, options={"default"=false}) |
||
254 | */ |
||
255 | protected $guest = false; |
||
256 | |||
257 | /** |
||
258 | * @var CustomerGroup |
||
259 | * |
||
260 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\MagentoBundle\Entity\CustomerGroup") |
||
261 | * @ORM\JoinColumn(name="customer_group_id", referencedColumnName="id", onDelete="SET NULL") |
||
262 | * @ConfigField( |
||
263 | * defaultValues={ |
||
264 | * "importexport"={ |
||
265 | * "full"=false |
||
266 | * } |
||
267 | * } |
||
268 | * ) |
||
269 | */ |
||
270 | protected $group; |
||
271 | |||
272 | /** |
||
273 | * @var Contact |
||
274 | * |
||
275 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\ContactBundle\Entity\Contact") |
||
276 | * @ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="SET NULL") |
||
277 | * @ConfigField( |
||
278 | * defaultValues={ |
||
279 | * "importexport"={ |
||
280 | * "excluded"=true |
||
281 | * } |
||
282 | * } |
||
283 | * ) |
||
284 | */ |
||
285 | protected $contact; |
||
286 | |||
287 | /** |
||
288 | * @var Account |
||
289 | * |
||
290 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\AccountBundle\Entity\Account") |
||
291 | * @ORM\JoinColumn(name="account_id", referencedColumnName="id", onDelete="SET NULL") |
||
292 | * @ConfigField( |
||
293 | * defaultValues={ |
||
294 | * "importexport"={ |
||
295 | * "excluded"=true |
||
296 | * } |
||
297 | * } |
||
298 | * ) |
||
299 | */ |
||
300 | protected $account; |
||
301 | |||
302 | /** |
||
303 | * @var Collection |
||
304 | * |
||
305 | * @ORM\OneToMany(targetEntity="Oro\Bundle\MagentoBundle\Entity\Address", |
||
306 | * mappedBy="owner", cascade={"all"}, orphanRemoval=true |
||
307 | * ) |
||
308 | * @ORM\OrderBy({"primary" = "DESC"}) |
||
309 | * @ConfigField( |
||
310 | * defaultValues={ |
||
311 | * "importexport"={ |
||
312 | * "full"=true |
||
313 | * } |
||
314 | * } |
||
315 | * ) |
||
316 | */ |
||
317 | protected $addresses; |
||
318 | |||
319 | /** |
||
320 | * @var Collection |
||
321 | * |
||
322 | * @ORM\OneToMany(targetEntity="Oro\Bundle\MagentoBundle\Entity\Cart", |
||
323 | * mappedBy="customer", cascade={"remove"}, orphanRemoval=true |
||
324 | * ) |
||
325 | * @ConfigField( |
||
326 | * defaultValues={ |
||
327 | * "importexport"={ |
||
328 | * "excluded"=true |
||
329 | * } |
||
330 | * } |
||
331 | * ) |
||
332 | */ |
||
333 | protected $carts; |
||
334 | |||
335 | /** |
||
336 | * @var Collection |
||
337 | * |
||
338 | * @ORM\OneToMany(targetEntity="Oro\Bundle\MagentoBundle\Entity\Order", |
||
339 | * mappedBy="customer", cascade={"remove"}, orphanRemoval=true |
||
340 | * ) |
||
341 | * @ConfigField( |
||
342 | * defaultValues={ |
||
343 | * "importexport"={ |
||
344 | * "excluded"=true |
||
345 | * } |
||
346 | * } |
||
347 | * ) |
||
348 | */ |
||
349 | protected $orders; |
||
350 | |||
351 | /** |
||
352 | * @var boolean |
||
353 | * |
||
354 | * @ORM\Column(type="boolean", name="is_active") |
||
355 | * @ConfigField( |
||
356 | * defaultValues={ |
||
357 | * "importexport"={ |
||
358 | * "excluded"=true |
||
359 | * } |
||
360 | * } |
||
361 | * ) |
||
362 | */ |
||
363 | protected $isActive = false; |
||
364 | |||
365 | /** |
||
366 | * @var float |
||
367 | * |
||
368 | * @ORM\Column(name="vat", type="string", length=255, nullable=true) |
||
369 | */ |
||
370 | protected $vat; |
||
371 | |||
372 | /** |
||
373 | * @var float |
||
374 | * |
||
375 | * @ORM\Column(name="lifetime", type="money", nullable=true) |
||
376 | * @ConfigField( |
||
377 | * defaultValues={ |
||
378 | * "importexport"={ |
||
379 | * "excluded"=true |
||
380 | * } |
||
381 | * } |
||
382 | * ) |
||
383 | */ |
||
384 | protected $lifetime = 0; |
||
385 | |||
386 | /** |
||
387 | * @var string |
||
388 | * |
||
389 | * @ORM\Column(name="currency", type="string", length=10, nullable=true) |
||
390 | * @ConfigField( |
||
391 | * defaultValues={ |
||
392 | * "importexport"={ |
||
393 | * "excluded"=true |
||
394 | * } |
||
395 | * } |
||
396 | * ) |
||
397 | */ |
||
398 | protected $currency; |
||
399 | |||
400 | /** |
||
401 | * @var int |
||
402 | * |
||
403 | * @ORM\Column(name="sync_state", type="integer", nullable=true) |
||
404 | * @ConfigField( |
||
405 | * defaultValues={ |
||
406 | * "importexport"={ |
||
407 | * "excluded"=true |
||
408 | * } |
||
409 | * } |
||
410 | * ) |
||
411 | */ |
||
412 | protected $syncState; |
||
413 | |||
414 | /** |
||
415 | * @var User |
||
416 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User") |
||
417 | * @ORM\JoinColumn(name="user_owner_id", referencedColumnName="id", onDelete="SET NULL") |
||
418 | * @ConfigField( |
||
419 | * defaultValues={ |
||
420 | * "importexport"={ |
||
421 | * "excluded"=true |
||
422 | * } |
||
423 | * } |
||
424 | * ) |
||
425 | */ |
||
426 | protected $owner; |
||
427 | |||
428 | /** |
||
429 | * @var Organization |
||
430 | * |
||
431 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization") |
||
432 | * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL") |
||
433 | * @ConfigField( |
||
434 | * defaultValues={ |
||
435 | * "importexport"={ |
||
436 | * "excluded"=true |
||
437 | * } |
||
438 | * } |
||
439 | * ) |
||
440 | */ |
||
441 | protected $organization; |
||
442 | |||
443 | /** |
||
444 | * @var string |
||
445 | * |
||
446 | * @ORM\Column(name="password", type="string", length=32, nullable=true) |
||
447 | */ |
||
448 | protected $password; |
||
449 | |||
450 | /** |
||
451 | * @var NewsletterSubscriber[]|Collection |
||
452 | * |
||
453 | * @ORM\OneToMany(targetEntity="Oro\Bundle\MagentoBundle\Entity\NewsletterSubscriber", |
||
454 | * mappedBy="customer", cascade={"remove"}, orphanRemoval=true) |
||
455 | * @ConfigField( |
||
456 | * defaultValues={ |
||
457 | * "importexport"={ |
||
458 | * "excluded"=true |
||
459 | * } |
||
460 | * } |
||
461 | * ) |
||
462 | */ |
||
463 | protected $newsletterSubscribers; |
||
464 | |||
465 | /** |
||
466 | * {@inheritdoc} |
||
467 | */ |
||
468 | public function __construct() |
||
476 | |||
477 | /** |
||
478 | * @param Website $website |
||
479 | * |
||
480 | * @return Customer |
||
481 | */ |
||
482 | public function setWebsite(Website $website) |
||
488 | |||
489 | /** |
||
490 | * @return Website |
||
491 | */ |
||
492 | public function getWebsite() |
||
496 | |||
497 | /** |
||
498 | * @return string |
||
499 | */ |
||
500 | public function getWebsiteName() |
||
504 | |||
505 | /** |
||
506 | * @param Store $store |
||
507 | * |
||
508 | * @return Customer |
||
509 | */ |
||
510 | public function setStore(Store $store) |
||
516 | |||
517 | /** |
||
518 | * @return Store |
||
519 | */ |
||
520 | public function getStore() |
||
524 | |||
525 | /** |
||
526 | * @return string|null |
||
527 | */ |
||
528 | public function getStoreName() |
||
532 | |||
533 | /** |
||
534 | * @return bool |
||
535 | */ |
||
536 | public function isConfirmed() |
||
540 | |||
541 | /** |
||
542 | * @param bool $confirmed |
||
543 | * @return Customer |
||
544 | */ |
||
545 | public function setConfirmed($confirmed) |
||
551 | |||
552 | /** |
||
553 | * @return bool |
||
554 | */ |
||
555 | public function isGuest() |
||
559 | |||
560 | /** |
||
561 | * @param bool $guest |
||
562 | * @return Customer |
||
563 | */ |
||
564 | public function setGuest($guest) |
||
570 | |||
571 | /** |
||
572 | * @return string |
||
573 | */ |
||
574 | public function getCreatedIn() |
||
578 | |||
579 | /** |
||
580 | * @param string $createdIn |
||
581 | * @return Customer |
||
582 | */ |
||
583 | public function setCreatedIn($createdIn) |
||
589 | |||
590 | /** |
||
591 | * @param CustomerGroup $group |
||
592 | * |
||
593 | * @return Customer |
||
594 | */ |
||
595 | public function setGroup(CustomerGroup $group) |
||
601 | |||
602 | /** |
||
603 | * @return CustomerGroup |
||
604 | */ |
||
605 | public function getGroup() |
||
609 | |||
610 | /** |
||
611 | * @param Contact $contact |
||
612 | * |
||
613 | * @return Customer |
||
614 | */ |
||
615 | public function setContact($contact) |
||
621 | |||
622 | /** |
||
623 | * @return Contact |
||
624 | */ |
||
625 | public function getContact() |
||
629 | |||
630 | /** |
||
631 | * @param Account $account |
||
632 | * |
||
633 | * @return Customer |
||
634 | */ |
||
635 | public function setAccount($account) |
||
641 | |||
642 | /** |
||
643 | * @return Account |
||
644 | */ |
||
645 | public function getAccount() |
||
649 | |||
650 | /** |
||
651 | * @param string $vat |
||
652 | * |
||
653 | * @return Customer |
||
654 | */ |
||
655 | public function setVat($vat) |
||
661 | |||
662 | /** |
||
663 | * @return string |
||
664 | */ |
||
665 | public function getVat() |
||
669 | |||
670 | /** |
||
671 | * @param bool $isActive |
||
672 | * |
||
673 | * @return Customer |
||
674 | */ |
||
675 | public function setIsActive($isActive) |
||
681 | |||
682 | /** |
||
683 | * @return bool |
||
684 | */ |
||
685 | public function getIsActive() |
||
689 | |||
690 | /** |
||
691 | * @param Collection $carts |
||
692 | */ |
||
693 | public function setCarts($carts) |
||
697 | |||
698 | /** |
||
699 | * @return Collection |
||
700 | */ |
||
701 | public function getCarts() |
||
705 | |||
706 | /** |
||
707 | * @return Collection |
||
708 | */ |
||
709 | public function getOrders() |
||
713 | |||
714 | /** |
||
715 | * @param int $originId |
||
716 | * |
||
717 | * @return Address|false |
||
718 | */ |
||
719 | public function getAddressByOriginId($originId) |
||
727 | |||
728 | /** |
||
729 | * @param double $lifetime |
||
730 | * |
||
731 | * @return Customer |
||
732 | */ |
||
733 | public function setLifetime($lifetime) |
||
739 | |||
740 | /** |
||
741 | * @return double |
||
742 | */ |
||
743 | public function getLifetime() |
||
747 | |||
748 | /** |
||
749 | * @param string $currency |
||
750 | * |
||
751 | * @return Customer |
||
752 | */ |
||
753 | public function setCurrency($currency) |
||
759 | |||
760 | /** |
||
761 | * @return string |
||
762 | */ |
||
763 | public function getCurrency() |
||
767 | |||
768 | /** |
||
769 | * Add address |
||
770 | * |
||
771 | * @param AbstractAddress $address |
||
772 | * @return Customer |
||
773 | */ |
||
774 | View Code Duplication | public function addAddress(AbstractAddress $address) |
|
784 | |||
785 | /** |
||
786 | * @return User |
||
787 | */ |
||
788 | public function getOwner() |
||
792 | |||
793 | /** |
||
794 | * @param User $user |
||
795 | * |
||
796 | * @return Customer |
||
797 | */ |
||
798 | public function setOwner(User $user) |
||
804 | |||
805 | /** |
||
806 | * Set organization |
||
807 | * |
||
808 | * @param Organization $organization |
||
809 | * @return Customer |
||
810 | */ |
||
811 | public function setOrganization(Organization $organization = null) |
||
817 | |||
818 | /** |
||
819 | * Get organization |
||
820 | * |
||
821 | * @return Organization |
||
822 | */ |
||
823 | public function getOrganization() |
||
827 | |||
828 | /** |
||
829 | * {@inheritdoc} |
||
830 | */ |
||
831 | public function getSyncState() |
||
835 | |||
836 | /** |
||
837 | * {@inheritdoc} |
||
838 | * |
||
839 | * @return Customer |
||
840 | */ |
||
841 | public function setSyncState($syncState) |
||
847 | |||
848 | /** |
||
849 | * @return string |
||
850 | */ |
||
851 | public function getPassword() |
||
855 | |||
856 | /** |
||
857 | * @param string $password |
||
858 | * @return Customer |
||
859 | */ |
||
860 | public function setPassword($password) |
||
866 | |||
867 | /** |
||
868 | * @param string $password |
||
869 | * @return Customer |
||
870 | */ |
||
871 | public function setGeneratedPassword($password) |
||
879 | |||
880 | /** |
||
881 | * @return string |
||
882 | */ |
||
883 | public function getGeneratedPassword() |
||
887 | |||
888 | /** |
||
889 | * @return NewsletterSubscriber[]|Collection |
||
890 | */ |
||
891 | public function getNewsletterSubscribers() |
||
895 | |||
896 | /** |
||
897 | * @param Collection|NewsletterSubscriber[] $newsletterSubscribers |
||
898 | * @return Customer |
||
899 | */ |
||
900 | public function setNewsletterSubscribers($newsletterSubscribers) |
||
906 | |||
907 | /** |
||
908 | * @return bool |
||
909 | */ |
||
910 | public function isSubscribed() |
||
920 | |||
921 | /** |
||
922 | * @return \DateTime |
||
923 | */ |
||
924 | public function getSyncedAt() |
||
928 | |||
929 | /** |
||
930 | * @param \DateTime|null $syncedAt |
||
931 | * @return Customer |
||
932 | */ |
||
933 | public function setSyncedAt(\DateTime $syncedAt = null) |
||
939 | |||
940 | /** |
||
941 | * @return \DateTime |
||
942 | */ |
||
943 | public function getImportedAt() |
||
947 | |||
948 | /** |
||
949 | * @param \DateTime|null $importedAt |
||
950 | * @return Customer |
||
951 | */ |
||
952 | public function setImportedAt(\DateTime $importedAt = null) |
||
958 | |||
959 | /** |
||
960 | * @return string |
||
961 | */ |
||
962 | public function __toString() |
||
966 | } |
||
967 |
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.