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 | class SimpleAddressValidation extends Ups |
||
19 | { |
||
20 | const ENDPOINT = '/AV'; |
||
21 | |||
22 | /** |
||
23 | * @var RequestInterface |
||
24 | */ |
||
25 | private $request; |
||
26 | |||
27 | /** |
||
28 | * @var ResponseInterface |
||
29 | * |
||
30 | * @todo make private |
||
31 | */ |
||
32 | public $response; |
||
33 | |||
34 | /** |
||
35 | * @var Address |
||
36 | */ |
||
37 | private $address; |
||
38 | |||
39 | /** |
||
40 | * @param string|null $accessKey UPS License Access Key |
||
41 | * @param string|null $userId UPS User ID |
||
42 | * @param string|null $password UPS User Password |
||
43 | * @param bool $useIntegration Determine if we should use production or CIE URLs. |
||
44 | * @param RequestInterface|null $request |
||
45 | * @param LoggerInterface|null $logger PSR3 compatible logger (optional) |
||
46 | */ |
||
47 | 2 | View Code Duplication | public function __construct( |
60 | |||
61 | /** |
||
62 | * Get address suggestions from UPS using the default Address Validation API (/AV) |
||
63 | * |
||
64 | * @param Address $address |
||
65 | * |
||
66 | * @throws Exception |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | 2 | public function validate(Address $address) |
|
93 | |||
94 | /** |
||
95 | * Create the AV request. |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | 2 | private function createRequest() |
|
133 | |||
134 | /** |
||
135 | * Format the response. |
||
136 | * |
||
137 | * @param SimpleXMLElement $response |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | 1 | private function formatResponse(SimpleXMLElement $response) |
|
151 | |||
152 | /** |
||
153 | * @return RequestInterface |
||
154 | */ |
||
155 | 2 | public function getRequest() |
|
163 | |||
164 | /** |
||
165 | * @param RequestInterface $request |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | 2 | public function setRequest(RequestInterface $request) |
|
175 | |||
176 | /** |
||
177 | * @return ResponseInterface |
||
178 | */ |
||
179 | public function getResponse() |
||
183 | |||
184 | /** |
||
185 | * @param ResponseInterface $response |
||
186 | * |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function setResponse(ResponseInterface $response) |
||
195 | } |
||
196 |
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.