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 CkEditorContext extends DrupalSubContextBase { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Asserts that a CKEditor instance exists and is fully loaded. |
||
| 16 | * |
||
| 17 | * @param string $id |
||
| 18 | * (optional) The editor instance ID. Defaults to the first available |
||
| 19 | * instance. |
||
| 20 | * |
||
| 21 | * @return string |
||
| 22 | * A snippet of JavaScript for calling instance methods. |
||
| 23 | * |
||
| 24 | * @Given CKEditor :id exists |
||
| 25 | * |
||
| 26 | * @Then CKEditor :id should exist |
||
| 27 | */ |
||
| 28 | public function assertEditor($id = NULL) { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Puts text or HTML into a CKEditor instance. |
||
| 38 | * |
||
| 39 | * @param string $text |
||
| 40 | * The text (or HTML) to insert into the editor. |
||
| 41 | * @param string $id |
||
| 42 | * (optional) The editor instance ID. |
||
| 43 | * |
||
| 44 | * @When I put :text into CKEditor |
||
| 45 | * @When I put :text into CKEditor :id |
||
| 46 | */ |
||
| 47 | public function insert($text, $id = NULL) { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Asserts that a CKEditor's content contains a snippet of text. |
||
| 54 | * |
||
| 55 | * @param string $text |
||
| 56 | * The text (or HTML) snippet to look for. |
||
| 57 | * @param string $id |
||
| 58 | * (optional) The editor instance ID. |
||
| 59 | * |
||
| 60 | * @throws ExpectationException |
||
| 61 | * If the editor does not contain the specified text. |
||
| 62 | * |
||
| 63 | * @Then CKEditor should contain :text |
||
| 64 | * @Then CKEditor :id should contain :text |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function assertEditorContains($text, $id = NULL) { |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Assert that a CKEditor's content matches a regular expression. |
||
| 79 | * |
||
| 80 | * @param string $expr |
||
| 81 | * The regular expression to match. |
||
| 82 | * @param string $id |
||
| 83 | * (optional) The editor instance ID. |
||
| 84 | * |
||
| 85 | * @throws ExpectationException |
||
| 86 | * If the expression does not match. |
||
| 87 | * |
||
| 88 | * @Then CKEditor should match :expression |
||
| 89 | * @Then CKEditor :id should match :expression |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function assertEditorMatch($expr, $id = NULL) { |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Gets the content of a CKEditor instance. |
||
| 104 | * |
||
| 105 | * @param string $id |
||
| 106 | * (optional) The editor instance ID. |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | * The HTML content of the editor. |
||
| 110 | */ |
||
| 111 | protected function getContent($id = NULL) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Executes a CKEditor command. |
||
| 118 | * |
||
| 119 | * @param string $command |
||
| 120 | * The command ID, as known to CKEditor's API. |
||
| 121 | * @param string $id |
||
| 122 | * (optional) The editor instance ID. |
||
| 123 | * @param mixed $data |
||
| 124 | * Additional data to pass to the executed command. |
||
| 125 | * |
||
| 126 | * @throws ExpectationException |
||
| 127 | * If the command cannot be executed (i.e., returns a falsy value). |
||
| 128 | * |
||
| 129 | * @When I execute the :command command in CKEditor |
||
| 130 | * @When I execute the :command command in CKEditor :id |
||
| 131 | */ |
||
| 132 | public function execute($command, $id = NULL, $data = NULL) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the first available CKEditor instance ID. |
||
| 149 | * |
||
| 150 | * @return string|false |
||
| 151 | * The first CKEditor instance ID, or FALSE if there are no instances. |
||
| 152 | */ |
||
| 153 | protected function getDefault() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns all CKEditor instance IDs. |
||
| 160 | * |
||
| 161 | * @return string[] |
||
| 162 | * The CKEditor instance IDs. |
||
| 163 | */ |
||
| 164 | protected function getKeys() { |
||
| 171 | |||
| 172 | } |
||
| 173 |
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.