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 namespace nyx\notify\transports\mail\drivers; |
||
17 | class Sendgrid implements mail\interfaces\Driver |
||
18 | { |
||
19 | /** |
||
20 | * The traits of a Sendgrid Mail Driver instance. |
||
21 | */ |
||
22 | use traits\CountsRecipients; |
||
23 | |||
24 | /** |
||
25 | * @var string The API key (bearer token) used to authorize requests to Sendgrid's API. |
||
26 | */ |
||
27 | protected $key; |
||
28 | |||
29 | /** |
||
30 | * @var \GuzzleHttp\ClientInterface The underlying HTTP Client instance. |
||
31 | */ |
||
32 | protected $client; |
||
33 | |||
34 | /** |
||
35 | * Creates a new Sendgrid Mail Driver instance. |
||
36 | * |
||
37 | * @param \GuzzleHttp\ClientInterface $client The HTTP Client to use. |
||
38 | * @param string $key The API key to be used to authorize requests to SparkPost's API. |
||
39 | */ |
||
40 | public function __construct(\GuzzleHttp\ClientInterface $client, string $key) |
||
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | View Code Duplication | public function send(\Swift_Mime_Message $message, &$failures = null) |
|
60 | |||
61 | /** |
||
62 | * Converts a MIME Message to an array payload in a structure understood by Sendgrid's "mail/send" endpoint. |
||
63 | * |
||
64 | * @param \Swift_Mime_Message $message The Message to convert. |
||
65 | * @return array The resulting payload. |
||
66 | */ |
||
67 | View Code Duplication | protected function messageToPayload(\Swift_Mime_Message $message) : array |
|
80 | |||
81 | /** |
||
82 | * Processes the MIME headers of a MIME Message into the payload structure passed in by reference. |
||
83 | * |
||
84 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
85 | * @param array& $payload A reference to the payload structure. |
||
86 | */ |
||
87 | protected function processHeaders(\Swift_Mime_Message $message, array &$payload) |
||
119 | |||
120 | /** |
||
121 | * Processes the fields containing e-mail addresses (from, to, cc, etc.) in the MIME Message into |
||
122 | * the payload structure passed in by reference. |
||
123 | * |
||
124 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
125 | * @param array& $payload A reference to the payload structure. |
||
126 | */ |
||
127 | protected function processAddresses(\Swift_Mime_Message $message, array &$payload) |
||
150 | |||
151 | /** |
||
152 | * Parses the first and only the first address from the given array of e-mail addresses into a structure |
||
153 | * understood by Sendgrid's API. |
||
154 | * |
||
155 | * @param array $addresses The e-mail addresses to parse. |
||
156 | * @return array |
||
157 | */ |
||
158 | protected function processFirstAddress(array $addresses) : array |
||
171 | |||
172 | /** |
||
173 | * Parses the given array of e-mail addresses into a structure understood by Sendgrid's API. |
||
174 | * |
||
175 | * @param array $addresses The e-mail addresses to parse. |
||
176 | * @return array |
||
177 | */ |
||
178 | protected function processAllAddresses(array $addresses) : array |
||
193 | |||
194 | /** |
||
195 | * Processes the MIME entities in the MIME Message into the payload structure passed in by reference. |
||
196 | * |
||
197 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
198 | * @param array& $payload A reference to the payload structure. |
||
199 | */ |
||
200 | protected function processMimeEntities(\Swift_Mime_Message $message, array &$payload) |
||
237 | |||
238 | /** |
||
239 | * Returns the MIME part of the specified content type contained in the given MIME message, if it's present. |
||
240 | * |
||
241 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
242 | * @param string $mimeType The content type the part to return must be of. |
||
243 | * @return \Swift_Mime_MimeEntity |
||
244 | */ |
||
245 | View Code Duplication | protected function getMimePart(\Swift_Mime_Message $message, string $mimeType) |
|
253 | |||
254 | /** |
||
255 | * Processes the attachments in the MIME Message into the payload structure passed in by reference. |
||
256 | * |
||
257 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
258 | * @param array& $payload A reference to the payload structure. |
||
259 | */ |
||
260 | View Code Duplication | protected function processAttachments(\Swift_Mime_Message $message, array &$payload) |
|
289 | } |
||
290 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: