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 |
||
30 | class CreditCardAuthReply implements ICreditCardAuthReply |
||
31 | { |
||
32 | use TTopLevelPayload, TPaymentContext; |
||
33 | |||
34 | /** @var string */ |
||
35 | protected $authorizationResponseCode; |
||
36 | /** @var string */ |
||
37 | protected $bankAuthorizationCode; |
||
38 | /** @var string */ |
||
39 | protected $cvv2ResponseCode; |
||
40 | /** @var string */ |
||
41 | protected $avsResponseCode; |
||
42 | /** @var string */ |
||
43 | protected $phoneResponseCode; |
||
44 | /** @var string */ |
||
45 | protected $nameResponseCode; |
||
46 | /** @var string */ |
||
47 | protected $emailResponseCode; |
||
48 | /** @var float */ |
||
49 | protected $amountAuthorized; |
||
50 | /** @var string */ |
||
51 | protected $currencyCode; |
||
52 | /** @var array Mapping of reply authorization response code to OMS response code */ |
||
53 | protected $responseCodeMap = [ |
||
54 | self::AUTHORIZATION_APPROVED => self::APPROVED_RESPONSE_CODE, |
||
55 | self::AUTHORIZATION_TIMEOUT_PAYMENT_PROVIDER => self::TIMEOUT_RESPONSE_CODE, |
||
56 | self::AUTHORIZATION_TIMEOUT_CARD_PROCESSOR => self::TIMEOUT_RESPONSE_CODE, |
||
57 | ]; |
||
58 | /** @var string[] AVS response codes that should be rejected */ |
||
59 | protected $invalidAvsCodes = ['N', 'AW']; |
||
60 | /** @var string[] CVV response codes that should be rejected */ |
||
61 | protected $invalidCvvCodes = ['N']; |
||
62 | |||
63 | /** |
||
64 | * @param IValidatorIterator |
||
65 | * @param ISchemaValidator |
||
66 | * @param IPayloadMap |
||
67 | * @param LoggerInterface |
||
68 | * @param IPayload |
||
69 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
70 | */ |
||
71 | View Code Duplication | public function __construct( |
|
|
|||
72 | IValidatorIterator $validators, |
||
73 | ISchemaValidator $schemaValidator, |
||
74 | IPayloadMap $payloadMap, |
||
75 | LoggerInterface $logger, |
||
76 | IPayload $parentPayload = null |
||
77 | ) { |
||
78 | $this->logger = $logger; |
||
79 | $this->validators = $validators; |
||
80 | $this->schemaValidator = $schemaValidator; |
||
81 | $this->parentPayload = $parentPayload; |
||
82 | |||
83 | $this->extractionPaths = [ |
||
84 | 'orderId' => 'string(x:PaymentContext/x:OrderId)', |
||
85 | 'cardNumber' => |
||
86 | 'string(x:PaymentContext/x:EncryptedPaymentAccountUniqueId|x:PaymentContext/x:PaymentAccountUniqueId)', |
||
87 | 'authorizationResponseCode' => 'string(x:AuthorizationResponseCode)', |
||
88 | 'bankAuthorizationCode' => 'string(x:BankAuthorizationCode)', |
||
89 | 'cvv2ResponseCode' => 'string(x:CVV2ResponseCode)', |
||
90 | 'avsResponseCode' => 'string(x:AVSResponseCode)', |
||
91 | 'amountAuthorized' => 'number(x:AmountAuthorized)', |
||
92 | 'currencyCode' => 'string(x:AmountAuthorized/@currencyCode)', |
||
93 | 'isEncrypted' => 'boolean(x:PaymentContext/x:EncryptedPaymentAccountUniqueId)', |
||
94 | ]; |
||
95 | $this->booleanExtractionPaths = [ |
||
96 | 'panIsToken' => 'string(x:PaymentContext/x:PaymentAccountUniqueId/@isToken)' |
||
97 | ]; |
||
98 | $this->optionalExtractionPaths = [ |
||
99 | 'phoneResponseCode' => 'x:PhoneResponseCode', |
||
100 | 'nameResponseCode' => 'x:NameResponseCode', |
||
101 | 'emailResponseCode' => 'x:EmailResponseCode', |
||
102 | ]; |
||
103 | } |
||
104 | |||
105 | public function getIsAVSCorrectionRequired() |
||
109 | |||
110 | public function getIsAuthApproved() |
||
114 | |||
115 | public function getAuthorizationResponseCode() |
||
119 | |||
120 | public function getIsAVSSuccessful() |
||
124 | |||
125 | public function getAVSResponseCode() |
||
129 | |||
130 | public function getIsCVV2CorrectionRequired() |
||
134 | |||
135 | public function getIsCVV2Successful($value = '') |
||
139 | |||
140 | public function getCVV2ResponseCode() |
||
144 | |||
145 | public function getIsAuthSuccessful() |
||
150 | |||
151 | public function getIsAuthTimeout() |
||
152 | { |
||
153 | $responseCode = $this->getAuthorizationResponseCode(); |
||
154 | return $responseCode === self::AUTHORIZATION_TIMEOUT_PAYMENT_PROVIDER |
||
155 | || $responseCode === self::AUTHORIZATION_TIMEOUT_CARD_PROCESSOR; |
||
156 | } |
||
157 | |||
158 | public function getIsAuthAcceptable() |
||
159 | { |
||
160 | // If there is a response code acceptable by the OMS self::getResponseCode |
||
161 | // doesn't return null, then the reply is acceptable |
||
162 | return !is_null($this->getResponseCode()); |
||
163 | } |
||
164 | |||
165 | public function getResponseCode() |
||
166 | { |
||
167 | $responseCode = $this->getAuthorizationResponseCode(); |
||
168 | return isset($this->responseCodeMap[$responseCode]) ? $this->responseCodeMap[$responseCode] : null; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Serialize the various parts of the payload into XML strings and |
||
173 | * simply concatenate them together. |
||
174 | * @return string |
||
175 | */ |
||
176 | protected function serializeContents() |
||
177 | { |
||
178 | return $this->serializePaymentContext() |
||
179 | . $this->serializeResponseCodes() |
||
180 | . $this->serializeAdditionalResponseCodes() |
||
181 | . $this->serializeAmount(); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Create an XML string representing the various response codes, e.g. |
||
186 | * AuthorizationResponseCode, BankAuthorizationCode, CVV2ResponseCode, etc. |
||
187 | * @return string |
||
188 | */ |
||
189 | protected function serializeResponseCodes() |
||
190 | { |
||
191 | $template = '<AuthorizationResponseCode>%s</AuthorizationResponseCode>' |
||
192 | . '<BankAuthorizationCode>%s</BankAuthorizationCode>' |
||
193 | . '<CVV2ResponseCode>%s</CVV2ResponseCode>' |
||
194 | . '<AVSResponseCode>%s</AVSResponseCode>'; |
||
195 | return sprintf( |
||
196 | $template, |
||
197 | $this->xmlEncode($this->getAuthorizationResponseCode()), |
||
198 | $this->xmlEncode($this->getBankAuthorizationCode()), |
||
199 | $this->xmlEncode($this->getCVV2ResponseCode()), |
||
200 | $this->xmlEncode($this->getAVSResponseCode()) |
||
201 | ); |
||
202 | } |
||
203 | |||
204 | public function getBankAuthorizationCode() |
||
208 | |||
209 | /** |
||
210 | * Create an XML string representing any of the optional response codes, |
||
211 | * e.g. EmailResponseCode, PhoneResponseCode, etc. |
||
212 | * @return string |
||
213 | */ |
||
214 | protected function serializeAdditionalResponseCodes() |
||
223 | |||
224 | public function getPhoneResponseCode() |
||
228 | |||
229 | public function getNameResponseCode() |
||
233 | |||
234 | public function getEmailResponseCode() |
||
238 | |||
239 | /** |
||
240 | * Create an XML string representing the amount authorized. |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function serializeAmount() |
||
244 | { |
||
245 | return sprintf( |
||
246 | '<AmountAuthorized currencyCode="%s">%01.2F</AmountAuthorized>', |
||
247 | $this->xmlEncode($this->getCurrencyCode()), |
||
248 | $this->getAmountAuthorized() |
||
249 | ); |
||
250 | } |
||
251 | |||
252 | public function getCurrencyCode() |
||
256 | |||
257 | public function getAmountAuthorized() |
||
261 | |||
262 | protected function getSchemaFile() |
||
266 | |||
267 | /** |
||
268 | * Return the name of the xml root node. |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | protected function getRootNodeName() |
||
276 | |||
277 | /** |
||
278 | * The XML namespace for the payload. |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | protected function getXmlNamespace() |
||
286 | } |
||
287 |
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.