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:
1 | <?php namespace Ups\Entity\AddressValidation; |
||
3 | class AVAddress |
||
4 | { |
||
5 | /** |
||
6 | * @var null|AddressClassification |
||
7 | */ |
||
8 | public $addressClassification; |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | public $consigneeName; |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | public $buildingName; |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | public $addressLine; |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | public $addressLine2; |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | public $addressLine3; |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | public $region; |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | public $politicalDivision2; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | public $politicalDivision1; |
||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | public $postcodePrimaryLow; |
||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | public $postcodeExtendedLow; |
||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | public $urbanization; |
||
53 | /** |
||
54 | * @var |
||
55 | */ |
||
56 | public $countryCode; |
||
57 | |||
58 | /** |
||
59 | * Address constructor. |
||
60 | * @param \SimpleXMLElement $xmlDoc |
||
61 | */ |
||
62 | 2 | public function __construct(\SimpleXMLElement $xmlDoc) |
|
63 | { |
||
64 | 2 | if ($xmlDoc->count() == 0) { |
|
65 | throw new \InvalidArgumentException(__METHOD__ . ': The passed object does not have any child nodes.'); |
||
66 | } |
||
67 | 2 | $this->addressClassification = isset($xmlDoc->AddressClassification) ? new AddressClassification($xmlDoc->AddressClassification) : null; |
|
68 | 2 | $this->consigneeName = isset($xmlDoc->ConsigneeName) ? (string)$xmlDoc->ConsigneeName : ''; |
|
69 | 2 | $this->buildingName = isset($xmlDoc->BuildingName) ? (string)$xmlDoc->BuildingName : ''; |
|
70 | 2 | if (isset($xmlDoc->AddressLine)) { |
|
71 | 2 | for ($i = 0, $len = count($xmlDoc->AddressLine); $i < $len; $i++) { |
|
72 | 2 | if ($i === 0) { |
|
73 | 2 | $this->addressLine = isset($xmlDoc->AddressLine[$i]) ? (string) $xmlDoc->AddressLine[$i] : ''; |
|
74 | 2 | } |
|
75 | View Code Duplication | elseif ($i === 1) { |
|
|
|||
76 | $this->addressLine2 = isset($xmlDoc->AddressLine[$i]) ? (string) $xmlDoc->AddressLine[$i] : ''; |
||
77 | } |
||
78 | View Code Duplication | elseif ($i === 2) { |
|
79 | $this->addressLine3 = isset($xmlDoc->AddressLine[$i]) ? (string) $xmlDoc->AddressLine[$i] : ''; |
||
80 | } |
||
81 | 2 | } |
|
82 | 2 | } |
|
83 | 2 | $this->region = isset($xmlDoc->Region) ? (string)$xmlDoc->Region : ''; |
|
84 | 2 | $this->politicalDivision2 = isset($xmlDoc->PoliticalDivision2) ? (string)$xmlDoc->PoliticalDivision2 : ''; |
|
85 | 2 | $this->politicalDivision1 = isset($xmlDoc->PoliticalDivision1) ? (string)$xmlDoc->PoliticalDivision1 : ''; |
|
86 | 2 | $this->postcodePrimaryLow = isset($xmlDoc->PostcodePrimaryLow) ? (string)$xmlDoc->PostcodePrimaryLow : ''; |
|
87 | 2 | $this->postcodeExtendedLow = isset($xmlDoc->PostcodeExtendedLow) ? (string)$xmlDoc->PostcodeExtendedLow : ''; |
|
88 | 2 | $this->urbanization = isset($xmlDoc->Urbanization) ? (string)$xmlDoc->Urbanization : ''; |
|
89 | 2 | $this->countryCode = isset($xmlDoc->CountryCode) ? (string)$xmlDoc->CountryCode : ''; |
|
90 | 2 | } |
|
91 | |||
92 | /** |
||
93 | * Convenience methods. Even though all properties are public, these methods provide a convenient interface to |
||
94 | * retrieve commonly requested parts so that the user doesn't have to remember which API fields reference |
||
95 | * which piece of information. For example, I won't have to remember that the city is in 'PoliticalDivision2'. |
||
96 | */ |
||
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | 1 | public function getCity() |
|
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | 1 | public function getStateProvince() |
|
113 | |||
114 | /** |
||
115 | * Return the address postal code |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | 1 | public function getPostalCode() |
|
123 | |||
124 | /** |
||
125 | * Return the address postal code with extension (i.e. the U.S. extended zip+4 postal code) |
||
126 | * |
||
127 | * @param string $divider |
||
128 | * @return string |
||
129 | */ |
||
130 | 1 | public function getPostalCodeWithExtension($divider = '-') |
|
134 | } |
||
135 |
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.