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 |
||
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) { |
||
150 | |||
151 | /** |
||
152 | * Get the mink context, so we can visit pages using the mink session. |
||
153 | */ |
||
154 | protected function getMinkContext() { |
||
161 | |||
162 | /** |
||
163 | * By default, prevent mail from being actually sent out during tests. |
||
164 | * |
||
165 | * @BeforeScenario |
||
166 | */ |
||
167 | public function disableMail() { |
||
174 | |||
175 | /** |
||
176 | * Restore mail sending. |
||
177 | * |
||
178 | * @AfterScenario |
||
179 | */ |
||
180 | public function enableMail() { |
||
183 | |||
184 | /** |
||
185 | * Allow opting in to actually sending mail out. |
||
186 | * |
||
187 | * @BeforeScenario @sendmail @sendemail |
||
188 | */ |
||
189 | public function sendMail() { |
||
192 | |||
193 | /** |
||
194 | * Allow opting in to mail collection. When using the default mail manager |
||
195 | * service, it is not necessary to use this tag. |
||
196 | * |
||
197 | * @BeforeScenario @mail @email |
||
198 | */ |
||
199 | public function collectMail() { |
||
202 | |||
203 | /** |
||
204 | * Stop collecting mail at scenario end. |
||
205 | * |
||
206 | * @AfterScenario @mail @email |
||
207 | */ |
||
208 | public function stopCollectingMail() { |
||
211 | |||
212 | /** |
||
213 | * This is mainly useful for testing this context. |
||
214 | * |
||
215 | * @When Drupal sends a/an (e)mail: |
||
216 | */ |
||
217 | public function DrupalSendsMail(TableNode $fields) { |
||
233 | |||
234 | /** |
||
235 | * Check all mail sent during the scenario. |
||
236 | * |
||
237 | * @Then (e)mail(s) has/have been sent: |
||
238 | * @Then (e)mail(s) has/have been sent to :to: |
||
239 | */ |
||
240 | View Code Duplication | public function mailHasBeenSent(TableNode $expectedMailTable, $to = NULL) { |
|
249 | |||
250 | /** |
||
251 | * Check mail sent since the last step that checked mail. |
||
252 | * |
||
253 | * @Then new (e)mail(s) is/are sent: |
||
254 | * @Then new (e)mail(s) is/are sent to :to: |
||
255 | */ |
||
256 | View Code Duplication | public function newMailIsSent(TableNode $expectedMailTable, $to = NULL) { |
|
265 | |||
266 | /** |
||
267 | * Check all mail sent during the scenario. |
||
268 | * |
||
269 | * @Then no (e)mail(s) has/have been sent |
||
270 | * @Then no (e)mail(s) has/have been sent to :to |
||
271 | */ |
||
272 | View Code Duplication | public function noMailHasBeenSent($to = NULL) { |
|
280 | |||
281 | /** |
||
282 | * Check mail sent since the last step that checked mail. |
||
283 | * |
||
284 | * @Then no new (e)mail(s) is/are sent |
||
285 | * @Then no new (e)mail(s) is/are sent to :to |
||
286 | */ |
||
287 | View Code Duplication | public function noNewMailIsSent($to = NULL) { |
|
295 | |||
296 | /** |
||
297 | * @When I follow the link to :urlFragment from the (e)mail |
||
298 | * @When I follow the link to :urlFragment from the (e)mail to :to |
||
299 | * @When I follow the link to :urlFragment from the (e)mail with the subject :subject |
||
300 | * @When I follow the link to :urlFragment from the (e)mail to :to with the subject :subject |
||
301 | */ |
||
302 | public function followLinkInMail($urlFragment, $to = '', $subject = '') { |
||
325 | |||
326 | } |
||
327 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.