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 MailContext 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 MailContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class MailContext extends RawDrupalContext { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The mail manager. |
||
| 15 | * |
||
| 16 | * @var \Drupal\DrupalMailManagerInterface |
||
| 17 | */ |
||
| 18 | protected $mailManager; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The number of mails received so far in this scenario, for each mail store. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $mailCount = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Get the mail manager service that handles stored test mail. |
||
| 29 | * |
||
| 30 | * @return \Drupal\DrupalMailManagerInterface |
||
| 31 | * The mail manager service. |
||
| 32 | */ |
||
| 33 | protected function getMailManager() { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get collected mail, matching certain specifications. |
||
| 44 | * |
||
| 45 | * @param array $matches |
||
| 46 | * Associative array of mail fields and the values to filter by. |
||
| 47 | * @param bool $new |
||
| 48 | * Whether to ignore previously seen mail. |
||
| 49 | * @param null|int $index |
||
| 50 | * A particular mail to return, e.g. 0 for first or -1 for last. |
||
| 51 | * @param string $store |
||
| 52 | * The name of the mail store to get mail from. |
||
| 53 | * |
||
| 54 | * @return \stdClass[] |
||
| 55 | * An array of mail, each formatted as a Drupal 8 |
||
| 56 | * \Drupal\Core\Mail\MailInterface::mail $message array. |
||
| 57 | */ |
||
| 58 | protected function getMail($matches = [], $new = FALSE, $index = NULL, $store = 'default') { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get the number of mails received in a particular mail store. |
||
| 85 | * |
||
| 86 | * @return int |
||
| 87 | * The number of mails received during this scenario. |
||
| 88 | */ |
||
| 89 | protected function getMailCount($store) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Determine if a mail meets criteria. |
||
| 101 | * |
||
| 102 | * @param array $mail |
||
| 103 | * The mail, as an array of mail fields. |
||
| 104 | * @param array $matches |
||
| 105 | * The criteria: an associative array of mail fields and desired values. |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | * Whether the mail matches the criteria. |
||
| 109 | */ |
||
| 110 | protected function matchesMail($mail = [], $matches = []) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Compare actual mail with expected mail. |
||
| 124 | * |
||
| 125 | * @param array $actualMail |
||
| 126 | * An array of actual mail. |
||
| 127 | * @param array $expectedMail |
||
| 128 | * An array of expected mail. |
||
| 129 | */ |
||
| 130 | protected function compareMail($actualMail, $expectedMail) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Sort mail by to, subject and body. |
||
| 156 | * |
||
| 157 | * @param array $mail |
||
| 158 | * An array of mail to sort. |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | * The same mail, but sorted. |
||
| 162 | */ |
||
| 163 | protected function sortMail($mail) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the mink context, so we can visit pages using the mink session. |
||
| 203 | */ |
||
| 204 | protected function getMinkContext() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * By default, prevent mail from being actually sent out during tests. |
||
| 214 | * |
||
| 215 | * @BeforeScenario |
||
| 216 | */ |
||
| 217 | public function disableMail() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Restore mail sending. |
||
| 227 | * |
||
| 228 | * @AfterScenario |
||
| 229 | */ |
||
| 230 | public function enableMail() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Allow opting in to actually sending mail out. |
||
| 236 | * |
||
| 237 | * @BeforeScenario @sendmail @sendemail |
||
| 238 | */ |
||
| 239 | public function sendMail() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Allow opting in to mail collection. When using the default mail manager |
||
| 245 | * service, it is not necessary to use this tag. |
||
| 246 | * |
||
| 247 | * @BeforeScenario @mail @email |
||
| 248 | */ |
||
| 249 | public function collectMail() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Stop collecting mail at scenario end. |
||
| 255 | * |
||
| 256 | * @AfterScenario @mail @email |
||
| 257 | */ |
||
| 258 | public function stopCollectingMail() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * This is mainly useful for testing this context. |
||
| 264 | * |
||
| 265 | * @When Drupal sends a/an (e)mail: |
||
| 266 | */ |
||
| 267 | public function DrupalSendsMail(TableNode $fields) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Check all mail sent during the scenario. |
||
| 282 | * |
||
| 283 | * @Then (e)mail(s) has/have been sent: |
||
| 284 | * @Then (e)mail(s) has/have been sent to :to: |
||
| 285 | */ |
||
| 286 | View Code Duplication | public function mailHasBeenSent(TableNode $expectedMailTable, $to = NULL) { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Check mail sent since the last step that checked mail. |
||
| 298 | * |
||
| 299 | * @Then new (e)mail(s) is/are sent: |
||
| 300 | * @Then new (e)mail(s) is/are sent to :to: |
||
| 301 | */ |
||
| 302 | View Code Duplication | public function newMailIsSent(TableNode $expectedMailTable, $to = NULL) { |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Check all mail sent during the scenario. |
||
| 314 | * |
||
| 315 | * @Then no (e)mail(s) has/have been sent |
||
| 316 | * @Then no (e)mail(s) has/have been sent to :to |
||
| 317 | */ |
||
| 318 | View Code Duplication | public function noMailHasBeenSent($to = NULL) { |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Check mail sent since the last step that checked mail. |
||
| 329 | * |
||
| 330 | * @Then no new (e)mail(s) is/are sent |
||
| 331 | * @Then no new (e)mail(s) is/are sent to :to |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function noNewMailIsSent($to = NULL) { |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @When I follow the link to :urlFragment from the (e)mail |
||
| 344 | * @When I follow the link to :urlFragment from the (e)mail to :to |
||
| 345 | * @When I follow the link to :urlFragment from the (e)mail with the subject :subject |
||
| 346 | * @When I follow the link to :urlFragment from the (e)mail to :to with the subject :subject |
||
| 347 | */ |
||
| 348 | public function followLinkInMail($urlFragment, $to = '', $subject = '') { |
||
| 371 | |||
| 372 | } |
||
| 373 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.