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 |
||
7 | class PaymentTest extends TestCase |
||
8 | { |
||
9 | //todo: credit card hash |
||
10 | |||
11 | /** |
||
12 | * MoipTest creating a credit card payment, passing all credit card data. |
||
13 | */ |
||
14 | public function testCreditCardPCI() |
||
27 | |||
28 | /** |
||
29 | * MoipTest creating a billet payment. |
||
30 | */ |
||
31 | View Code Duplication | public function testBillet() |
|
32 | { |
||
33 | $this->mockHttpSession($this->body_order); |
||
34 | $order = $this->createOrder()->create(); |
||
35 | $this->mockHttpSession($this->body_billet_pay); |
||
36 | $payment = $order->payments()->setBoleto(new \DateTime('today +1day'), 'http://dev.moip.com.br/images/logo-header-moip.png')->execute(); |
||
37 | $this->assertNotEmpty($payment->getFundingInstrument()->boleto); |
||
38 | } |
||
39 | |||
40 | View Code Duplication | public function testCreditCardPCIStore() |
|
50 | |||
51 | View Code Duplication | public function testShouldCreateEscrowPaymentWithCreditCard() |
|
63 | |||
64 | /** |
||
65 | * MoipTest creating a credit card multipayment, passing all credit card data. |
||
66 | */ |
||
67 | public function testMultipaymentCreditCardPCI() |
||
80 | |||
81 | /** |
||
82 | * MoipTest creating a billet multipayment. |
||
83 | */ |
||
84 | View Code Duplication | public function testMultipaymentBillet() |
|
92 | |||
93 | View Code Duplication | public function testCapturePreAuthorizedPayment() |
|
108 | |||
109 | public function testCapturePreAuthorizedMultiPayment() |
||
110 | { |
||
111 | $this->mockHttpSession($this->body_multiorder); |
||
112 | $order = $this->createMultiorder()->create(); |
||
113 | $this->mockHttpSession($this->body_cc_multipay); |
||
114 | $payment = $order->multipayments() |
||
115 | ->setCreditCard(5, 2018, '4012001037141112', 123, $this->createHolder()) |
||
116 | ->setDelayCapture(true) |
||
117 | ->execute(); |
||
118 | |||
119 | $this->mockHttpSession($this->body_capture_multipay); |
||
120 | $captured_payment = $payment->capture(); |
||
121 | |||
122 | $this->assertEquals('AUTHORIZED', $captured_payment->getStatus()); |
||
123 | } |
||
124 | |||
125 | public function testCancelPreAuthorizedMultiPayment() |
||
126 | { |
||
127 | $this->mockHttpSession($this->body_multiorder); |
||
128 | $order = $this->createMultiorder()->create(); |
||
129 | $this->mockHttpSession($this->body_cc_multipay); |
||
130 | $payment = $order->multipayments() |
||
131 | ->setCreditCard(5, 2018, '4012001037141112', 123, $this->createHolder()) |
||
132 | ->setDelayCapture(true) |
||
133 | ->execute(); |
||
134 | |||
135 | $this->mockHttpSession($this->body_cancel_multipay); |
||
136 | $cancelled_payment = $payment->cancel(); |
||
137 | |||
138 | $this->assertEquals('CANCELLED', $cancelled_payment->getStatus()); |
||
139 | } |
||
140 | |||
141 | View Code Duplication | public function testCancelPreAuthorizedPayment() |
|
156 | |||
157 | public function testGetPayment() |
||
171 | |||
172 | public function testGetMultiPayment() |
||
185 | } |
||
186 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: