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 | class FeatureContext extends OroFeatureContext implements OroElementFactoryAware |
||
13 | { |
||
14 | use ElementFactoryDictionary; |
||
15 | |||
16 | /** |
||
17 | * Assert that value of given field is a primary. |
||
18 | * In frontend primary value is marked as bold. |
||
19 | * Also primary value is value that showing in grid |
||
20 | * Example: And Phone "+1 415-731-9375" should be primary |
||
21 | * Example: And email "[email protected]" should be primary |
||
22 | * |
||
23 | * @Then /^(?P<field>[^"]+) "(?P<value>[^"]+)" should be primary$/ |
||
24 | */ |
||
25 | public function fieldValueShouldBePrimary($field, $value) |
||
47 | |||
48 | /** |
||
49 | * Click edit icon (pencil) into address at entity view page |
||
50 | * Example: And click edit LOS ANGELES address |
||
51 | * |
||
52 | * @Given /^click edit (?P<address>[^"]+) address$/ |
||
53 | */ |
||
54 | View Code Duplication | public function clickEditAddress($address) |
|
69 | |||
70 | /** |
||
71 | * Delete address form entity view page by clicking on trash icon given address |
||
72 | * Example: When I delete Ukraine address |
||
73 | * |
||
74 | * @When /^(?:|I )delete (?P<address>[^"]+) address$/ |
||
75 | */ |
||
76 | public function iDeleteAddress($address) |
||
98 | |||
99 | /** |
||
100 | * Assert that entity view page has default avatar (info-user.png) |
||
101 | * |
||
102 | * @Then avatar should be default avatar |
||
103 | */ |
||
104 | public function avatarShouldBeDefaultAvatar() |
||
110 | |||
111 | /** |
||
112 | * Assert avatar image src, e.g. charlie-sheen.jpg |
||
113 | * Example: And avatar should be "charlie-sheen.jpg" |
||
114 | * |
||
115 | * @Then avatar should be :arg1 |
||
116 | */ |
||
117 | public function avatarShouldBe($imgName) |
||
123 | |||
124 | /** |
||
125 | * Assert that two accounts sets at view entity page |
||
126 | * Example: And Warner Brothers and Columbia Pictures should be set as accounts |
||
127 | * |
||
128 | * @Then /^(?P<acc1>[^"]+) and (?P<acc2>[^"]+) should be set as accounts$/ |
||
129 | */ |
||
130 | public function assertAccountsNames($acc1, $acc2) |
||
147 | |||
148 | /** |
||
149 | * Assert social links |
||
150 | * Example: And should see next social links: |
||
151 | * | Twitter | https://twitter.com/charliesheen | |
||
152 | * | Facebook | https://www.facebook.com/CharlieSheen | |
||
153 | * | Google+ | https://profiles.google.com/111536551725236448567 | |
||
154 | * | LinkedIn | http://www.linkedin.com/in/charlie-sheen-74755931 | |
||
155 | * |
||
156 | * @Then should see next social links: |
||
157 | */ |
||
158 | public function shouldSeeNextSocialLinks(TableNode $table) |
||
185 | |||
186 | /** |
||
187 | * Assert count of addresses in entity view page |
||
188 | * Example: And contact has 2 addresses |
||
189 | * Example: Then contact has one address |
||
190 | * Example: And two addresses should be in page |
||
191 | * |
||
192 | * @Then :count addresses should be in page |
||
193 | * @Then /^(.*) has (?P<count>(one|two|[\d]+)) address(?:|es)$/ |
||
194 | */ |
||
195 | public function assertAddressCount($count) |
||
205 | |||
206 | /** |
||
207 | * Assert that given address is a primary address. |
||
208 | * Be aware that you can't delete primary address. |
||
209 | * Example: Then LOS ANGELES address must be primary |
||
210 | * |
||
211 | * @Then /^(?P<address>[^"]+) address must be primary$/ |
||
212 | */ |
||
213 | public function assertPrimaryAddress($address) |
||
232 | |||
233 | /** |
||
234 | * Delete all elements in collection field |
||
235 | * Example: And I delete all addresses |
||
236 | * |
||
237 | * @Given /^(?:|I )delete all (?P<field>[^"]+)$/ |
||
238 | */ |
||
239 | public function iDeleteAllAddresses($field) |
||
248 | |||
249 | /** |
||
250 | * @param int|string $count |
||
251 | * @return int |
||
252 | */ |
||
253 | View Code Duplication | protected function getCount($count) |
|
266 | } |
||
267 |
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.