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 |
||
16 | trait SendGridApi |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $sg_params = []; |
||
22 | |||
23 | /** |
||
24 | * clean for posting to sendgrid |
||
25 | * @param $params |
||
26 | * @return array|mixed |
||
27 | */ |
||
28 | public function cleanParams($params) |
||
50 | |||
51 | /** |
||
52 | * @param bool $clean |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getSgParams($clean = true) |
||
56 | { |
||
57 | return $clean ? $this->cleanParams($this->sg_params) : $this->sg_params; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param array $sg_params |
||
62 | */ |
||
63 | public function setSgParams($sg_params) |
||
64 | { |
||
65 | $this->sg_params = $sg_params; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param array $personalizations |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function setPersonalizations($personalizations) |
||
73 | { |
||
74 | $this->sg_params['personalizations'] = $personalizations; |
||
75 | return $this; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param Personalize $personalize |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function addPersonalizations(Personalize $personalize) |
||
83 | { |
||
84 | $this->sg_params['personalizations'][] = $personalize; |
||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param $email |
||
90 | * @param null $name |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setFrom($email, $name = null) |
||
98 | |||
99 | /** |
||
100 | * @param $email |
||
101 | * @param null $name |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function setReplyTo($email, $name = null) |
||
109 | |||
110 | /** |
||
111 | * @param string $subject |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function setSubject($subject) |
||
119 | |||
120 | /** |
||
121 | * @param array $content |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function setContent($content) |
||
129 | |||
130 | /** |
||
131 | * @param $type |
||
132 | * @param $value |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function addContent($type, $value = null) |
||
140 | |||
141 | /** |
||
142 | * @param array $attachments |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function setAttachments($attachments) |
||
146 | { |
||
147 | $this->sg_params['attachments'] = $attachments; |
||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param Attachment $attachment |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function addAttachments(Attachment $attachment) |
||
156 | { |
||
157 | $this->sg_params['attachments'][] = $attachment; |
||
158 | return $this; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param string $id |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function setTemplateId($id) |
||
166 | { |
||
167 | $this->sg_params['template_id'] = $id; |
||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param Sections $section |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function setSection($section) |
||
176 | { |
||
177 | $this->sg_params['section'] = $section; |
||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param string $key |
||
183 | * @param string $value |
||
184 | * @return $this |
||
185 | */ |
||
186 | View Code Duplication | public function addSection($key, $value) |
|
195 | |||
196 | /** |
||
197 | * @param Headers $headers |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function setHeaders($headers) |
||
201 | { |
||
202 | $this->sg_params['headers'] = $headers; |
||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param string $key |
||
208 | * @param string $value |
||
209 | * @return $this |
||
210 | */ |
||
211 | View Code Duplication | public function addHeaders($key, $value) |
|
219 | |||
220 | /** |
||
221 | * @param array $categories |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function setCategories($categories) |
||
225 | { |
||
226 | $this->sg_params['categories'] = $categories; |
||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param string $category |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function addCategories($category) |
||
239 | |||
240 | /** |
||
241 | * @param CustomArgs $custom_args |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function setCustomArgs($custom_args) |
||
249 | |||
250 | /** |
||
251 | * @param string $key |
||
252 | * @param string $val |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function addCustomArgs($key, $val) |
||
263 | |||
264 | /** |
||
265 | * @param int $send_at |
||
266 | * @return $this |
||
267 | */ |
||
268 | public function setSendAt($send_at) |
||
273 | |||
274 | /** |
||
275 | * @param string $batch_id |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function setBatchId($batch_id) |
||
283 | |||
284 | /** |
||
285 | * @param Asm $asm |
||
286 | * @return $this |
||
287 | */ |
||
288 | public function setAsm(Asm $asm) |
||
293 | |||
294 | /** |
||
295 | * @param string $ip_pool_name |
||
296 | * @return $this |
||
297 | */ |
||
298 | public function setIpPoolName($ip_pool_name) |
||
303 | |||
304 | /** |
||
305 | * @param MailSettings $mailSetting |
||
306 | * @return $this |
||
307 | */ |
||
308 | public function setMailSettings(MailSettings $mailSetting) |
||
313 | |||
314 | /** |
||
315 | * @param TrackingSettings $trackingSettings |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function setTrackingSettings(TrackingSettings $trackingSettings) |
||
323 | } |
||
324 |
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.