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 Newsletter |
||
8 | { |
||
9 | /** @var \DrewM\MailChimp\MailChimp */ |
||
10 | protected $mailChimp; |
||
11 | |||
12 | /** @var \Spatie\Newsletter\NewsletterListCollection */ |
||
13 | protected $lists; |
||
14 | |||
15 | public function __construct(MailChimp $mailChimp, NewsletterListCollection $lists) |
||
21 | |||
22 | public function subscribe(string $email, array $mergeFields = [], string $listName = '', array $options = []) |
||
23 | { |
||
24 | $list = $this->lists->findByName($listName); |
||
25 | |||
26 | $options = $this->getSubscriptionOptions($email, $mergeFields, $options); |
||
27 | |||
28 | $response = $this->mailChimp->post("lists/{$list->getId()}/members", $options); |
||
29 | |||
30 | if (! $this->lastActionSucceeded()) { |
||
31 | return false; |
||
32 | } |
||
33 | |||
34 | return $response; |
||
35 | } |
||
36 | |||
37 | public function subscribePending(string $email, array $mergeFields = [], string $listName = '', array $options = []) |
||
43 | |||
44 | public function subscribeOrUpdate(string $email, array $mergeFields = [], string $listName = '', array $options = []) |
||
45 | { |
||
46 | $list = $this->lists->findByName($listName); |
||
47 | |||
48 | $options = $this->getSubscriptionOptions($email, $mergeFields, $options); |
||
49 | |||
50 | $response = $this->mailChimp->put("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", $options); |
||
51 | |||
52 | if (! $this->lastActionSucceeded()) { |
||
53 | return false; |
||
54 | } |
||
55 | |||
56 | return $response; |
||
57 | } |
||
58 | |||
59 | public function getMembers(string $listName = '', array $parameters = []) |
||
65 | |||
66 | public function getMember(string $email, string $listName = '') |
||
72 | |||
73 | public function getMemberActivity(string $email, string $listName = '') |
||
79 | |||
80 | public function hasMember(string $email, string $listName = ''): bool |
||
94 | |||
95 | public function isSubscribed(string $email, string $listName = ''): bool |
||
109 | |||
110 | View Code Duplication | public function unsubscribe(string $email, string $listName = '') |
|
124 | |||
125 | View Code Duplication | public function updateEmailAddress(string $currentEmailAddress, string $newEmailAddress, string $listName = '') |
|
135 | |||
136 | View Code Duplication | public function delete(string $email, string $listName = '') |
|
144 | |||
145 | View Code Duplication | public function deletePermanently(string $email, string $listName = '') |
|
146 | { |
||
153 | |||
154 | public function getTags(string $email, string $listName = '') |
||
160 | |||
161 | View Code Duplication | public function addTags(array $tags, string $email, string $listName = '') |
|
173 | |||
174 | View Code Duplication | public function removeTags(array $tags, string $email, string $listName = '') |
|
186 | |||
187 | public function createCampaign( |
||
228 | |||
229 | public function updateContent(string $campaignId, string $html, array $options = []) |
||
243 | |||
244 | public function getApi(): MailChimp |
||
248 | |||
249 | /** |
||
250 | * @return array|false |
||
251 | */ |
||
252 | public function getLastError() |
||
256 | |||
257 | public function lastActionSucceeded(): bool |
||
261 | |||
262 | protected function getSubscriberHash(string $email): string |
||
266 | |||
267 | protected function getSubscriptionOptions(string $email, array $mergeFields, array $options): array |
||
283 | } |
||
284 |
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.