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 |
||
| 15 | class UtilityContext extends DrupalSubContextBase { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Asserts that a form field is not present. |
||
| 19 | * |
||
| 20 | * @param string $field |
||
| 21 | * The field locator. |
||
| 22 | * |
||
| 23 | * @Then I should not see a :field field |
||
| 24 | */ |
||
| 25 | public function assertFieldNotExists($field) { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Asserts that a certain number of elements match a CSS selector. |
||
| 31 | * |
||
| 32 | * @param string $selector |
||
| 33 | * The selector. |
||
| 34 | * @param int $count |
||
| 35 | * The number of elements expected to match the selector. |
||
| 36 | * |
||
| 37 | * @throws ExpectationException |
||
| 38 | * If the number of elements that match the selector is not the expected |
||
| 39 | * number. |
||
| 40 | * |
||
| 41 | * @Then :count element(s) should match :selector |
||
| 42 | */ |
||
| 43 | View Code Duplication | public function assertSelectorMatch($selector, $count) { |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Asserts that a minimum number of elements match a CSS selector. |
||
| 58 | * |
||
| 59 | * @param string $selector |
||
| 60 | * The selector. |
||
| 61 | * @param int $count |
||
| 62 | * The number of elements expected to match the selector. |
||
| 63 | * |
||
| 64 | * @throws ExpectationException |
||
| 65 | * If the number of elements that match the selector is less than expected. |
||
| 66 | * |
||
| 67 | * @Then at least :count element(s) should match :selector |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function assertSelectorMatchAtLeast($selector, $count) { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Asserts that no elements match a CSS selector. |
||
| 84 | * |
||
| 85 | * @param string $selector |
||
| 86 | * The selector. |
||
| 87 | * |
||
| 88 | * @Then no elements should match :selector |
||
| 89 | * @Then nothing should match :selector |
||
| 90 | */ |
||
| 91 | public function assertSelectorMatchNothing($selector) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Asserts than an element is empty. |
||
| 97 | * |
||
| 98 | * @param string $selector |
||
| 99 | * The element's CSS selector. |
||
| 100 | * |
||
| 101 | * @throws ExpectationException |
||
| 102 | * If the element has any HTML content. |
||
| 103 | * |
||
| 104 | * @Then the :selector element(s) should be empty |
||
| 105 | */ |
||
| 106 | public function assertElementIsEmpty($selector) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Clears a field. |
||
| 119 | * |
||
| 120 | * @param string $field |
||
| 121 | * The field to clear. |
||
| 122 | * @param ElementInterface $container |
||
| 123 | * (optional) The containing element. |
||
| 124 | * |
||
| 125 | * @When I clear :field |
||
| 126 | */ |
||
| 127 | public function clearField($field, ElementInterface $container = NULL) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Finds a collapsible details element by its summary text. |
||
| 133 | * |
||
| 134 | * @param string $summary_text |
||
| 135 | * The summary text. |
||
| 136 | * |
||
| 137 | * @return \Behat\Mink\Element\NodeElement|null |
||
| 138 | * The details element, or NULL if it was not found. |
||
| 139 | */ |
||
| 140 | public function findCollapsible($summary_text) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Generates random image media assets. |
||
| 156 | * |
||
| 157 | * @param int $count |
||
| 158 | * (optional) How many to generate. Defaults to 1. |
||
| 159 | * |
||
| 160 | * @Given a random image |
||
| 161 | * @Given :count random images |
||
| 162 | */ |
||
| 163 | public function generateRandomImages($count = 1) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Accepts any currently open alert box(es), then optionally runs a callback. |
||
| 189 | * |
||
| 190 | * @param callable $then |
||
| 191 | * (optional) A function to run after accepting the alerts. |
||
| 192 | * @param mixed[] $arguments |
||
| 193 | * (optional) Arguments for the callback. |
||
| 194 | * |
||
| 195 | * @When I accept the alert(s) |
||
| 196 | */ |
||
| 197 | public function acceptAlerts(callable $then = NULL, array $arguments = []) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * "Manually" writes text into a field. |
||
| 216 | * |
||
| 217 | * @param string $text |
||
| 218 | * The text to write into the field. |
||
| 219 | * @param string $field |
||
| 220 | * The label, placeholder, ID or name of the field. |
||
| 221 | * |
||
| 222 | * @Given I write :text into :field |
||
| 223 | */ |
||
| 224 | public function iWriteTextIntoField($text, $field) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Asserts that the current page is an admin page. |
||
| 234 | * |
||
| 235 | * @Then The page should be an admin page |
||
| 236 | */ |
||
| 237 | public function assertAdminPage() { |
||
| 242 | |||
| 243 | } |
||
| 244 |
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.