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 |
||
12 | class MessageContext extends RawDrupalContext implements TranslatableContext |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * {@inheritDoc} |
||
17 | */ |
||
18 | public static function getTranslationResources() |
||
22 | |||
23 | /** |
||
24 | * Checks if the current page contains the given error message |
||
25 | * |
||
26 | * @param $message |
||
27 | * string The text to be checked |
||
28 | * |
||
29 | * @Then I should see the error message( containing) :message |
||
30 | */ |
||
31 | public function assertErrorVisible($message) |
||
40 | |||
41 | /** |
||
42 | * Checks if the current page contains the given set of error messages |
||
43 | * |
||
44 | * @param array $messages |
||
45 | * An array of texts to be checked. The first row should consist of the |
||
46 | * string "Error messages". |
||
47 | * |
||
48 | * @Then I should see the following error message(s): |
||
49 | */ |
||
50 | View Code Duplication | public function assertMultipleErrors(TableNode $messages) |
|
59 | |||
60 | /** |
||
61 | * Checks if the current page does not contain the given error message |
||
62 | * |
||
63 | * @param $message |
||
64 | * string The text to be checked |
||
65 | * |
||
66 | * @Given I should not see the error message( containing) :message |
||
67 | */ |
||
68 | public function assertNotErrorVisible($message) |
||
76 | |||
77 | /** |
||
78 | * Checks if the current page does not contain the given set error messages |
||
79 | * |
||
80 | * @param array $messages |
||
81 | * An array of texts to be checked. The first row should consist of the |
||
82 | * string "Error messages". |
||
83 | * |
||
84 | * @Then I should not see the following error messages: |
||
85 | */ |
||
86 | View Code Duplication | public function assertNotMultipleErrors(TableNode $messages) |
|
95 | |||
96 | /** |
||
97 | * Checks if the current page contains the given success message |
||
98 | * |
||
99 | * @param $message |
||
100 | * string The text to be checked |
||
101 | * |
||
102 | * @Then I should see the success message( containing) :message |
||
103 | */ |
||
104 | public function assertSuccessMessage($message) |
||
113 | |||
114 | /** |
||
115 | * Checks if the current page contains the given set of success messages |
||
116 | * |
||
117 | * @param array $message |
||
118 | * An array of texts to be checked. The first row should consist of the |
||
119 | * string "Success messages". |
||
120 | * |
||
121 | * @Then I should see the following success messages: |
||
122 | */ |
||
123 | View Code Duplication | public function assertMultipleSuccessMessage(TableNode $messages) |
|
132 | |||
133 | /** |
||
134 | * Checks if the current page does not contain the given set of success message |
||
135 | * |
||
136 | * @param $message |
||
137 | * string The text to be checked |
||
138 | * |
||
139 | * @Given I should not see the success message( containing) :message |
||
140 | */ |
||
141 | public function assertNotSuccessMessage($message) |
||
149 | |||
150 | /** |
||
151 | * Checks if the current page does not contain the given set of success messages |
||
152 | * |
||
153 | * @param array $message |
||
154 | * An array of texts to be checked. The first row should consist of the |
||
155 | * string "Success messages". |
||
156 | * |
||
157 | * @Then I should not see the following success messages: |
||
158 | */ |
||
159 | View Code Duplication | public function assertNotMultipleSuccessMessage(TableNode $messages) |
|
168 | |||
169 | /** |
||
170 | * Checks if the current page contains the given warning message |
||
171 | * |
||
172 | * @param $message |
||
173 | * string The text to be checked |
||
174 | * |
||
175 | * @Then I should see the warning message( containing) :message |
||
176 | */ |
||
177 | public function assertWarningMessage($message) |
||
186 | |||
187 | /** |
||
188 | * Checks if the current page contains the given set of warning messages |
||
189 | * |
||
190 | * @param array $message |
||
191 | * An array of texts to be checked. The first row should consist of the |
||
192 | * string "Warning messages". |
||
193 | * |
||
194 | * @Then I should see the following warning messages: |
||
195 | */ |
||
196 | View Code Duplication | public function assertMultipleWarningMessage(TableNode $messages) |
|
205 | |||
206 | /** |
||
207 | * Checks if the current page does not contain the given set of warning message |
||
208 | * |
||
209 | * @param $message |
||
210 | * string The text to be checked |
||
211 | * |
||
212 | * @Given I should not see the warning message( containing) :message |
||
213 | */ |
||
214 | public function assertNotWarningMessage($message) |
||
222 | |||
223 | /** |
||
224 | * Checks if the current page does not contain the given set of warning messages |
||
225 | * |
||
226 | * @param array $message |
||
227 | * An array of texts to be checked. The first row should consist of the |
||
228 | * string "Warning messages". |
||
229 | * |
||
230 | * @Then I should not see the following warning messages: |
||
231 | */ |
||
232 | View Code Duplication | public function assertNotMultipleWarningMessage(TableNode $messages) |
|
241 | |||
242 | /** |
||
243 | * Checks if the current page contain the given message |
||
244 | * |
||
245 | * @param $message |
||
246 | * string The message to be checked |
||
247 | * |
||
248 | * @Then I should see the message( containing) :message |
||
249 | */ |
||
250 | public function assertMessage($message) |
||
259 | |||
260 | /** |
||
261 | * Checks if the current page does not contain the given message |
||
262 | * |
||
263 | * @param $message |
||
264 | * string The message to be checked |
||
265 | * |
||
266 | * @Then I should not see the message( containing) :message |
||
267 | */ |
||
268 | public function assertNotMessage($message) |
||
276 | |||
277 | /** |
||
278 | * Checks whether the given list of messages is valid. |
||
279 | * |
||
280 | * This checks whether the list has only one column and has the correct |
||
281 | * header. |
||
282 | * |
||
283 | * @param \Behat\Gherkin\Node\TableNode $messages |
||
284 | * The list of messages. |
||
285 | * @param string $expected_header |
||
286 | * The header that should be present in the list. |
||
287 | */ |
||
288 | protected function assertValidMessageTable(TableNode $messages, $expected_header) |
||
305 | |||
306 | /** |
||
307 | * Internal callback to check for a specific message in a given context. |
||
308 | * |
||
309 | * @param $message |
||
310 | * string The message to be checked |
||
311 | * @param $selectorId |
||
312 | * string CSS selector name |
||
313 | * @param $exceptionMsgNone |
||
314 | * string The message being thrown when no message is contained, string |
||
315 | * should contain one '%s' as a placeholder for the current URL |
||
316 | * @param $exceptionMsgMissing |
||
317 | * string The message being thrown when the message is not contained, string |
||
318 | * should contain two '%s' as placeholders for the current URL and the message. |
||
319 | * |
||
320 | * @throws \Behat\Mink\Exception\ExpectationException |
||
321 | * Thrown when the expected message is not present in the page. |
||
322 | */ |
||
323 | private function assert($message, $selectorId, $exceptionMsgNone, $exceptionMsgMissing) |
||
337 | |||
338 | /** |
||
339 | * Internal callback to check if the current page does not contain the given message |
||
340 | * |
||
341 | * @param $message |
||
342 | * string The message to be checked |
||
343 | * @param $selectorId |
||
344 | * string CSS selector name |
||
345 | * @param $exceptionMsg |
||
346 | * string The message being thrown when the message is contained, string |
||
347 | * should contain two '%s' as placeholders for the current URL and the message. |
||
348 | * |
||
349 | * @throws \Behat\Mink\Exception\ExpectationException |
||
350 | * Thrown when the expected message is present in the page. |
||
351 | */ |
||
352 | private function assertNot($message, $selectorId, $exceptionMsg) |
||
364 | } |
||
365 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.