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 |
||
13 | final class ManagingCustomBlocksContext implements Context |
||
14 | { |
||
15 | /** |
||
16 | * @var IndexPageInterface |
||
17 | */ |
||
18 | private $indexPage; |
||
19 | |||
20 | /** |
||
21 | * @var CreatePageInterface |
||
22 | */ |
||
23 | private $createPage; |
||
24 | |||
25 | /** |
||
26 | * @var UpdatePageInterface |
||
27 | */ |
||
28 | private $updatePage; |
||
29 | |||
30 | /** |
||
31 | * @var ShowPageInterface |
||
32 | */ |
||
33 | private $showPage; |
||
34 | |||
35 | /** |
||
36 | * @param IndexPageInterface $indexPage |
||
37 | * @param CreatePageInterface $createPage |
||
38 | * @param UpdatePageInterface $updatePage |
||
39 | * @param ShowPageInterface $showPage |
||
40 | */ |
||
41 | public function __construct( |
||
52 | |||
53 | /** |
||
54 | * @Given I want to create a new custom block |
||
55 | * @Given I want to add a new custom block |
||
56 | */ |
||
57 | public function iWantToCreateNewCustomBlock() |
||
61 | |||
62 | /** |
||
63 | * @Given I browse custom blocks of the store |
||
64 | */ |
||
65 | public function iWantToBrowseCustomBlocksOfTheStore() |
||
69 | |||
70 | /** |
||
71 | * @When I set its body to :body |
||
72 | */ |
||
73 | public function iSetItsBodyTo($body) |
||
77 | |||
78 | /** |
||
79 | * @When I set its link to :link |
||
80 | */ |
||
81 | public function iSetItsLinkTo($link) |
||
85 | |||
86 | /** |
||
87 | * @When I set its name to :name |
||
88 | */ |
||
89 | public function iSetItsNameTo($name) |
||
93 | |||
94 | /** |
||
95 | * @When I set its title to :title |
||
96 | */ |
||
97 | public function iSetItsTitleTo($title) |
||
101 | |||
102 | /** |
||
103 | * @When I attach :imagePath as its image |
||
104 | */ |
||
105 | public function iAttachAsItsImage($imagePath) |
||
109 | |||
110 | /** |
||
111 | * @When I add it |
||
112 | * @When I try to add it |
||
113 | */ |
||
114 | public function iAddIt() |
||
118 | |||
119 | /** |
||
120 | * @Then /^I should be notified that (body|name) is required$/ |
||
121 | */ |
||
122 | public function iShouldBeNotifiedThatElementIsRequired($element) |
||
129 | |||
130 | /** |
||
131 | * @Then the custom block :name should appear in the store |
||
132 | * @Then I should see the custom block :name in the list |
||
133 | */ |
||
134 | View Code Duplication | public function theCustomBlockShouldAppearInTheStore($name) |
|
145 | |||
146 | /** |
||
147 | * @Then I should see :amount custom blocks in the list |
||
148 | */ |
||
149 | public function iShouldSeeThatManyCustomBlocksInTheList($amount) |
||
157 | |||
158 | /** |
||
159 | * @Then the custom block :name should not be added |
||
160 | */ |
||
161 | View Code Duplication | public function theCustomBlockShouldNotBeAdded($name) |
|
172 | |||
173 | /** |
||
174 | * @When /^I preview (this custom block)$/ |
||
175 | */ |
||
176 | public function iPreviewCustomBlock(CustomBlock $customBlock) |
||
180 | |||
181 | /** |
||
182 | * @Given /^I want to edit (this custom block)$/ |
||
183 | */ |
||
184 | public function iWantToEditThisCustomBlock(CustomBlock $customBlock) |
||
188 | |||
189 | /** |
||
190 | * @When I change its body to :body |
||
191 | */ |
||
192 | public function iChangeItsBodyTo($body) |
||
196 | |||
197 | /** |
||
198 | * @When I change its link to :link |
||
199 | */ |
||
200 | public function iChangeItsLinkTo($link) |
||
204 | |||
205 | /** |
||
206 | * @When I change its title to :title |
||
207 | */ |
||
208 | public function iChangeItsTitleTo($title) |
||
212 | |||
213 | /** |
||
214 | * @When I save my changes |
||
215 | * @When I try to save my changes |
||
216 | */ |
||
217 | public function iSaveMyChanges() |
||
221 | |||
222 | /** |
||
223 | * @When I delete custom block :name |
||
224 | */ |
||
225 | public function iDeleteCustomBlock($name) |
||
230 | |||
231 | /** |
||
232 | * @Then I should see :expected in this block contents |
||
233 | */ |
||
234 | public function iShouldSeeInThisBlockContents($expected) |
||
238 | |||
239 | /** |
||
240 | * @Then /^(this custom block) should have body "([^"]+)"$/ |
||
241 | */ |
||
242 | public function thisCustomBlockShouldHaveBody(CustomBlock $customBlock, $body) |
||
248 | |||
249 | /** |
||
250 | * @Then /^(this custom block) should have link "([^"]+)"$/ |
||
251 | */ |
||
252 | public function thisCustomBlockShouldHaveLink(CustomBlock $customBlock, $link) |
||
258 | |||
259 | /** |
||
260 | * @Then /^(this custom block) should have title "([^"]+)"$/ |
||
261 | */ |
||
262 | public function thisCustomBlockShouldHaveTitle(CustomBlock $customBlock, $title) |
||
268 | |||
269 | /** |
||
270 | * @Then the custom block :name should no longer exist in the store |
||
271 | */ |
||
272 | public function theCustomBlockShouldNoLongerExistInTheStore($name) |
||
279 | } |
||
280 |
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.