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 |
||
10 | class MailchimpDriver implements Driver |
||
11 | { |
||
12 | /** @var MailChimp */ |
||
13 | public $client; |
||
14 | |||
15 | /** @var NewsletterListCollection */ |
||
16 | public $lists; |
||
17 | |||
18 | /** |
||
19 | * MailchimpDriver constructor. |
||
20 | * @param array $credentials |
||
21 | * @param array $config |
||
22 | * @throws InvalidNewsletterList |
||
23 | */ |
||
24 | public function __construct(array $credentials, array $config) |
||
29 | |||
30 | |||
31 | /** |
||
32 | * @param string $email |
||
33 | * @param array $options |
||
34 | * @param string $listName |
||
35 | * @return array|false |
||
36 | * @throws ApiError |
||
37 | * @throws InvalidNewsletterList |
||
38 | */ |
||
39 | View Code Duplication | public function subscribe(string $email, array $options = [], string $listName = '') |
|
53 | |||
54 | |||
55 | /** |
||
56 | * @param string $email |
||
57 | * @param array $options |
||
58 | * @param string $listName |
||
59 | * @return array|bool|false |
||
60 | * @throws ApiError |
||
61 | * @throws InvalidNewsletterList |
||
62 | */ |
||
63 | View Code Duplication | public function subscribeOrUpdate(string $email, array $options = [], string $listName = '') |
|
77 | |||
78 | public function addMember(string $email, array $options = [], string $listName = '') |
||
84 | |||
85 | /** |
||
86 | * @param string $listName |
||
87 | * @param array $parameters |
||
88 | * @return array|false |
||
89 | * @throws InvalidNewsletterList |
||
90 | */ |
||
91 | public function getMembers(string $listName = '', array $parameters = []) |
||
97 | |||
98 | /** |
||
99 | * @param string $email |
||
100 | * @param string $listName |
||
101 | * @return array|false |
||
102 | * @throws InvalidNewsletterList |
||
103 | */ |
||
104 | public function getMember(string $email, string $listName = '') |
||
110 | |||
111 | /** |
||
112 | * @param string $email |
||
113 | * @param string $listName |
||
114 | * @return bool |
||
115 | * @throws InvalidNewsletterList |
||
116 | */ |
||
117 | public function hasMember(string $email, string $listName = ''): bool |
||
131 | |||
132 | /** |
||
133 | * @param string $email |
||
134 | * @param string $listName |
||
135 | * @return bool |
||
136 | * @throws InvalidNewsletterList |
||
137 | */ |
||
138 | public function isSubscribed(string $email, string $listName = ''): bool |
||
152 | |||
153 | /** |
||
154 | * @param string $email |
||
155 | * @param string $listName |
||
156 | * @return array|false |
||
157 | * @throws ApiError |
||
158 | * @throws InvalidNewsletterList |
||
159 | */ |
||
160 | public function unsubscribe(string $email, string $listName = '') |
||
174 | |||
175 | |||
176 | /** |
||
177 | * @param string $email |
||
178 | * @param string $listName |
||
179 | * @return array|false |
||
180 | * @throws InvalidNewsletterList |
||
181 | */ |
||
182 | public function delete(string $email, string $listName = '') |
||
190 | |||
191 | /** |
||
192 | * @return string|false |
||
193 | */ |
||
194 | public function getLastError() |
||
198 | |||
199 | /** |
||
200 | * @return MailChimp |
||
201 | */ |
||
202 | public function getApi(): MailChimp |
||
206 | |||
207 | /** |
||
208 | * @param string $email |
||
209 | * @return string |
||
210 | */ |
||
211 | protected function getSubscriberHash(string $email): string |
||
215 | |||
216 | /** |
||
217 | * @param string $email |
||
218 | * @param array $options |
||
219 | * @return array |
||
220 | */ |
||
221 | protected function getSubscriptionOptions(string $email, array $options): array |
||
233 | } |
||
234 |
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.