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; |
||
15 | class Postmark implements mail\interfaces\Driver |
||
16 | { |
||
17 | /** |
||
18 | * The traits of a Postmark Mail Driver instance. |
||
19 | */ |
||
20 | use traits\CountsRecipients; |
||
21 | |||
22 | /** |
||
23 | * @var string The API key used to authorize requests to Postmark's API. |
||
24 | */ |
||
25 | protected $key; |
||
26 | |||
27 | /** |
||
28 | * @var \GuzzleHttp\ClientInterface The underlying HTTP Client instance. |
||
29 | */ |
||
30 | protected $client; |
||
31 | |||
32 | /** |
||
33 | * Creates a new Postmark Mail Driver instance. |
||
34 | * |
||
35 | * @param \GuzzleHttp\ClientInterface $client The HTTP Client to use. |
||
36 | * @param string $key The API key to be used to authorize requests to Postmark's API. |
||
37 | */ |
||
38 | public function __construct(\GuzzleHttp\ClientInterface $client, string $key) |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | View Code Duplication | public function send(\Swift_Mime_Message $message, &$failures = null) |
|
58 | |||
59 | /** |
||
60 | * Converts a MIME Message to an array payload in a structure understood by Postmark's "email" endpoint. |
||
61 | * |
||
62 | * @param \Swift_Mime_Message $message The Message to convert. |
||
63 | * @return array The resulting payload. |
||
64 | */ |
||
65 | View Code Duplication | protected function messageToPayload(\Swift_Mime_Message $message) : array |
|
78 | |||
79 | /** |
||
80 | * Processes the MIME headers of a MIME Message into the payload structure passed in by reference. |
||
81 | * |
||
82 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
83 | * @param array& $payload A reference to the payload structure. |
||
84 | */ |
||
85 | protected function processHeaders(\Swift_Mime_Message $message, array &$payload) |
||
132 | |||
133 | /** |
||
134 | * Processes the fields containing e-mail addresses (from, to, cc, etc.) in the MIME Message into |
||
135 | * the payload structure passed in by reference. |
||
136 | * |
||
137 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
138 | * @param array& $payload A reference to the payload structure. |
||
139 | */ |
||
140 | protected function processAddresses(\Swift_Mime_Message $message, array &$payload) |
||
157 | |||
158 | /** |
||
159 | * Converts an array of e-mail addresses into a string compliant with Postmark's API's format. |
||
160 | * |
||
161 | * @param array $emails The e-mail addresses to convert. |
||
162 | * @return string |
||
163 | */ |
||
164 | protected function emailsToString(array $emails) : string |
||
174 | |||
175 | /** |
||
176 | * Processes the MIME entities in the MIME Message into the payload structure passed in by reference. |
||
177 | * |
||
178 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
179 | * @param array& $payload A reference to the payload structure. |
||
180 | */ |
||
181 | protected function processMimeEntities(\Swift_Mime_Message $message, array &$payload) |
||
203 | |||
204 | /** |
||
205 | * Returns the MIME part of the specified content type contained in the given MIME message, if it's present. |
||
206 | * |
||
207 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
208 | * @param string $mimeType The content type the part to return must be of. |
||
209 | * @return \Swift_Mime_MimeEntity |
||
210 | */ |
||
211 | View Code Duplication | protected function getMimePart(\Swift_Mime_Message $message, string $mimeType) |
|
219 | |||
220 | /** |
||
221 | * Processes the attachments in the MIME Message into the payload structure passed in by reference. |
||
222 | * |
||
223 | * @param \Swift_Mime_Message $message The MIME Message to process. |
||
224 | * @param array& $payload A reference to the payload structure. |
||
225 | */ |
||
226 | View Code Duplication | protected function processAttachments(\Swift_Mime_Message $message, array &$payload) |
|
254 | } |
||
255 |
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: