Complex classes like SendgridTransport 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 SendgridTransport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class SendgridTransport extends Transport |
||
16 | { |
||
17 | use SendGrid { |
||
18 | sgDecode as decode; |
||
19 | } |
||
20 | |||
21 | const SMTP_API_NAME = 'sendgrid/x-smtpapi'; |
||
22 | const BASE_URL = 'https://api.sendgrid.com/v3/mail/send'; |
||
23 | |||
24 | /** |
||
25 | * @var Client |
||
26 | */ |
||
27 | private $client; |
||
28 | private $attachments; |
||
29 | private $numberOfRecipients; |
||
30 | private $apiKey; |
||
31 | private $endpoint; |
||
32 | |||
33 | public function __construct(ClientInterface $client, $api_key, $endpoint = null) |
||
34 | { |
||
35 | $this->client = $client; |
||
|
|||
36 | $this->apiKey = $api_key; |
||
37 | $this->endpoint = isset($endpoint) ? $endpoint : self::BASE_URL; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
||
91 | |||
92 | /** |
||
93 | * @param Swift_Mime_SimpleMessage $message |
||
94 | * @return array |
||
95 | */ |
||
96 | private function getPersonalizations(Swift_Mime_SimpleMessage $message) |
||
123 | |||
124 | /** |
||
125 | * Get From Addresses. |
||
126 | * |
||
127 | * @param Swift_Mime_SimpleMessage $message |
||
128 | * @return array |
||
129 | */ |
||
130 | private function getFrom(Swift_Mime_SimpleMessage $message) |
||
139 | |||
140 | /** |
||
141 | * Get ReplyTo Addresses. |
||
142 | * |
||
143 | * @param Swift_Mime_SimpleMessage $message |
||
144 | * @return array |
||
145 | */ |
||
146 | private function getReplyTo(Swift_Mime_SimpleMessage $message) |
||
155 | |||
156 | /** |
||
157 | * Get contents. |
||
158 | * |
||
159 | * @param Swift_Mime_SimpleMessage $message |
||
160 | * @return array |
||
161 | */ |
||
162 | private function getContents(Swift_Mime_SimpleMessage $message) |
||
204 | |||
205 | /** |
||
206 | * @param Swift_Mime_SimpleMessage $message |
||
207 | * @return array |
||
208 | */ |
||
209 | private function getAttachments(Swift_Mime_SimpleMessage $message) |
||
228 | |||
229 | /** |
||
230 | * Set Request Body Parameters |
||
231 | * |
||
232 | * @param Swift_Mime_SimpleMessage $message |
||
233 | * @param array $data |
||
234 | * @return array |
||
235 | * @throws \Exception |
||
236 | */ |
||
237 | protected function setParameters(Swift_Mime_SimpleMessage $message, $data) |
||
288 | |||
289 | private function setPersonalizations(&$data, $personalizations) |
||
302 | |||
303 | /** |
||
304 | * @param $payload |
||
305 | * @return Response |
||
306 | */ |
||
307 | private function post($payload) |
||
311 | } |
||
312 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.