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 |
||
12 | final class ManagingRoutesContext implements Context |
||
13 | { |
||
14 | /** |
||
15 | * @var IndexPageInterface |
||
16 | */ |
||
17 | private $indexPage; |
||
18 | |||
19 | /** |
||
20 | * @var CreatePageInterface |
||
21 | */ |
||
22 | private $createPage; |
||
23 | |||
24 | /** |
||
25 | * @var UpdatePageInterface |
||
26 | */ |
||
27 | private $updatePage; |
||
28 | |||
29 | /** |
||
30 | * @param IndexPageInterface $indexPage |
||
31 | * @param CreatePageInterface $createPage |
||
32 | * @param UpdatePageInterface $updatePage |
||
33 | */ |
||
34 | public function __construct( |
||
43 | |||
44 | /** |
||
45 | * @Given I want to create a new route |
||
46 | * @Given I want to add a new route |
||
47 | */ |
||
48 | public function iWantToCreateNewRoute() |
||
52 | |||
53 | /** |
||
54 | * @When I want to browse routes of the store |
||
55 | */ |
||
56 | public function iWantToBrowseRoutesOfTheStore() |
||
60 | |||
61 | /** |
||
62 | * @When I set its name to :name |
||
63 | */ |
||
64 | public function iSetItsNameTo($name) |
||
68 | |||
69 | /** |
||
70 | * @When I choose :title as its content |
||
71 | */ |
||
72 | public function iChooseContent($title) |
||
76 | |||
77 | /** |
||
78 | * @When I add it |
||
79 | * @When I try to add it |
||
80 | */ |
||
81 | public function iAddIt() |
||
85 | |||
86 | /** |
||
87 | * @Then the route :name should appear in the store |
||
88 | * @Then I should see the route :name in the list |
||
89 | */ |
||
90 | View Code Duplication | public function theRouteShouldAppearInTheStore($name) |
|
101 | |||
102 | /** |
||
103 | * @Then I should see :amount routes in the list |
||
104 | */ |
||
105 | public function iShouldSeeThatManyRoutesInTheList($amount) |
||
113 | |||
114 | /** |
||
115 | * @When I delete route :name |
||
116 | */ |
||
117 | public function iDeleteRoute($name) |
||
122 | |||
123 | /** |
||
124 | * @Given the route :name should no longer exist in the store |
||
125 | */ |
||
126 | public function theRouteShouldNoLongerExistInTheStore($name) |
||
133 | |||
134 | /** |
||
135 | * @Given /^I want to edit (this route)$/ |
||
136 | */ |
||
137 | public function iWantToEditThisRoute(Route $route) |
||
141 | |||
142 | /** |
||
143 | * @When I choose :title as its new content |
||
144 | */ |
||
145 | public function iChooseNewContent($title) |
||
149 | |||
150 | /** |
||
151 | * @When I save my changes |
||
152 | * @When I try to save my changes |
||
153 | */ |
||
154 | public function iSaveMyChanges() |
||
158 | |||
159 | /** |
||
160 | * @Then /^(this route) should have assigned "([^"]+)" content$/ |
||
161 | */ |
||
162 | public function thisRouteShouldHaveAssignedContent(Route $route, $contentTitle) |
||
173 | |||
174 | /** |
||
175 | * @Then I should be notified that name is required |
||
176 | */ |
||
177 | public function iShouldBeNotifiedThatElementIsRequired() |
||
184 | |||
185 | /** |
||
186 | * @Then the route with content :title should not be added |
||
187 | */ |
||
188 | View Code Duplication | public function theRouteWithContentShouldNotBeAdded($title) |
|
199 | } |
||
200 |
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.