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 |
||
11 | class NewsletterTest extends PHPUnit_Framework_TestCase |
||
12 | { |
||
13 | /** @var Mockery\Mock */ |
||
14 | protected $mailChimpApi; |
||
15 | |||
16 | /** @var \Spatie\Newsletter\Newsletter */ |
||
17 | protected $newsletter; |
||
18 | |||
19 | public function setUp() |
||
20 | { |
||
21 | $this->mailChimpApi = Mockery::mock(MailChimp::class); |
||
22 | |||
23 | $this->mailChimpApi->shouldReceive('getLastError')->andReturn(false); |
||
24 | |||
25 | $newsletterLists = NewsletterListCollection::createFromConfig( |
||
26 | [ |
||
27 | 'lists' => [ |
||
28 | 'list1' => ['id' => 123], |
||
29 | 'list2' => ['id' => 456], |
||
30 | ], |
||
31 | 'defaultListName' => 'list1', |
||
32 | ] |
||
33 | |||
34 | ); |
||
35 | |||
36 | $this->newsletter = new Newsletter($this->mailChimpApi, $newsletterLists); |
||
37 | } |
||
38 | |||
39 | public function tearDown() |
||
45 | |||
46 | /** @test */ |
||
47 | View Code Duplication | public function it_can_subscribe_someone() |
|
|
|||
48 | { |
||
49 | $email = '[email protected]'; |
||
50 | |||
51 | $url = 'lists/123/members'; |
||
52 | |||
53 | $this->mailChimpApi->shouldReceive('post')->withArgs([ |
||
54 | $url, |
||
55 | [ |
||
56 | 'email_address' => $email, |
||
57 | 'status' => 'subscribed', |
||
58 | 'email_type' => 'html', |
||
59 | ], |
||
60 | ]); |
||
61 | |||
62 | $this->newsletter->subscribe($email); |
||
63 | } |
||
64 | |||
65 | /** @test */ |
||
66 | public function it_can_subscribe_someone_with_merge_fields() |
||
67 | { |
||
68 | $email = '[email protected]'; |
||
69 | |||
70 | $mergeFields = ['FNAME' => 'Freek']; |
||
71 | |||
72 | $url = 'lists/123/members'; |
||
73 | |||
74 | $this->mailChimpApi->shouldReceive('post') |
||
75 | ->once() |
||
76 | ->withArgs([ |
||
77 | $url, |
||
78 | [ |
||
79 | 'email_address' => $email, |
||
80 | 'status' => 'subscribed', |
||
81 | 'merge_fields' => $mergeFields, |
||
82 | 'email_type' => 'html', |
||
83 | ], |
||
84 | ]); |
||
85 | |||
86 | $this->newsletter->subscribe($email, $mergeFields); |
||
87 | } |
||
88 | |||
89 | /** @test */ |
||
90 | View Code Duplication | public function it_can_subscribe_someone_to_an_alternative_list() |
|
109 | |||
110 | /** @test */ |
||
111 | public function it_can_override_the_defaults_when_subscribing_someone() |
||
112 | { |
||
113 | $email = '[email protected]'; |
||
114 | |||
115 | $url = 'lists/123/members'; |
||
116 | |||
117 | $this->mailChimpApi->shouldReceive('post') |
||
118 | ->once() |
||
119 | ->withArgs([ |
||
120 | $url, |
||
121 | [ |
||
122 | 'email_address' => $email, |
||
123 | 'status' => 'pending', |
||
124 | 'email_type' => 'text', |
||
125 | ], |
||
126 | ]); |
||
127 | |||
128 | $this->newsletter->subscribe($email, [], '', ['email_type' => 'text', 'status' => 'pending']); |
||
129 | } |
||
130 | |||
131 | /** @test */ |
||
132 | View Code Duplication | public function it_can_unsubscribe_someone() |
|
155 | |||
156 | /** @test */ |
||
157 | View Code Duplication | public function it_can_unsubscribe_someone_from_a_specific_list() |
|
180 | |||
181 | /** @test */ |
||
182 | public function it_can_delete_someone() |
||
183 | { |
||
184 | $email = '[email protected]'; |
||
200 | |||
201 | /** @test */ |
||
202 | public function it_can_delete_someone_from_a_specific_list() |
||
220 | |||
221 | /** @test */ |
||
222 | public function it_exposes_the_api() |
||
228 | |||
229 | /** @test */ |
||
230 | View Code Duplication | public function it_can_get_the_member() |
|
248 | |||
249 | /** @test */ |
||
250 | View Code Duplication | public function it_can_get_the_member_from_a_specific_list() |
|
268 | |||
269 | /** @test */ |
||
270 | public function is_can_create_a_campaign() |
||
317 | } |
||
318 |
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.