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 |
||
21 | class OrderServiceTest extends PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | private $mailChimpStub; |
||
24 | private $mcOrder; |
||
25 | private $mcOrderLine; |
||
26 | private $mcProduct; |
||
27 | private $mcProductVariant; |
||
28 | private $mcCustomer; |
||
29 | private $mcAddress; |
||
30 | |||
31 | public function setUp() |
||
32 | { |
||
33 | parent::setUp(); // TODO: Change the autogenerated stub |
||
34 | |||
35 | $this->mailChimpStub = $this |
||
36 | ->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Services\MailChimp') |
||
37 | ->disableOriginalConstructor() |
||
38 | ->getMock(); |
||
39 | $this->mcOrder = $this |
||
40 | ->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\Order') |
||
41 | ->getMock(); |
||
42 | $this->mcOrderLine = $this |
||
43 | ->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\OrderLine') |
||
44 | ->getMock(); |
||
45 | $this->mcProduct = $this |
||
46 | ->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\Product') |
||
47 | ->getMock(); |
||
48 | $this->mcProductVariant = $this |
||
49 | ->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\ProductVariant') |
||
50 | ->getMock(); |
||
51 | $this->mcCustomer = $this |
||
52 | ->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\Customer') |
||
53 | ->getMock(); |
||
54 | $this->mcAddress = $this |
||
55 | ->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\Address') |
||
56 | ->getMock(); |
||
57 | } |
||
58 | |||
59 | public function testSendOrderToMailChimpSuccess() |
||
60 | { |
||
61 | $method = RequestTypes::$POST; |
||
62 | $resource = '/orders'; |
||
63 | $body = [ |
||
64 | 'landing_site' => "landing_site", |
||
65 | 'financial_status' => "processing", |
||
66 | 'fulfillment_status' => "", |
||
67 | 'currency_code' => "EUR", |
||
68 | 'order_total' => 21, |
||
69 | 'tax_total' => 4.48, |
||
70 | 'shipping_total' => 0, |
||
71 | 'tracking_code' => "prec", |
||
72 | 'processed_at_foreign' => "", |
||
73 | 'cancelled_at_foreign' => "", |
||
74 | 'updated_at_foreign' => "", |
||
75 | 'id' => "1234", |
||
76 | 'campaign_id' => "", |
||
77 | 'customer' => [ |
||
78 | 'id' => "50", |
||
79 | 'email_address' => "[email protected]", |
||
80 | 'opt_in_status' => true, |
||
81 | 'company' => "", |
||
82 | 'first_name' => "Kevin", |
||
83 | 'last_name' => "Murillo", |
||
84 | 'orders_count' => 5, |
||
85 | 'total_spent' => 50.75, |
||
86 | 'address' => [ |
||
87 | 'address1' => "Av. Test nº 92", |
||
88 | 'address2' => "", |
||
89 | 'city' => "Barcelona", |
||
90 | 'province' => "Barcelona", |
||
91 | 'province_code' => "BCN", |
||
92 | 'postal_code' => "08080", |
||
93 | 'country' => "Spain", |
||
94 | 'country_code' => "ESP", |
||
95 | ] |
||
96 | ], |
||
97 | 'shipping_address' => [ |
||
98 | 'name' => "shipping_address address for [email protected]", |
||
99 | 'address1' => "Av. Test nº 92", |
||
100 | 'address2' => "", |
||
101 | 'city' => "Barcelona", |
||
102 | 'province' => "Barcelona", |
||
103 | 'province_code' => "BCN", |
||
104 | 'postal_code' => "08080", |
||
105 | 'country' => "Spain", |
||
106 | 'country_code' => "ESP", |
||
107 | ], |
||
108 | 'billing_address' => [ |
||
109 | 'name' => "billing_address address for [email protected]", |
||
110 | 'address1' => "Av. Test nº 92", |
||
111 | 'address2' => "", |
||
112 | 'city' => "Barcelona", |
||
113 | 'province' => "Barcelona", |
||
114 | 'province_code' => "BCN", |
||
115 | 'postal_code' => "08080", |
||
116 | 'country' => "Spain", |
||
117 | 'country_code' => "ESP", |
||
118 | ] |
||
119 | ]; |
||
120 | |||
121 | // Create mocks |
||
122 | $this->setCustomerMock(); |
||
123 | $this->setAddressMock(); |
||
124 | $this->setOrderMock($this->mcCustomer, $this->mcAddress); |
||
125 | |||
126 | // Check that doRequest call have the right params |
||
127 | $this->mailChimpStub |
||
128 | ->expects($this->once()) |
||
129 | ->method('doRequest') |
||
130 | ->with( |
||
131 | $method, |
||
132 | $body, |
||
133 | $resource |
||
134 | ); |
||
135 | |||
136 | // Call method to test |
||
137 | $orderService = new OrderService($this->mailChimpStub); |
||
138 | $orderService->create($this->mcOrder); |
||
139 | } |
||
140 | |||
141 | private function setOrderMock($customerMock, $addressMock) |
||
172 | |||
173 | private function setOrderLineMock() |
||
193 | |||
194 | View Code Duplication | private function setProductMock() |
|
207 | |||
208 | View Code Duplication | private function setProductVariantMock() |
|
221 | |||
222 | private function setCustomerMock() |
||
243 | |||
244 | private function setAddressMock() |
||
264 | } |
||
265 |
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.