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 IndexPage extends Authenticated |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @param TestableInput[] $inputs example: |
||
| 15 | * ```php |
||
| 16 | * [ |
||
| 17 | * Input::asAdvancedSearch(Tester, 'Name'), |
||
| 18 | * Select2::asAdvancedSearch(Tester, 'Status') |
||
| 19 | * ] |
||
| 20 | *``` |
||
| 21 | */ |
||
| 22 | public function containsFilters(array $inputs): void |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param string[] $list array of legend list |
||
| 37 | */ |
||
| 38 | public function containsLegend(array $list): void |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string[] $buttons array of buttons |
||
| 51 | */ |
||
| 52 | public function containsBulkButtons(array $buttons): void |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string[] $columnNames array of column names |
||
| 63 | * @param string|null $representation the representation name |
||
| 64 | * @throws \Codeception\Exception\ModuleException |
||
| 65 | */ |
||
| 66 | public function containsColumns(array $columnNames, $representation = null): void |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param int $amount |
||
| 85 | */ |
||
| 86 | public function containsAmountOfRows(int $amount): void |
||
| 90 | |||
| 91 | public function getRowDataKeyByNumber(int $rowNumber): string |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Filters index page table |
||
| 100 | * |
||
| 101 | * @param TestableInput $inputElement |
||
| 102 | * @param $value |
||
| 103 | * @throws \Codeception\Exception\ModuleException |
||
| 104 | */ |
||
| 105 | public function filterBy(TestableInput $inputElement, string $value): void |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Selects table row by its number |
||
| 117 | * |
||
| 118 | * @param int $n - number of the row that should be selected |
||
| 119 | */ |
||
| 120 | public function selectTableRowByNumber(int $n): void |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Opens table row menu by its number |
||
| 130 | * |
||
| 131 | * @param int $n - number of the row which menu should be opened |
||
| 132 | */ |
||
| 133 | public function openRowMenuByNumber(int $n): void |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Opens table row menu by item id |
||
| 140 | * |
||
| 141 | * @param string $id - id of item which menu should be opened |
||
| 142 | */ |
||
| 143 | public function openRowMenuById(string $id): void |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Clicks to row menu option |
||
| 150 | * |
||
| 151 | * @param $option - the name of option that should be clicked |
||
| 152 | * @throws \Codeception\Exception\ModuleException |
||
| 153 | */ |
||
| 154 | public function chooseRowMenuOption(string $option): void |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Parse tbody, count td and return result |
||
| 162 | * |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | public function countRowsInTableBody(): int |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Checks whether filtering works properly |
||
| 172 | * |
||
| 173 | * @param string $filterBy |
||
| 174 | * @param string $name |
||
| 175 | * @throws \Codeception\Exception\ModuleException |
||
| 176 | */ |
||
| 177 | public function checkFilterBy(string $filterBy, string $name): void |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Checks whether sorting works properly |
||
| 188 | * |
||
| 189 | * Method find column by $sortBy, parse data, call default sort by $sortBy |
||
| 190 | * and compare data in table with sort(copy_data_from_table) |
||
| 191 | * |
||
| 192 | * @param string $sortBy |
||
| 193 | * @throws \Codeception\Exception\ModuleException |
||
| 194 | */ |
||
| 195 | public function checkSortingBy(string $sortBy): void |
||
| 225 | } |
||
| 226 |
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.