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 CustomerAddress 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 CustomerAddress, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class CustomerAddress extends AbstractEntity |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * getShippingMultipleDefaultName |
||
| 31 | * |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | 26 | public function getShippingMultipleDefaultName() |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Set from customer. |
||
| 41 | * |
||
| 42 | * @param \Eccube\Entity\Customer $Customer |
||
| 43 | * |
||
| 44 | * @return \Eccube\Entity\CustomerAddress |
||
| 45 | */ |
||
| 46 | 26 | View Code Duplication | public function setFromCustomer(Customer $Customer) |
| 70 | |||
| 71 | /** |
||
| 72 | * Set from Shipping. |
||
| 73 | * |
||
| 74 | * @param \Eccube\Entity\Shipping $Shipping |
||
| 75 | * |
||
| 76 | * @return \Eccube\Entity\CustomerAddress |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function setFromShipping(Shipping $Shipping) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @var int |
||
| 104 | * |
||
| 105 | * @ORM\Column(name="id", type="integer", options={"unsigned":true}) |
||
| 106 | * @ORM\Id |
||
| 107 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
| 108 | */ |
||
| 109 | private $id; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string|null |
||
| 113 | * |
||
| 114 | * @ORM\Column(name="name01", type="string", length=255, nullable=true) |
||
| 115 | */ |
||
| 116 | private $name01; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var string|null |
||
| 120 | * |
||
| 121 | * @ORM\Column(name="name02", type="string", length=255, nullable=true) |
||
| 122 | */ |
||
| 123 | private $name02; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string|null |
||
| 127 | * |
||
| 128 | * @ORM\Column(name="kana01", type="string", length=255, nullable=true) |
||
| 129 | */ |
||
| 130 | private $kana01; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var string|null |
||
| 134 | * |
||
| 135 | * @ORM\Column(name="kana02", type="string", length=255, nullable=true) |
||
| 136 | */ |
||
| 137 | private $kana02; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var string|null |
||
| 141 | * |
||
| 142 | * @ORM\Column(name="company_name", type="string", length=255, nullable=true) |
||
| 143 | */ |
||
| 144 | private $company_name; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var string|null |
||
| 148 | * |
||
| 149 | * @ORM\Column(name="zip01", type="string", length=3, nullable=true) |
||
| 150 | */ |
||
| 151 | private $zip01; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var string|null |
||
| 155 | * |
||
| 156 | * @ORM\Column(name="zip02", type="string", length=4, nullable=true) |
||
| 157 | */ |
||
| 158 | private $zip02; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var string|null |
||
| 162 | * |
||
| 163 | * @ORM\Column(name="zipcode", type="string", length=7, nullable=true) |
||
| 164 | */ |
||
| 165 | private $zipcode; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var string|null |
||
| 169 | * |
||
| 170 | * @ORM\Column(name="addr01", type="string", length=255, nullable=true) |
||
| 171 | */ |
||
| 172 | private $addr01; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var string|null |
||
| 176 | * |
||
| 177 | * @ORM\Column(name="addr02", type="string", length=255, nullable=true) |
||
| 178 | */ |
||
| 179 | private $addr02; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var string|null |
||
| 183 | * |
||
| 184 | * @ORM\Column(name="tel01", type="string", length=5, nullable=true) |
||
| 185 | */ |
||
| 186 | private $tel01; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var string|null |
||
| 190 | * |
||
| 191 | * @ORM\Column(name="tel02", type="string", length=4, nullable=true) |
||
| 192 | */ |
||
| 193 | private $tel02; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @var string|null |
||
| 197 | * |
||
| 198 | * @ORM\Column(name="tel03", type="string", length=4, nullable=true) |
||
| 199 | */ |
||
| 200 | private $tel03; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @var string|null |
||
| 204 | * |
||
| 205 | * @ORM\Column(name="fax01", type="string", length=5, nullable=true) |
||
| 206 | */ |
||
| 207 | private $fax01; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @var string|null |
||
| 211 | * |
||
| 212 | * @ORM\Column(name="fax02", type="string", length=4, nullable=true) |
||
| 213 | */ |
||
| 214 | private $fax02; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @var string|null |
||
| 218 | * |
||
| 219 | * @ORM\Column(name="fax03", type="string", length=4, nullable=true) |
||
| 220 | */ |
||
| 221 | private $fax03; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @var \DateTime |
||
| 225 | * |
||
| 226 | * @ORM\Column(name="create_date", type="datetimetz") |
||
| 227 | */ |
||
| 228 | private $create_date; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @var \DateTime |
||
| 232 | * |
||
| 233 | * @ORM\Column(name="update_date", type="datetimetz") |
||
| 234 | */ |
||
| 235 | private $update_date; |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @var \Eccube\Entity\Customer |
||
| 239 | * |
||
| 240 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\Customer", inversedBy="CustomerAddresses") |
||
| 241 | * @ORM\JoinColumns({ |
||
| 242 | * @ORM\JoinColumn(name="customer_id", referencedColumnName="id") |
||
| 243 | * }) |
||
| 244 | */ |
||
| 245 | private $Customer; |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @var \Eccube\Entity\Master\Country |
||
| 249 | * |
||
| 250 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\Master\Country") |
||
| 251 | * @ORM\JoinColumns({ |
||
| 252 | * @ORM\JoinColumn(name="country_id", referencedColumnName="id") |
||
| 253 | * }) |
||
| 254 | */ |
||
| 255 | private $Country; |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @var \Eccube\Entity\Master\Pref |
||
| 259 | * |
||
| 260 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\Master\Pref") |
||
| 261 | * @ORM\JoinColumns({ |
||
| 262 | * @ORM\JoinColumn(name="pref_id", referencedColumnName="id") |
||
| 263 | * }) |
||
| 264 | */ |
||
| 265 | private $Pref; |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get id. |
||
| 269 | * |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | 7 | public function getId() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Set name01. |
||
| 279 | * |
||
| 280 | * @param string|null $name01 |
||
| 281 | * |
||
| 282 | * @return CustomerAddress |
||
| 283 | */ |
||
| 284 | 45 | public function setName01($name01 = null) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Get name01. |
||
| 293 | * |
||
| 294 | * @return string|null |
||
| 295 | */ |
||
| 296 | 43 | public function getName01() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Set name02. |
||
| 303 | * |
||
| 304 | * @param string|null $name02 |
||
| 305 | * |
||
| 306 | * @return CustomerAddress |
||
| 307 | */ |
||
| 308 | 45 | public function setName02($name02 = null) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Get name02. |
||
| 317 | * |
||
| 318 | * @return string|null |
||
| 319 | */ |
||
| 320 | 42 | public function getName02() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Set kana01. |
||
| 327 | * |
||
| 328 | * @param string|null $kana01 |
||
| 329 | * |
||
| 330 | * @return CustomerAddress |
||
| 331 | */ |
||
| 332 | 44 | public function setKana01($kana01 = null) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Get kana01. |
||
| 341 | * |
||
| 342 | * @return string|null |
||
| 343 | */ |
||
| 344 | 42 | public function getKana01() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Set kana02. |
||
| 351 | * |
||
| 352 | * @param string|null $kana02 |
||
| 353 | * |
||
| 354 | * @return CustomerAddress |
||
| 355 | */ |
||
| 356 | 44 | public function setKana02($kana02 = null) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get kana02. |
||
| 365 | * |
||
| 366 | * @return string|null |
||
| 367 | */ |
||
| 368 | 42 | public function getKana02() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Set companyName. |
||
| 375 | * |
||
| 376 | * @param string|null $companyName |
||
| 377 | * |
||
| 378 | * @return CustomerAddress |
||
| 379 | */ |
||
| 380 | 34 | public function setCompanyName($companyName = null) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Get companyName. |
||
| 389 | * |
||
| 390 | * @return string|null |
||
| 391 | */ |
||
| 392 | 42 | public function getCompanyName() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Set zip01. |
||
| 399 | * |
||
| 400 | * @param string|null $zip01 |
||
| 401 | * |
||
| 402 | * @return CustomerAddress |
||
| 403 | */ |
||
| 404 | 45 | public function setZip01($zip01 = null) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Get zip01. |
||
| 413 | * |
||
| 414 | * @return string|null |
||
| 415 | */ |
||
| 416 | 42 | public function getZip01() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Set zip02. |
||
| 423 | * |
||
| 424 | * @param string|null $zip02 |
||
| 425 | * |
||
| 426 | * @return CustomerAddress |
||
| 427 | */ |
||
| 428 | 45 | public function setZip02($zip02 = null) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Get zip02. |
||
| 437 | * |
||
| 438 | * @return string|null |
||
| 439 | */ |
||
| 440 | 42 | public function getZip02() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Set zipcode. |
||
| 447 | * |
||
| 448 | * @param string|null $zipcode |
||
| 449 | * |
||
| 450 | * @return CustomerAddress |
||
| 451 | */ |
||
| 452 | 33 | public function setZipcode($zipcode = null) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Get zipcode. |
||
| 461 | * |
||
| 462 | * @return string|null |
||
| 463 | */ |
||
| 464 | public function getZipcode() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Set addr01. |
||
| 471 | * |
||
| 472 | * @param string|null $addr01 |
||
| 473 | * |
||
| 474 | * @return CustomerAddress |
||
| 475 | */ |
||
| 476 | 44 | public function setAddr01($addr01 = null) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Get addr01. |
||
| 485 | * |
||
| 486 | * @return string|null |
||
| 487 | */ |
||
| 488 | 43 | public function getAddr01() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Set addr02. |
||
| 495 | * |
||
| 496 | * @param string|null $addr02 |
||
| 497 | * |
||
| 498 | * @return CustomerAddress |
||
| 499 | */ |
||
| 500 | 44 | public function setAddr02($addr02 = null) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Get addr02. |
||
| 509 | * |
||
| 510 | * @return string|null |
||
| 511 | */ |
||
| 512 | 43 | public function getAddr02() |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Set tel01. |
||
| 519 | * |
||
| 520 | * @param string|null $tel01 |
||
| 521 | * |
||
| 522 | * @return CustomerAddress |
||
| 523 | */ |
||
| 524 | 43 | public function setTel01($tel01 = null) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Get tel01. |
||
| 533 | * |
||
| 534 | * @return string|null |
||
| 535 | */ |
||
| 536 | 42 | public function getTel01() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Set tel02. |
||
| 543 | * |
||
| 544 | * @param string|null $tel02 |
||
| 545 | * |
||
| 546 | * @return CustomerAddress |
||
| 547 | */ |
||
| 548 | 43 | public function setTel02($tel02 = null) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Get tel02. |
||
| 557 | * |
||
| 558 | * @return string|null |
||
| 559 | */ |
||
| 560 | 42 | public function getTel02() |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Set tel03. |
||
| 567 | * |
||
| 568 | * @param string|null $tel03 |
||
| 569 | * |
||
| 570 | * @return CustomerAddress |
||
| 571 | */ |
||
| 572 | 43 | public function setTel03($tel03 = null) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Get tel03. |
||
| 581 | * |
||
| 582 | * @return string|null |
||
| 583 | */ |
||
| 584 | 42 | public function getTel03() |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Set fax01. |
||
| 591 | * |
||
| 592 | * @param string|null $fax01 |
||
| 593 | * |
||
| 594 | * @return CustomerAddress |
||
| 595 | */ |
||
| 596 | 45 | public function setFax01($fax01 = null) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Get fax01. |
||
| 605 | * |
||
| 606 | * @return string|null |
||
| 607 | */ |
||
| 608 | 42 | public function getFax01() |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Set fax02. |
||
| 615 | * |
||
| 616 | * @param string|null $fax02 |
||
| 617 | * |
||
| 618 | * @return CustomerAddress |
||
| 619 | */ |
||
| 620 | 45 | public function setFax02($fax02 = null) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Get fax02. |
||
| 629 | * |
||
| 630 | * @return string|null |
||
| 631 | */ |
||
| 632 | 42 | public function getFax02() |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Set fax03. |
||
| 639 | * |
||
| 640 | * @param string|null $fax03 |
||
| 641 | * |
||
| 642 | * @return CustomerAddress |
||
| 643 | */ |
||
| 644 | 45 | public function setFax03($fax03 = null) |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Get fax03. |
||
| 653 | * |
||
| 654 | * @return string|null |
||
| 655 | */ |
||
| 656 | 42 | public function getFax03() |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Set createDate. |
||
| 663 | * |
||
| 664 | * @param \DateTime $createDate |
||
| 665 | * |
||
| 666 | * @return CustomerAddress |
||
| 667 | */ |
||
| 668 | 17 | public function setCreateDate($createDate) |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Get createDate. |
||
| 677 | * |
||
| 678 | * @return \DateTime |
||
| 679 | */ |
||
| 680 | public function getCreateDate() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Set updateDate. |
||
| 687 | * |
||
| 688 | * @param \DateTime $updateDate |
||
| 689 | * |
||
| 690 | * @return CustomerAddress |
||
| 691 | */ |
||
| 692 | 17 | public function setUpdateDate($updateDate) |
|
| 698 | |||
| 699 | /** |
||
| 700 | * Get updateDate. |
||
| 701 | * |
||
| 702 | * @return \DateTime |
||
| 703 | */ |
||
| 704 | public function getUpdateDate() |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Set customer. |
||
| 711 | * |
||
| 712 | * @param \Eccube\Entity\Customer|null $customer |
||
| 713 | * |
||
| 714 | * @return CustomerAddress |
||
| 715 | */ |
||
| 716 | 37 | public function setCustomer(\Eccube\Entity\Customer $customer = null) |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Get customer. |
||
| 725 | * |
||
| 726 | * @return \Eccube\Entity\Customer|null |
||
| 727 | */ |
||
| 728 | 2 | public function getCustomer() |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Set country. |
||
| 735 | * |
||
| 736 | * @param \Eccube\Entity\Master\Country|null $country |
||
| 737 | * |
||
| 738 | * @return CustomerAddress |
||
| 739 | */ |
||
| 740 | public function setCountry(\Eccube\Entity\Master\Country $country = null) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Get country. |
||
| 749 | * |
||
| 750 | * @return \Eccube\Entity\Master\Country|null |
||
| 751 | */ |
||
| 752 | public function getCountry() |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Set pref. |
||
| 759 | * |
||
| 760 | * @param \Eccube\Entity\Master\Pref|null $pref |
||
| 761 | * |
||
| 762 | * @return CustomerAddress |
||
| 763 | */ |
||
| 764 | 44 | public function setPref(\Eccube\Entity\Master\Pref $pref = null) |
|
| 770 | |||
| 771 | /** |
||
| 772 | * Get pref. |
||
| 773 | * |
||
| 774 | * @return \Eccube\Entity\Master\Pref|null |
||
| 775 | */ |
||
| 776 | 43 | public function getPref() |
|
| 780 | } |
||
| 781 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.