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 |
||
18 | View Code Duplication | trait TPhysicalAddress |
|
|
|||
19 | { |
||
20 | /** @var array */ |
||
21 | protected $lines; |
||
22 | /** @var string */ |
||
23 | protected $city; |
||
24 | /** @var string */ |
||
25 | protected $mainDivision; |
||
26 | /** @var string */ |
||
27 | protected $countryCode; |
||
28 | /** @var string */ |
||
29 | protected $postalCode; |
||
30 | |||
31 | public function getLines() |
||
35 | |||
36 | public function setLines($lines) |
||
37 | { |
||
38 | $this->lines = $this->cleanAddressLines($lines); |
||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | public function getCity() |
||
46 | |||
47 | public function setCity($city) |
||
48 | { |
||
49 | $this->city = $this->cleanString($city, 35); |
||
50 | return $this; |
||
51 | } |
||
52 | |||
53 | public function getMainDivision() |
||
57 | |||
58 | public function setMainDivision($div) |
||
59 | { |
||
60 | $this->mainDivision = $this->cleanString($div, 35); |
||
61 | return $this; |
||
62 | } |
||
63 | |||
64 | public function getCountryCode() |
||
68 | |||
69 | public function setCountryCode($code) |
||
70 | { |
||
71 | $cleaned = $this->cleanString($code, 40); |
||
72 | $this->countryCode = strlen($cleaned) >= 2 ? $cleaned : null; |
||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | public function getPostalCode() |
||
80 | |||
81 | public function setPostalCode($code) |
||
82 | { |
||
83 | $this->postalCode = $this->cleanString($code, 15); |
||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Aggregate the address lines into the ShippingAddress node. This is an optional node. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | protected function serializePhysicalAddress() |
||
108 | |||
109 | /** |
||
110 | * Build the Shipping Address Node |
||
111 | * |
||
112 | * @param array lines Street Address |
||
113 | * @return string |
||
114 | */ |
||
115 | protected function buildPhysicalAddressNodes(array $lines) |
||
116 | { |
||
117 | return sprintf( |
||
118 | '<%s>%s<City>%s</City>%s<CountryCode>%s</CountryCode>%s</%1$s>', |
||
119 | $this->getPhysicalAddressRootNodeName(), |
||
120 | implode('', $lines), |
||
121 | $this->xmlEncode($this->getCity()), |
||
122 | $this->serializeOptionalXmlEncodedValue('MainDivision', $this->getMainDivision()), |
||
123 | $this->xmlEncode($this->getCountryCode()), |
||
124 | $this->serializeOptionalXmlEncodedValue('PostalCode', $this->getPostalCode()) |
||
125 | ); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Make sure we have max 4 address lines of 70 chars max |
||
130 | * |
||
131 | * If there are more than 4 lines concatenate all extra lines with the 4th line. |
||
132 | * |
||
133 | * Truncate any lines to 70 chars max. |
||
134 | * |
||
135 | * @param string $lines |
||
136 | * @return array or null |
||
137 | */ |
||
138 | protected function cleanAddressLines($lines) |
||
139 | { |
||
140 | $finalLines = null; |
||
141 | |||
142 | if (is_string($lines)) { |
||
143 | $trimmed = trim($lines); |
||
144 | $addressLines = preg_split("/\n/", $trimmed, null, PREG_SPLIT_NO_EMPTY); |
||
145 | |||
146 | $newLines = []; |
||
147 | foreach ($addressLines as $line) { |
||
148 | $newLines[] = $this->cleanString($line, 70); |
||
149 | } |
||
150 | |||
151 | if (count($newLines) > 4) { |
||
152 | // concat lines beyond the four allowed down into the last line |
||
153 | $newLines[3] = $this->cleanString(implode(' ', array_slice($newLines, 3)), 70); |
||
154 | } |
||
155 | |||
156 | $finalLines = array_slice($newLines, 0, 4); |
||
157 | } |
||
158 | |||
159 | return empty($finalLines) ? null : $finalLines; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Node name wrapping the physical address elements. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | abstract protected function getPhysicalAddressRootNodeName(); |
||
168 | |||
169 | /** |
||
170 | * encode the passed in string to be safe for xml if it is not null, |
||
171 | * otherwise simply return the null parameter. |
||
172 | * |
||
173 | * @param string|null |
||
174 | * @return string|null |
||
175 | */ |
||
176 | abstract protected function xmlEncode($value = null); |
||
177 | |||
178 | /** |
||
179 | * encode the passed in string to be safe for xml if it is not null, |
||
180 | * otherwise simply return the null parameter. |
||
181 | * @param mixed $value |
||
182 | * @return string | null |
||
183 | */ |
||
184 | abstract protected function serializeOptionalXmlEncodedValue($name, $value); |
||
185 | } |
||
186 |
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.