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 FixtureContext extends BaseFixtureContext |
||
16 | { |
||
17 | use StepHelper; |
||
18 | |||
19 | /** |
||
20 | * Select a gallery item by type and name |
||
21 | * |
||
22 | * @Given /^I (?:(?:click on)|(?:select)) the file named "([^"]+)" in the gallery$/ |
||
23 | * @param string $name |
||
24 | */ |
||
25 | public function stepISelectGalleryItem($name) |
||
31 | |||
32 | /** |
||
33 | * Check the checkbox for a given gallery item |
||
34 | * @Given /^I check the file named "([^"]+)" in the gallery$/ |
||
35 | * @param string $name |
||
36 | */ |
||
37 | public function stepICheckTheGalleryItem($name) |
||
45 | |||
46 | /** |
||
47 | * @Then /^I should see the file named "([^"]+)" in the gallery$/ |
||
48 | * @param string $name |
||
49 | */ |
||
50 | public function iShouldSeeTheGalleryItem($name) |
||
55 | |||
56 | /** |
||
57 | * @Then /^I should not see the file named "([^"]+)" in the gallery$/ |
||
58 | * @param string $name |
||
59 | */ |
||
60 | public function iShouldNotSeeTheGalleryItem($name) |
||
65 | |||
66 | /** |
||
67 | * @Then /^I should see the "([^"]*)" form$/ |
||
68 | * @param string $id HTML ID of form |
||
69 | */ |
||
70 | View Code Duplication | public function iShouldSeeTheForm($id) |
|
|
|||
71 | { |
||
72 | /** @var DocumentElement $page */ |
||
73 | $page = $this->getMainContext()->getSession()->getPage(); |
||
74 | $form = $page->find('css', "form#{$id}"); |
||
75 | assertNotNull($form, "form with id $id could not be found"); |
||
76 | assertTrue($form->isVisible(), "form with id $id is not visible"); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @Then /^I should see the file status flag$/ |
||
81 | */ |
||
82 | View Code Duplication | public function iShouldSeeTheFileStatusFlag() |
|
83 | { |
||
84 | $page = $this->getMainContext()->getSession()->getPage(); |
||
85 | $flag = $page->find('css', '.editor__status-flag'); |
||
86 | assertNotNull($flag, "File editor status flag could not be found"); |
||
87 | assertTrue($flag->isVisible(), "File status flag is not visible"); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @Then /^I should not see the file status flag$/ |
||
92 | */ |
||
93 | public function iShouldNotSeeTheFileStatusFlag() |
||
99 | |||
100 | /** |
||
101 | * @Given /^I click on the latest history item$/ |
||
102 | */ |
||
103 | public function iClickOnTheLatestHistoryItem() |
||
120 | |||
121 | /** |
||
122 | * @Given /^I attach the file "([^"]*)" to dropzone "([^"]*)"$/ |
||
123 | * @see MinkContext::attachFileToField() |
||
124 | * @param string $path |
||
125 | * @param string $name |
||
126 | */ |
||
127 | public function iAttachTheFileToDropzone($path, $name) |
||
161 | |||
162 | /** |
||
163 | * Checks that the message box contains specified text. |
||
164 | * |
||
165 | * @Then /^I should see "(?P<text>(?:[^"]|\\")*)" in the message box$/ |
||
166 | * @param string $text |
||
167 | */ |
||
168 | public function assertMessageBoxContainsText($text) |
||
176 | |||
177 | /** |
||
178 | * Helper for finding items in the visible gallery view |
||
179 | * |
||
180 | * @param string $name Title of item |
||
181 | * @param int $timeout |
||
182 | * @return NodeElement |
||
183 | */ |
||
184 | protected function getGalleryItem($name, $timeout = 3) |
||
206 | |||
207 | /** |
||
208 | * @Given /^a page "([^"]*)" containing an image "([^"]*)"$/ |
||
209 | * @param string $page |
||
210 | * @param string $image |
||
211 | */ |
||
212 | public function aPageContaining($page, $image) |
||
232 | } |
||
233 |
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.