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 |
||
16 | class Locator extends Ups |
||
17 | { |
||
18 | private $request; |
||
19 | |||
20 | const ENDPOINT = '/Locator'; |
||
21 | |||
22 | const OPTION_DROP_LOCATIONS_AND_WILL_CALL_LOCATIONS = 1; |
||
23 | const OPTION_ALL_AVAILABLE_ADDITIONAL_SERVICES = 8; |
||
24 | const OPTION_ALL_AVAILABLE_PROGRAM_TYPES = 16; |
||
25 | const OPTION_ALL_AVAILABLE_ADDITIONAL_SERVICES_AND_PROGRAM_TYPES = 24; |
||
26 | const OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS = 32; |
||
27 | const OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_ADDITIONAL_SERVICES = 40; |
||
28 | const OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_PROGRAM_TYPES = 48; |
||
29 | const OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_ADDITIONAL_SERVICES_AND_PROGRAM_TYPES = 56; |
||
30 | const OPTION_UPS_ACCESS_POINT_LOCATIONS = 64; |
||
31 | |||
32 | /** |
||
33 | * @param string|null $accessKey UPS License Access Key |
||
34 | * @param string|null $userId UPS User ID |
||
35 | * @param string|null $password UPS User Password |
||
36 | * @param bool $useIntegration Determine if we should use production or CIE URLs. |
||
37 | * @param RequestInterface $request |
||
38 | * @param LoggerInterface $logger PSR3 compatible logger (optional) |
||
39 | */ |
||
40 | 3 | View Code Duplication | public function __construct($accessKey = null, $userId = null, $password = null, $useIntegration = false, RequestInterface $request = null, LoggerInterface $logger = null) |
47 | |||
48 | 3 | public function getLocations(LocatorRequest $request, $requestOption = self::OPTION_UPS_ACCESS_POINT_LOCATIONS) |
|
52 | |||
53 | /** |
||
54 | * Creates and sends a request for the given shipment. This handles checking for |
||
55 | * errors in the response back from UPS. |
||
56 | * |
||
57 | * @param LocatorRequest $request |
||
58 | * @param int $requestOption |
||
59 | * |
||
60 | * @throws Exception |
||
61 | * |
||
62 | * @return \stdClass |
||
63 | */ |
||
64 | 3 | View Code Duplication | private function sendRequest(LocatorRequest $request, $requestOption) |
83 | |||
84 | /** |
||
85 | * Create the TimeInTransit request. |
||
86 | * |
||
87 | * @param LocatorRequest $locatorRequest The request details. Refer to the UPS documentation for available structure |
||
88 | * @param $requestOption |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 3 | private function createRequest(LocatorRequest $locatorRequest, $requestOption) |
|
93 | { |
||
94 | 3 | $xml = new DOMDocument(); |
|
95 | 3 | $xml->formatOutput = true; |
|
96 | |||
97 | 3 | $trackRequest = $xml->appendChild($xml->createElement('LocatorRequest')); |
|
98 | 3 | $trackRequest->setAttribute('xml:lang', 'en-US'); |
|
99 | |||
100 | 3 | $request = $trackRequest->appendChild($xml->createElement('Request')); |
|
101 | |||
102 | 3 | $node = $xml->importNode($this->createTransactionNode(), true); |
|
103 | 3 | $request->appendChild($node); |
|
104 | |||
105 | 3 | $request->appendChild($xml->createElement('RequestAction', 'Locator')); |
|
106 | 3 | $request->appendChild($xml->createElement('RequestOption', $requestOption)); |
|
107 | |||
108 | // Origin Address |
||
109 | 3 | $trackRequest->appendChild($locatorRequest->getOriginAddress()->toNode($xml)); |
|
110 | |||
111 | // Translate |
||
112 | 3 | $trackRequest->appendChild($locatorRequest->getTranslate()->toNode($xml)); |
|
113 | |||
114 | // Unit of measurement |
||
115 | 3 | if ($locatorRequest->getUnitOfMeasurement()) { |
|
116 | 3 | $trackRequest->appendChild($locatorRequest->getUnitOfMeasurement()->toNode($xml)); |
|
117 | 3 | } |
|
118 | |||
119 | // LocationSearchCriteria |
||
120 | 3 | if ($locatorRequest->getLocationSearchCriteria()) { |
|
121 | 2 | $trackRequest->appendChild($locatorRequest->getLocationSearchCriteria()->toNode($xml)); |
|
122 | 2 | } |
|
123 | |||
124 | 3 | return $xml->saveXML(); |
|
125 | } |
||
126 | |||
127 | private function formatResponse(SimpleXMLElement $response) |
||
133 | |||
134 | /** |
||
135 | * @return RequestInterface |
||
136 | */ |
||
137 | 3 | View Code Duplication | public function getRequest() |
145 | |||
146 | /** |
||
147 | * @param RequestInterface $request |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | 3 | public function setRequest(RequestInterface $request) |
|
157 | |||
158 | /** |
||
159 | * @return ResponseInterface |
||
160 | */ |
||
161 | public function getResponse() |
||
165 | |||
166 | /** |
||
167 | * @param ResponseInterface $response |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function setResponse(ResponseInterface $response) |
||
177 | } |
||
178 |
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.