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 |
||
11 | class SendgridV3Transport extends Transport |
||
12 | { |
||
13 | const MAXIMUM_FILE_SIZE = 7340032; |
||
14 | const SMTP_API_NAME = 'sendgrid/x-smtpapi'; |
||
15 | const BASE_URL = 'https://api.sendgrid.com/v3/mail/send'; |
||
16 | |||
17 | private $client; |
||
18 | private $options; |
||
19 | |||
20 | public function __construct(ClientInterface $client, $api_key) |
||
21 | { |
||
22 | $this->client = $client; |
||
23 | $this->options = [ |
||
24 | 'headers' => [ |
||
25 | 'Authorization' => 'Bearer ' . $api_key, |
||
26 | 'Content-Type' => 'application/json', |
||
27 | ], |
||
28 | ]; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
||
60 | |||
61 | /** |
||
62 | * @param Swift_Mime_Message $message |
||
63 | * @return array |
||
64 | */ |
||
65 | private function getPersonalizations(Swift_Mime_Message $message) |
||
92 | |||
93 | /** |
||
94 | * Get From Addresses. |
||
95 | * |
||
96 | * @param Swift_Mime_Message $message |
||
97 | * @return array |
||
98 | */ |
||
99 | private function getFrom(Swift_Mime_Message $message) |
||
108 | |||
109 | /** |
||
110 | * Get ReplyTo Addresses. |
||
111 | * |
||
112 | * @param Swift_Mime_Message $message |
||
113 | * @return array |
||
114 | */ |
||
115 | private function getReplyTo(Swift_Mime_Message $message) |
||
124 | |||
125 | /** |
||
126 | * Get contents. |
||
127 | * |
||
128 | * @param Swift_Mime_Message $message |
||
129 | * @return array |
||
130 | */ |
||
131 | private function getContents(Swift_Mime_Message $message) |
||
152 | |||
153 | /** |
||
154 | * @param Swift_Mime_Message $message |
||
155 | * @return array |
||
156 | */ |
||
157 | private function getAttachments(Swift_Mime_Message $message) |
||
158 | { |
||
159 | $attachments = []; |
||
160 | foreach ($message->getChildren() as $attachment) { |
||
161 | if ((!$attachment instanceof Swift_Attachment && !$attachment instanceof Swift_Image) || !strlen($attachment->getBody()) > self::MAXIMUM_FILE_SIZE) { |
||
162 | continue; |
||
163 | } |
||
164 | $attachments[] = [ |
||
165 | 'content' => base64_encode($attachment->getBody()), |
||
166 | 'filename' => $attachment->getFilename(), |
||
167 | 'type' => $attachment->getContentType(), |
||
168 | 'disposition' => $attachment->getDisposition(), |
||
169 | 'content_id' => $attachment->getId(), |
||
170 | ]; |
||
171 | } |
||
172 | return $attachments; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Set Request Body Parameters |
||
177 | * |
||
178 | * @param Swift_Mime_Message $message |
||
179 | * @param array $data |
||
180 | * @return array |
||
181 | * @throws \Exception |
||
182 | */ |
||
183 | protected function setParameters(Swift_Mime_Message $message, $data) |
||
224 | |||
225 | private function setPersonalizations(&$data, $personalizations) |
||
237 | } |
||
238 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.