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 = []) { | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * Compare actual mail with expected mail.  | 
            ||
| 125 | *  | 
            ||
| 126 | * @param array $actualMail  | 
            ||
| 127 | * An array of actual mail.  | 
            ||
| 128 | * @param array $expectedMail  | 
            ||
| 129 | * An array of expected mail.  | 
            ||
| 130 | */  | 
            ||
| 131 |   protected function compareMail($actualMail, $expectedMail) { | 
            ||
| 158 | |||
| 159 | /**  | 
            ||
| 160 | * Sort mail by to, subject and body.  | 
            ||
| 161 | *  | 
            ||
| 162 | * @param array $mail  | 
            ||
| 163 | * An array of mail to sort.  | 
            ||
| 164 | *  | 
            ||
| 165 | * @return array  | 
            ||
| 166 | * The same mail, but sorted.  | 
            ||
| 167 | */  | 
            ||
| 168 |   protected function sortMail($mail) { | 
            ||
| 205 | |||
| 206 | /**  | 
            ||
| 207 | * Get the mink context, so we can visit pages using the mink session.  | 
            ||
| 208 | */  | 
            ||
| 209 |   protected function getMinkContext() { | 
            ||
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * By default, prevent mail from being actually sent out during tests.  | 
            ||
| 219 | *  | 
            ||
| 220 | * @BeforeScenario  | 
            ||
| 221 | */  | 
            ||
| 222 |   public function disableMail() { | 
            ||
| 229 | |||
| 230 | /**  | 
            ||
| 231 | * Restore mail sending.  | 
            ||
| 232 | *  | 
            ||
| 233 | * @AfterScenario  | 
            ||
| 234 | */  | 
            ||
| 235 |   public function enableMail() { | 
            ||
| 238 | |||
| 239 | /**  | 
            ||
| 240 | * Allow opting in to actually sending mail out.  | 
            ||
| 241 | *  | 
            ||
| 242 | * @BeforeScenario @sendmail @sendemail  | 
            ||
| 243 | */  | 
            ||
| 244 |   public function sendMail() { | 
            ||
| 247 | |||
| 248 | /**  | 
            ||
| 249 | * Allow opting in to mail collection. When using the default mail manager  | 
            ||
| 250 | * service, it is not necessary to use this tag.  | 
            ||
| 251 | *  | 
            ||
| 252 | * @BeforeScenario @mail @email  | 
            ||
| 253 | */  | 
            ||
| 254 |   public function collectMail() { | 
            ||
| 257 | |||
| 258 | /**  | 
            ||
| 259 | * Stop collecting mail at scenario end.  | 
            ||
| 260 | *  | 
            ||
| 261 | * @AfterScenario @mail @email  | 
            ||
| 262 | */  | 
            ||
| 263 |   public function stopCollectingMail() { | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * This is mainly useful for testing this context.  | 
            ||
| 269 | *  | 
            ||
| 270 | * @When Drupal sends a/an (e)mail:  | 
            ||
| 271 | */  | 
            ||
| 272 |   public function DrupalSendsMail(TableNode $fields) { | 
            ||
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * Check all mail sent during the scenario.  | 
            ||
| 287 | *  | 
            ||
| 288 | * @Then (e)mail(s) has/have been sent:  | 
            ||
| 289 | * @Then (e)mail(s) has/have been sent to :to:  | 
            ||
| 290 | */  | 
            ||
| 291 | View Code Duplication |   public function mailHasBeenSent(TableNode $expectedMailTable, $to = NULL) { | 
            |
| 300 | |||
| 301 | /**  | 
            ||
| 302 | * Check mail sent since the last step that checked mail.  | 
            ||
| 303 | *  | 
            ||
| 304 | * @Then new (e)mail(s) is/are sent:  | 
            ||
| 305 | * @Then new (e)mail(s) is/are sent to :to:  | 
            ||
| 306 | */  | 
            ||
| 307 | View Code Duplication |   public function newMailIsSent(TableNode $expectedMailTable, $to = NULL) { | 
            |
| 316 | |||
| 317 | /**  | 
            ||
| 318 | * Check all mail sent during the scenario.  | 
            ||
| 319 | *  | 
            ||
| 320 | * @Then no (e)mail(s) has/have been sent  | 
            ||
| 321 | * @Then no (e)mail(s) has/have been sent to :to  | 
            ||
| 322 | */  | 
            ||
| 323 | View Code Duplication |   public function noMailHasBeenSent($to = NULL) { | 
            |
| 331 | |||
| 332 | /**  | 
            ||
| 333 | * Check mail sent since the last step that checked mail.  | 
            ||
| 334 | *  | 
            ||
| 335 | * @Then no new (e)mail(s) is/are sent  | 
            ||
| 336 | * @Then no new (e)mail(s) is/are sent to :to  | 
            ||
| 337 | */  | 
            ||
| 338 | View Code Duplication |   public function noNewMailIsSent($to = NULL) { | 
            |
| 346 | |||
| 347 | /**  | 
            ||
| 348 | * @When I follow the link to :urlFragment from the (e)mail  | 
            ||
| 349 | * @When I follow the link to :urlFragment from the (e)mail to :to  | 
            ||
| 350 | * @When I follow the link to :urlFragment from the (e)mail with the subject :subject  | 
            ||
| 351 | * @When I follow the link to :urlFragment from the (e)mail to :to with the subject :subject  | 
            ||
| 352 | */  | 
            ||
| 353 |   public function followLinkInMail($urlFragment, $to = '', $subject = '') { | 
            ||
| 376 | |||
| 377 | }  | 
            ||
| 378 | 
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.