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 TShippingAddress |
|
|
|||
19 | { |
||
20 | /** @var array */ |
||
21 | protected $shipToLines; |
||
22 | /** @var string */ |
||
23 | protected $shipToCity; |
||
24 | /** @var string */ |
||
25 | protected $shipToMainDivision; |
||
26 | /** @var string */ |
||
27 | protected $shipToCountryCode; |
||
28 | /** @var string */ |
||
29 | protected $shipToPostalCode; |
||
30 | |||
31 | public function getShipToLines() |
||
35 | |||
36 | public function setShipToLines($lines) |
||
37 | { |
||
38 | $this->shipToLines = $this->cleanAddressLines($lines); |
||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Make sure we have max 4 address lines of 70 chars max |
||
44 | * |
||
45 | * If there are more than 4 lines concatenate all extra lines with the 4th line. |
||
46 | * |
||
47 | * Truncate any lines to 70 chars max. |
||
48 | * |
||
49 | * @param string $lines |
||
50 | * @return array or null |
||
51 | */ |
||
52 | protected function cleanAddressLines($lines) |
||
53 | { |
||
54 | $finalLines = null; |
||
55 | |||
56 | if (is_string($lines)) { |
||
57 | $trimmed = trim($lines); |
||
58 | $addressLines = preg_split("/\n/", $trimmed, null, PREG_SPLIT_NO_EMPTY); |
||
59 | |||
60 | $newLines = []; |
||
61 | foreach ($addressLines as $line) { |
||
62 | $newLines[] = $this->cleanString($line, 70); |
||
63 | } |
||
64 | |||
65 | if (count($newLines) > 4) { |
||
66 | // concat lines beyond the four allowed down into the last line |
||
67 | $newLines[3] = $this->cleanString(implode(' ', array_slice($newLines, 3)), 70); |
||
68 | } |
||
69 | |||
70 | $finalLines = array_slice($newLines, 0, 4); |
||
71 | } |
||
72 | |||
73 | return empty($finalLines) ? null : $finalLines; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Aggregate the shipTo address lines into the ShippingAddress node. This is an optional node. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | protected function serializeShippingAddress() |
||
97 | |||
98 | /** |
||
99 | * Build the Shipping Address Node |
||
100 | * @param array lines Street Address |
||
101 | * @return type |
||
102 | */ |
||
103 | protected function buildShippingAddressNode(array $lines) |
||
104 | { |
||
105 | return sprintf( |
||
106 | '<ShippingAddress>%s<City>%s</City>%s<CountryCode>%s</CountryCode>%s</ShippingAddress>', |
||
107 | implode('', $lines), |
||
108 | $this->xmlEncode($this->getShipToCity()), |
||
109 | $this->serializeOptionalXmlEncodedValue('MainDivision', $this->getShipToMainDivision()), |
||
110 | $this->xmlEncode($this->getShipToCountryCode()), |
||
111 | $this->serializeOptionalXmlEncodedValue('PostalCode', $this->getShipToPostalCode()) |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | public function getShipToCity() |
||
119 | |||
120 | public function setShipToCity($city) |
||
121 | { |
||
122 | $this->shipToCity = $this->cleanString($city, 35); |
||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | public function getShipToMainDivision() |
||
130 | |||
131 | public function setShipToMainDivision($div) |
||
132 | { |
||
133 | $this->shipToMainDivision = $this->cleanString($div, 35); |
||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | public function getShipToCountryCode() |
||
141 | |||
142 | public function setShipToCountryCode($code) |
||
143 | { |
||
144 | $cleaned = $this->cleanString($code, 40); |
||
145 | $this->shipToCountryCode = strlen($cleaned) >= 2 ? $cleaned : null; |
||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | public function getShipToPostalCode() |
||
153 | |||
154 | public function setShipToPostalCode($code) |
||
155 | { |
||
156 | $this->shipToPostalCode = $this->cleanString($code, 15); |
||
157 | return $this; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Trim any white space and return the resulting string truncating to $maxLength. |
||
162 | * |
||
163 | * Return null if the result is an empty string or not a string |
||
164 | * |
||
165 | * @param string $string |
||
166 | * @param int $maxLength |
||
167 | * @return string or null |
||
168 | */ |
||
169 | abstract protected function cleanString($string, $maxLength); |
||
170 | |||
171 | /** |
||
172 | * Serialize an optional element containing a string. The value will be |
||
173 | * xml-encoded if is not null. |
||
174 | * |
||
175 | * @param string |
||
176 | * @param string |
||
177 | * @return string |
||
178 | */ |
||
179 | abstract protected function serializeOptionalXmlEncodedValue($name, $value); |
||
180 | |||
181 | /** |
||
182 | * encode the passed in string to be safe for xml if it is not null, |
||
183 | * otherwise simply return the null parameter. |
||
184 | * |
||
185 | * @param string|null |
||
186 | * @return string|null |
||
187 | */ |
||
188 | abstract protected function xmlEncode($value = null); |
||
189 | } |
||
190 |
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.