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 |
||
12 | class MailjetDriver implements Driver |
||
13 | { |
||
14 | /** @var Client */ |
||
15 | public $client; |
||
16 | |||
17 | /** @var NewsletterListCollection */ |
||
18 | public $lists; |
||
19 | |||
20 | /** @var array */ |
||
21 | private $lastError; |
||
22 | |||
23 | public function __construct(array $credentials, array $config) |
||
32 | |||
33 | /** |
||
34 | * @param string $email |
||
35 | * @param array $options |
||
36 | * @param string $listName |
||
37 | * @return Response |
||
38 | * @throws ApiError |
||
39 | * @throws InvalidNewsletterList |
||
40 | */ |
||
41 | public function subscribe(string $email, array $options = [], string $listName = '') |
||
61 | |||
62 | /** |
||
63 | * @param string $email |
||
64 | * @param array $options |
||
65 | * @param string $listName |
||
66 | * @return Response |
||
67 | * @throws ApiError |
||
68 | * @throws InvalidNewsletterList |
||
69 | */ |
||
70 | public function subscribeOrUpdate(string $email, array $options = [], string $listName = '') |
||
74 | |||
75 | /** |
||
76 | * @param string $email |
||
77 | * @param array $options |
||
78 | * @param string $listName |
||
79 | * @return Response |
||
80 | * @throws ApiError |
||
81 | * @throws InvalidNewsletterList |
||
82 | */ |
||
83 | public function addMember(string $email, array $options = [], string $listName = '') |
||
89 | |||
90 | /** |
||
91 | * @param string $listName |
||
92 | * @param array $parameters |
||
93 | * @return array |
||
94 | * @throws ApiError |
||
95 | * @throws InvalidNewsletterList |
||
96 | */ |
||
97 | public function getMembers(string $listName = '', array $parameters = []) |
||
116 | |||
117 | /** |
||
118 | * @param string $email |
||
119 | * @param string $listName |
||
120 | * @return array |
||
121 | * @throws ApiError |
||
122 | */ |
||
123 | public function getMember(string $email, string $listName = '') |
||
134 | |||
135 | |||
136 | /** |
||
137 | * @param string $email |
||
138 | * @param string $listName |
||
139 | * @return bool |
||
140 | * @throws ApiError |
||
141 | * @throws InvalidNewsletterList |
||
142 | */ |
||
143 | View Code Duplication | public function hasMember(string $email, string $listName = ''): bool |
|
164 | |||
165 | /** |
||
166 | * @param string $email |
||
167 | * @param string $listName |
||
168 | * @return bool |
||
169 | * @throws ApiError |
||
170 | * @throws InvalidNewsletterList |
||
171 | */ |
||
172 | View Code Duplication | public function isSubscribed(string $email, string $listName = ''): bool |
|
193 | |||
194 | /** |
||
195 | * @param string $email |
||
196 | * @param string $listName |
||
197 | * @return Response |
||
198 | * @throws ApiError |
||
199 | * @throws InvalidNewsletterList |
||
200 | */ |
||
201 | View Code Duplication | public function unsubscribe(string $email, string $listName = '') |
|
219 | |||
220 | |||
221 | /** |
||
222 | * @param string $email |
||
223 | * @param string $listName |
||
224 | * @return Response |
||
225 | * @throws ApiError |
||
226 | * @throws InvalidNewsletterList |
||
227 | */ |
||
228 | View Code Duplication | public function delete(string $email, string $listName = '') |
|
250 | |||
251 | public function getLastError() |
||
255 | |||
256 | public function getApi() |
||
260 | } |
||
261 |
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.