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 AddressValidation extends Ups |
||
19 | { |
||
20 | const ENDPOINT = '/XAV'; |
||
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 int |
||
36 | */ |
||
37 | private $requestOption; |
||
38 | |||
39 | /** |
||
40 | * @var Address |
||
41 | */ |
||
42 | private $address; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | private $maxSuggestion; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $useAVResponseObject = false; |
||
53 | |||
54 | /** |
||
55 | * Request Options. |
||
56 | */ |
||
57 | const REQUEST_OPTION_ADDRESS_VALIDATION = 1; |
||
58 | const REQUEST_OPTION_ADDRESS_CLASSIFICATION = 2; |
||
59 | const REQUEST_OPTION_ADDRESS_VALIDATION_AND_CLASSIFICATION = 3; |
||
60 | |||
61 | /** |
||
62 | * @param string|null $accessKey UPS License Access Key |
||
63 | * @param string|null $userId UPS User ID |
||
64 | * @param string|null $password UPS User Password |
||
65 | * @param bool $useIntegration Determine if we should use production or CIE URLs. |
||
66 | * @param RequestInterface|null $request |
||
67 | * @param LoggerInterface|null $logger PSR3 compatible logger (optional) |
||
68 | */ |
||
69 | 15 | View Code Duplication | public function __construct( |
82 | |||
83 | /** |
||
84 | * Turn on returning of the AddressValidationResponse object |
||
85 | */ |
||
86 | 14 | public function activateReturnObjectOnValidate() |
|
90 | |||
91 | /** |
||
92 | * Turn off returning of the AddressValidationResponse object |
||
93 | */ |
||
94 | public function deActivateReturnObjectOnValidate() |
||
98 | |||
99 | /** |
||
100 | * Get address suggestions from UPS using the 'Street Level' Address Validation API (/XAV) |
||
101 | * |
||
102 | * @param Address $address |
||
103 | * @param int $requestOption |
||
104 | * @param int $maxSuggestion |
||
105 | * |
||
106 | * @throws Exception |
||
107 | * |
||
108 | * @return stdClass|AddressValidationResponse |
||
109 | */ |
||
110 | 15 | public function validate(Address $address, $requestOption = self::REQUEST_OPTION_ADDRESS_VALIDATION, $maxSuggestion = 15) |
|
147 | |||
148 | /** |
||
149 | * Create the XAV request. |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | 15 | private function createRequest() |
|
211 | |||
212 | /** |
||
213 | * Format the response. |
||
214 | * |
||
215 | * @param SimpleXMLElement $response |
||
216 | * |
||
217 | * @return stdClass |
||
218 | */ |
||
219 | private function formatResponse(SimpleXMLElement $response) |
||
223 | |||
224 | /** |
||
225 | * @return RequestInterface |
||
226 | */ |
||
227 | 15 | public function getRequest() |
|
235 | |||
236 | /** |
||
237 | * @param RequestInterface $request |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | 15 | public function setRequest(RequestInterface $request) |
|
247 | |||
248 | /** |
||
249 | * @return ResponseInterface |
||
250 | */ |
||
251 | public function getResponse() |
||
255 | |||
256 | /** |
||
257 | * @param ResponseInterface $response |
||
258 | * |
||
259 | * @return $this |
||
260 | */ |
||
261 | public function setResponse(ResponseInterface $response) |
||
267 | } |
||
268 |
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.