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 |
||
12 | class Refund extends MoipResource |
||
13 | { |
||
14 | /** |
||
15 | * @const string |
||
16 | */ |
||
17 | const PATH = 'refunds'; |
||
18 | |||
19 | /** |
||
20 | * Refunds means. |
||
21 | * |
||
22 | * @const string |
||
23 | */ |
||
24 | const METHOD_CREDIT_CARD = 'CREDIT_CARD'; |
||
25 | |||
26 | /** |
||
27 | * Refunds means. |
||
28 | * |
||
29 | * @const string |
||
30 | */ |
||
31 | const METHOD_BANK_ACCOUNT = 'BANK_ACCOUNT'; |
||
32 | |||
33 | /** |
||
34 | * Refunds means. |
||
35 | * |
||
36 | * @const string |
||
37 | */ |
||
38 | const METHOD_MONEY_ORDER = 'MONEY_ORDER'; |
||
39 | |||
40 | /** |
||
41 | * @var \Moip\Resource\Orders |
||
42 | */ |
||
43 | private $order; |
||
44 | |||
45 | /** |
||
46 | * @var \Moip\Resource\Payment |
||
47 | */ |
||
48 | private $payment; |
||
49 | |||
50 | /** |
||
51 | * Initializes new instances. |
||
52 | */ |
||
53 | public function initialize() |
||
57 | |||
58 | /** |
||
59 | * Mount refund structure. |
||
60 | * |
||
61 | * @param \stdClass $response |
||
62 | * |
||
63 | * @return $this |
||
64 | */ |
||
65 | protected function populate(stdClass $response) |
||
106 | |||
107 | /** |
||
108 | * Get id MoIP refund. |
||
109 | * |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getId() |
||
117 | |||
118 | /** |
||
119 | * Get a Refund. |
||
120 | * |
||
121 | * @param string $moip_id Refund id. |
||
122 | * |
||
123 | * @return stdClass |
||
124 | */ |
||
125 | public function get($moip_id) |
||
129 | |||
130 | /** |
||
131 | * Get status from MoIP refund. |
||
132 | * |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getStatus() |
||
140 | |||
141 | /** |
||
142 | * Get MoIP refund type. |
||
143 | * |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getType() |
||
151 | |||
152 | /** |
||
153 | * Create a new refund in api MoIP. |
||
154 | * |
||
155 | * @param stdClass $data |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | private function execute(stdClass $data = null, $resourceId = null) |
||
166 | |||
167 | /** |
||
168 | * Checks path that will be the request. |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | private function getPath($resourceId = null) |
||
186 | |||
187 | /** |
||
188 | * Bank account is the bank address of a particular vendor or a customer. |
||
189 | * |
||
190 | * @param string $type Kind of bank account. possible values: CHECKING, SAVING. |
||
191 | * @param string $bankNumber Bank number. possible values: 001, 237, 341, 041. |
||
192 | * @param int $agencyNumber Branch number. |
||
193 | * @param int $agencyCheckNumber Checksum of the agency. |
||
194 | * @param int $accountNumber Account number. |
||
195 | * @param int $accountCheckNumber Digit account checker. |
||
196 | * @param \Moip\Resource\Customer $holder |
||
197 | * |
||
198 | * @return \stdClass |
||
199 | */ |
||
200 | private function bankAccountDataCustomer($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder) |
||
220 | |||
221 | /** |
||
222 | * Bank account is the bank address of a particular vendor or a customer. |
||
223 | * |
||
224 | * @param \Moip\Resource\BankAccount $bankAccount |
||
225 | * |
||
226 | * @return \stdClass |
||
227 | */ |
||
228 | private function bankAccountData(BankAccount $bankAccount) |
||
248 | |||
249 | /** |
||
250 | * Making a full refund to the bank account. |
||
251 | * |
||
252 | * @param string $type Kind of bank account. possible values: CHECKING, SAVING. |
||
253 | * @param string $bankNumber Bank number. possible values: 001, 237, 341, 041. |
||
254 | * @param int $agencyNumber Branch number. |
||
255 | * @param int $agencyCheckNumber Checksum of the agency. |
||
256 | * @param int $accountNumber Account number. |
||
257 | * @param int $accountCheckNumber Digit account checker. |
||
258 | * @param \Moip\Resource\Customer $holder |
||
259 | * |
||
260 | * @return Refund |
||
261 | */ |
||
262 | public function bankAccountFull($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder) |
||
268 | |||
269 | /** |
||
270 | * Making a partial refund in the bank account. |
||
271 | * |
||
272 | * @param string $type Kind of bank account. possible values: CHECKING, SAVING. |
||
273 | * @param string $bankNumber Bank number. possible values: 001, 237, 341, 041. |
||
274 | * @param int $agencyNumber Branch number. |
||
275 | * @param int $agencyCheckNumber Checksum of the agency. |
||
276 | * @param int $accountNumber Account number. |
||
277 | * @param int $accountCheckNumber Digit account checker. |
||
278 | * @param \Moip\Resource\Customer $holder |
||
279 | * |
||
280 | * @return Refund |
||
281 | */ |
||
282 | public function bankAccountPartial($amount, $type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder) |
||
289 | |||
290 | /** |
||
291 | * Making a full refund in credit card. |
||
292 | * |
||
293 | * @return \Moip\Resource\Refund |
||
294 | */ |
||
295 | public function creditCardFull() |
||
299 | |||
300 | /** |
||
301 | * Making a partial refund in credit card. |
||
302 | * |
||
303 | * @param int|float $amount value of refund. |
||
304 | * |
||
305 | * @return \Moip\Resource\Refund |
||
306 | */ |
||
307 | public function creditCardPartial($amount) |
||
314 | |||
315 | /** |
||
316 | * Get iterator. |
||
317 | * |
||
318 | * @return \ArrayIterator |
||
319 | */ |
||
320 | public function getIterator() |
||
330 | |||
331 | /** |
||
332 | * Set order. |
||
333 | * |
||
334 | * @param \Moip\Resource\Orders $order |
||
335 | */ |
||
336 | public function setOrder(Orders $order) |
||
340 | |||
341 | /** |
||
342 | * Set payment. |
||
343 | * |
||
344 | * @param \Moip\Resource\Payment $payment |
||
345 | */ |
||
346 | public function setPayment(Payment $payment) |
||
350 | |||
351 | View Code Duplication | public function bankAccount($resourceId, BankAccount $bankAccount, $amount = null) |
|
361 | |||
362 | View Code Duplication | public function creditCard($resourceId, $amount = null) |
|
373 | |||
374 | private function isOrder($resourceId) |
||
378 | } |
||
379 |
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.