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:
Complex classes like SendgridV3Transport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SendgridV3Transport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class SendgridV3Transport extends Transport |
||
14 | { |
||
15 | const MAXIMUM_FILE_SIZE = 7340032; |
||
16 | const SMTP_API_NAME = 'sendgrid/x-smtpapi'; |
||
17 | const BASE_URL = 'https://api.sendgrid.com/v3/mail/send'; |
||
18 | |||
19 | /** |
||
20 | * @var Client |
||
21 | */ |
||
22 | private $client; |
||
23 | private $options; |
||
24 | private $attachments; |
||
25 | private $numberOfRecipients; |
||
26 | |||
27 | View Code Duplication | public function __construct(ClientInterface $client, $api_key) |
|
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
||
75 | |||
76 | /** |
||
77 | * @param Swift_Mime_Message $message |
||
78 | * @return array |
||
79 | */ |
||
80 | private function getPersonalizations(Swift_Mime_Message $message) |
||
107 | |||
108 | /** |
||
109 | * Get From Addresses. |
||
110 | * |
||
111 | * @param Swift_Mime_Message $message |
||
112 | * @return array |
||
113 | */ |
||
114 | private function getFrom(Swift_Mime_Message $message) |
||
123 | |||
124 | /** |
||
125 | * Get ReplyTo Addresses. |
||
126 | * |
||
127 | * @param Swift_Mime_Message $message |
||
128 | * @return array |
||
129 | */ |
||
130 | private function getReplyTo(Swift_Mime_Message $message) |
||
139 | |||
140 | /** |
||
141 | * Get contents. |
||
142 | * |
||
143 | * @param Swift_Mime_Message $message |
||
144 | * @return array |
||
145 | */ |
||
146 | private function getContents(Swift_Mime_Message $message) |
||
167 | |||
168 | /** |
||
169 | * @param Swift_Mime_Message $message |
||
170 | * @return array |
||
171 | */ |
||
172 | private function getAttachments(Swift_Mime_Message $message) |
||
192 | |||
193 | /** |
||
194 | * Set Request Body Parameters |
||
195 | * |
||
196 | * @param Swift_Mime_Message $message |
||
197 | * @param array $data |
||
198 | * @return array |
||
199 | * @throws \Exception |
||
200 | */ |
||
201 | protected function setParameters(Swift_Mime_Message $message, $data) |
||
248 | |||
249 | private function setPersonalizations(&$data, $personalizations) |
||
262 | |||
263 | /** |
||
264 | * @param $payload |
||
265 | * @return Response |
||
266 | */ |
||
267 | private function post($payload) |
||
271 | } |
||
272 |
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.