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 |
||
7 | class Select2Context extends BaseContext |
||
8 | { |
||
9 | /** |
||
10 | * Fills in Select2 field with specified |
||
11 | * |
||
12 | * @When /^(?:|I )fill in select2 "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
||
13 | * @When /^(?:|I )fill in select2 "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/ |
||
14 | */ |
||
15 | public function iFillInSelect2Field($field, $value) |
||
22 | |||
23 | /** |
||
24 | * Fill Select2 input field |
||
25 | * |
||
26 | * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
||
27 | */ |
||
28 | public function iFillInSelect2InputWith($field, $value) |
||
35 | |||
36 | /** |
||
37 | * Fill Select2 input field and select a value |
||
38 | * |
||
39 | * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/ |
||
40 | */ |
||
41 | public function iFillInSelect2InputWithAndSelect($field, $value, $entry) |
||
49 | |||
50 | /** |
||
51 | * Fill Select2 input field |
||
52 | * |
||
53 | * @Then /^(?:|I )should see (?P<num>\d+) choice(?:|s) in select2 "(?P<field>(?:[^"]|\\")*)"$/ |
||
54 | */ |
||
55 | public function iShouldSeeSelectChoices($field, $num) |
||
61 | |||
62 | /** |
||
63 | * Open Select2 choice list |
||
64 | * |
||
65 | * @param DocumentElement $page |
||
66 | * @param string $field |
||
67 | * @throws \Exception |
||
68 | */ |
||
69 | View Code Duplication | private function openField(DocumentElement $page, $field) |
|
84 | |||
85 | /** |
||
86 | * Fill Select2 search field |
||
87 | * |
||
88 | * @param DocumentElement $page |
||
89 | * @param string $field |
||
90 | * @param string $value |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | private function fillSearchField(DocumentElement $page, $field, $value) |
||
113 | |||
114 | /** |
||
115 | * Select value in choice list |
||
116 | * |
||
117 | * @param DocumentElement $page |
||
118 | * @param string $field |
||
119 | * @param string $value |
||
120 | * @throws \Exception |
||
121 | */ |
||
122 | private function selectValue(DocumentElement $page, $field, $value) |
||
136 | |||
137 | /** |
||
138 | * Wait the end of fetching Select2 results |
||
139 | * |
||
140 | * @param int $time Time to wait in seconds |
||
141 | */ |
||
142 | private function waitForLoadingResults($time = 60) |
||
154 | } |
||
155 |
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.