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 | const DEFAULT_TIMEOUT = 60; |
||
10 | |||
11 | /** @var int */ |
||
12 | private $timeout; |
||
13 | |||
14 | /** |
||
15 | * Constructor |
||
16 | * |
||
17 | * @param int $timeout Timeout for waiting results (in seconds) |
||
18 | */ |
||
19 | public function __construct($timeout = self::DEFAULT_TIMEOUT) |
||
23 | |||
24 | /** |
||
25 | * Fills in Select2 field with specified |
||
26 | * |
||
27 | * @When /^(?:|I )fill in select2 "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
||
28 | * @When /^(?:|I )fill in select2 "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/ |
||
29 | */ |
||
30 | public function iFillInSelect2Field($field, $value) |
||
37 | |||
38 | /** |
||
39 | * Fills in Select2 field with specified and wait for results |
||
40 | * |
||
41 | * @When /^(?:|I )fill in select2 "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and wait (?P<time>(?:[^"]|\\")*) seconds until results are loaded$/ |
||
42 | * @When /^(?:|I )fill in select2 "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)" and wait (?P<time>(?:[^"]|\\")*) seconds until results are loaded$/ |
||
43 | */ |
||
44 | public function iFillInSelect2FieldWaitUntilResultsAreLoaded($field, $value, $time) |
||
51 | |||
52 | /** |
||
53 | * Fill Select2 input field |
||
54 | * |
||
55 | * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
||
56 | */ |
||
57 | public function iFillInSelect2InputWith($field, $value) |
||
64 | |||
65 | /** |
||
66 | * Fill Select2 input field and select a value |
||
67 | * |
||
68 | * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/ |
||
69 | */ |
||
70 | public function iFillInSelect2InputWithAndSelect($field, $value, $entry) |
||
78 | |||
79 | /** |
||
80 | * Fill Select2 input field |
||
81 | * |
||
82 | * @Then /^(?:|I )should see (?P<num>\d+) choice(?:|s) in select2 "(?P<field>(?:[^"]|\\")*)"$/ |
||
83 | */ |
||
84 | public function iShouldSeeSelectChoices($field, $num) |
||
90 | |||
91 | /** |
||
92 | * Open Select2 choice list |
||
93 | * |
||
94 | * @param DocumentElement $page |
||
95 | * @param string $field |
||
96 | * @throws \Exception |
||
97 | */ |
||
98 | View Code Duplication | private function openField(DocumentElement $page, $field) |
|
116 | |||
117 | /** |
||
118 | * Fill Select2 search field |
||
119 | * |
||
120 | * @param DocumentElement $page |
||
121 | * @param string $field |
||
122 | * @param string $value |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | private function fillSearchField(DocumentElement $page, $field, $value) |
||
145 | |||
146 | /** |
||
147 | * Select value in choice list |
||
148 | * |
||
149 | * @param DocumentElement $page |
||
150 | * @param string $field |
||
151 | * @param string $value |
||
152 | * @param int $time |
||
153 | * @throws \Exception |
||
154 | */ |
||
155 | private function selectValue(DocumentElement $page, $field, $value, $time) |
||
169 | |||
170 | /** |
||
171 | * Wait the end of fetching Select2 results |
||
172 | * |
||
173 | * @param int $time Time to wait in seconds |
||
174 | */ |
||
175 | private function waitForLoadingResults($time) |
||
185 | } |
||
186 |
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.