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 |
||
15 | class Response extends AbstractResponse |
||
16 | { |
||
17 | 75 | public function __construct(RequestInterface $request, $data) |
|
22 | |||
23 | /** |
||
24 | * Is the transaction a redirect? |
||
25 | * |
||
26 | * @return bool |
||
27 | */ |
||
28 | 15 | public function isRedirect() |
|
32 | |||
33 | /** |
||
34 | * Get the redirect url from the response. |
||
35 | * |
||
36 | * Returns null if the request was not a redirect. |
||
37 | * |
||
38 | * @return string|null |
||
39 | */ |
||
40 | 9 | public function getRedirectUrl() |
|
41 | { |
||
42 | 9 | if (isset($this->data['redirect_url'])) { |
|
43 | 6 | return $this->data['redirect_url']; |
|
44 | } |
||
45 | |||
46 | 3 | return null; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Is the transaction successful? |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 30 | public function isSuccessful() |
|
59 | |||
60 | /** |
||
61 | * Get the transaction reference. |
||
62 | * |
||
63 | * @return string|null |
||
64 | */ |
||
65 | 12 | View Code Duplication | public function getTransactionReference() |
73 | /** |
||
74 | * Get the transaction id. |
||
75 | * |
||
76 | * @return string|null |
||
77 | */ |
||
78 | 12 | View Code Duplication | public function getTransactionId() |
79 | { |
||
80 | 12 | if (isset($this->data['payment']['merchant_payment_code'])) { |
|
81 | 9 | return $this->data['payment']['merchant_payment_code']; |
|
82 | } |
||
83 | |||
84 | 3 | return null; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Get the error message from the response. |
||
89 | * |
||
90 | * Returns null if the request was successful. |
||
91 | * |
||
92 | * @return string|null |
||
93 | */ |
||
94 | 15 | public function getMessage() |
|
95 | { |
||
96 | 15 | if (!$this->isSuccessful()) { |
|
97 | 6 | return '[' . $this->data['status_code'] . '] ' . $this->data['status_message']; |
|
98 | 9 | } elseif ($boletoData = $this->getBoleto()) { |
|
99 | 3 | return $boletoData['boleto_barcode']; |
|
100 | 6 | } elseif ($transactionStatus = $this->getTransactionStatus()) { |
|
101 | 3 | return '[' . $transactionStatus['code'] . '] ' .$transactionStatus['description']; |
|
102 | } |
||
103 | |||
104 | 3 | return null; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get a cardReference, from the createCard requests. |
||
109 | * |
||
110 | * @return string|null |
||
111 | */ |
||
112 | 6 | public function getCardReference() |
|
120 | |||
121 | /** |
||
122 | * Get all paymentData, from the requests. |
||
123 | * |
||
124 | * @return string|null |
||
125 | */ |
||
126 | 27 | public function getPaymentData($key = null) |
|
127 | { |
||
128 | 27 | if (isset($this->data['payment'])) { |
|
129 | 24 | if ($key) { |
|
130 | 24 | if (isset($this->data['payment'][$key])) { |
|
131 | 21 | return $this->data['payment'][$key]; |
|
132 | } else { |
||
133 | 6 | return null; |
|
134 | } |
||
135 | } |
||
136 | |||
137 | 3 | return $this->data['payment']; |
|
138 | } |
||
139 | |||
140 | 3 | return null; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get the boleto_url, boleto_barcode and boleto_expiration_date in the |
||
145 | * transaction object. |
||
146 | * |
||
147 | * @return array|null the boleto_url, boleto_barcode and boleto_expiration_date |
||
148 | */ |
||
149 | 12 | public function getBoleto() |
|
150 | { |
||
151 | 12 | $data = null; |
|
152 | |||
153 | 12 | if (isset($this->data['payment']['boleto_url'])) { |
|
154 | $data = array( |
||
155 | 6 | 'boleto_url' => $this->data['payment']['boleto_url'], |
|
156 | 6 | 'boleto_barcode' => $this->data['payment']['boleto_barcode'], |
|
157 | 6 | 'boleto_expiration_date' => $this->data['payment']['due_date'], |
|
158 | ); |
||
159 | } |
||
160 | |||
161 | 12 | return $data; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Get the payment status out of the response array |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | 9 | public function getPaymentStatus() |
|
174 | |||
175 | /** |
||
176 | * Get the transaction status out of the response array |
||
177 | * |
||
178 | * @return array|null |
||
179 | */ |
||
180 | 9 | public function getTransactionStatus() |
|
185 | |||
186 | /** |
||
187 | * Get the refunds propeties out of the response array |
||
188 | * |
||
189 | * @return array|null |
||
190 | */ |
||
191 | 3 | public function getRefunds() |
|
196 | |||
197 | |||
198 | /** |
||
199 | * Get the risk analysis in the transaction object. |
||
200 | * |
||
201 | * @return array|null the boleto_url, boleto_barcode and boleto_expiration_date |
||
202 | */ |
||
203 | 6 | public function getRiskAnalysis() |
|
213 | |||
214 | } |
||
215 |
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.