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 Tradeability extends Ups |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * |
||
21 | */ |
||
22 | const ENDPOINT_LANDEDCOST = '/LandedCost'; |
||
23 | /** |
||
24 | * @var string |
||
25 | * |
||
26 | * @deprecated |
||
27 | */ |
||
28 | protected $productionBaseUrl = 'https://www.ups.com/webservices'; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | * |
||
33 | * @deprecated |
||
34 | */ |
||
35 | protected $integrationBaseUrl = 'https://wwwcie.ups.com/webservices'; |
||
36 | /** |
||
37 | * @var |
||
38 | */ |
||
39 | private $request; |
||
40 | |||
41 | /** |
||
42 | * @param string|null $accessKey UPS License Access Key |
||
43 | * @param string|null $userId UPS User ID |
||
44 | * @param string|null $password UPS User Password |
||
45 | * @param bool $useIntegration Determine if we should use production or CIE URLs. |
||
46 | * @param RequestInterface|null $request |
||
47 | * @param LoggerInterface|null $logger PSR3 compatible logger (optional) |
||
48 | */ |
||
49 | View Code Duplication | public function __construct( |
|
|
|||
50 | $accessKey = null, |
||
51 | $userId = null, |
||
52 | $password = null, |
||
53 | $useIntegration = false, |
||
54 | RequestInterface $request = null, |
||
55 | LoggerInterface $logger = null |
||
56 | ) { |
||
57 | if (null !== $request) { |
||
58 | $this->setRequest($request); |
||
59 | } |
||
60 | parent::__construct($accessKey, $userId, $password, $useIntegration, $logger); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param LandedCostRequest $request |
||
65 | * @return SimpleXmlElement |
||
66 | * @throws Exception |
||
67 | */ |
||
68 | public function getLandedCosts(LandedCostRequest $request) |
||
69 | { |
||
70 | $request = $this->createRequestLandedCost($request); |
||
71 | $response = $this->sendRequest( |
||
72 | $request, self::ENDPOINT_LANDEDCOST, |
||
73 | 'ProcessLCRequest', 'LandedCost' |
||
74 | ); |
||
75 | |||
76 | if (isset($response->LandedCostResponse->QueryResponse)) { |
||
77 | return $response->LandedCostResponse->QueryResponse; |
||
78 | } |
||
79 | |||
80 | return $response->LandedCostResponse->EstimateResponse; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Create the LandedCostRequest request. |
||
85 | * |
||
86 | * @param LandedCostRequest $landedCostRequest The request details. Refer to the UPS documentation for available structure |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | private function createRequestLandedCost(LandedCostRequest $landedCostRequest) |
||
91 | { |
||
92 | $xml = new DOMDocument(); |
||
93 | $xml->formatOutput = true; |
||
94 | |||
95 | $tradeabilityRequest = $xml->appendChild($xml->createElement('LandedCostRequest')); |
||
96 | $tradeabilityRequest->setAttribute('xml:lang', 'en-US'); |
||
97 | |||
98 | $request = $tradeabilityRequest->appendChild($xml->createElement('Request')); |
||
99 | |||
100 | $node = $xml->importNode($this->createTransactionNode(), true); |
||
101 | $request->appendChild($node); |
||
102 | |||
103 | $request->appendChild($xml->createElement('RequestAction', 'LandedCost')); |
||
104 | |||
105 | if ($landedCostRequest->getQueryRequest() !== null) { |
||
106 | $tradeabilityRequest->appendChild($landedCostRequest->getQueryRequest()->toNode($xml)); |
||
107 | } |
||
108 | |||
109 | return $xml->saveXML(); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Creates and sends a request for the given data. Most errors are handled in SoapRequest |
||
114 | * |
||
115 | * @param string $request |
||
116 | * @param string $endpoint |
||
117 | * @param string $operation |
||
118 | * @param string $wsdl |
||
119 | * |
||
120 | * @throws Exception |
||
121 | * |
||
122 | * @return \stdClass |
||
123 | */ |
||
124 | private function sendRequest($request, $endpoint, $operation, $wsdl) |
||
125 | { |
||
126 | $endpointurl = $this->compileEndpointUrl($endpoint); |
||
127 | $this->response = $this->getRequest()->request( |
||
128 | $this->createAccess(), $request, $endpointurl, $operation, $wsdl |
||
129 | ); |
||
130 | $response = $this->response->getResponse(); |
||
131 | |||
132 | if (null === $response) { |
||
133 | throw new Exception('Failure (0): Unknown error', 0); |
||
134 | } |
||
135 | |||
136 | return $this->formatResponse($response); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return RequestInterface |
||
141 | */ |
||
142 | public function getRequest() |
||
143 | { |
||
144 | if (null === $this->request) { |
||
145 | $this->request = new SoapRequest($this->logger); |
||
146 | } |
||
147 | |||
148 | return $this->request; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param RequestInterface $request |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setRequest(RequestInterface $request) |
||
157 | { |
||
158 | $this->request = $request; |
||
159 | |||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Format the response. |
||
165 | * |
||
166 | * @param SimpleXMLElement $response |
||
167 | * |
||
168 | * @return \stdClass |
||
169 | */ |
||
170 | private function formatResponse(SimpleXMLElement $response) |
||
174 | } |
||
175 |
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.