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 |
||
14 | trait Generic { |
||
15 | |||
16 | /** |
||
17 | * Default screen size. |
||
18 | */ |
||
19 | protected $defaultScreenSize = ['width' => 1024, 'height' => 768]; |
||
20 | |||
21 | /** |
||
22 | * Screen size in use. |
||
23 | */ |
||
24 | protected $screenSize = ['width' => 1024, 'height' => 768]; |
||
25 | |||
26 | /** |
||
27 | * Assert that given user can perform given operation on given content. |
||
28 | * |
||
29 | * @param string $name |
||
30 | * User name. |
||
31 | * @param string $op |
||
32 | * Operation: view, edit or delete. |
||
33 | * @param string $title |
||
34 | * Content title. |
||
35 | * |
||
36 | * @throws \Exception |
||
37 | * If user cannot perform given operation on given content. |
||
38 | * |
||
39 | * @Then :name can :op content :content |
||
40 | */ |
||
41 | View Code Duplication | public function userCanContent($name, $op, $title) { |
|
57 | |||
58 | /** |
||
59 | * Assert that given user cannot perform given operation on given content. |
||
60 | * |
||
61 | * @param string $name |
||
62 | * User name. |
||
63 | * @param string $op |
||
64 | * Operation: view, edit or delete. |
||
65 | * @param string $title |
||
66 | * Content title. |
||
67 | * |
||
68 | * @throws \Exception |
||
69 | * If user can perform given operation on given content. |
||
70 | * |
||
71 | * @Then :name can not :op content :content |
||
72 | * @Then :name cannot :op content :content |
||
73 | */ |
||
74 | View Code Duplication | public function userCanNotContent($name, $op, $title) { |
|
89 | |||
90 | /** |
||
91 | * Assert presence of content edit link given its name and content title. |
||
92 | * |
||
93 | * @param string $link |
||
94 | * Link "name" HTML attribute. |
||
95 | * @param string $title |
||
96 | * Content title. |
||
97 | * |
||
98 | * @throws \Behat\Mink\Exception\ExpectationException |
||
99 | * If no edit link for given content has been found. |
||
100 | * |
||
101 | * @Then I should see the link :link to edit content :content |
||
102 | */ |
||
103 | public function assertContentEditLink($link, $title) { |
||
108 | |||
109 | /** |
||
110 | * Assert absence of content edit link given its content title. |
||
111 | * |
||
112 | * @param string $title |
||
113 | * Content title. |
||
114 | * |
||
115 | * @throws \Behat\Mink\Exception\ExpectationException |
||
116 | * If edit link for given content has been found. |
||
117 | * |
||
118 | * @Then I should not see a link to edit content :content |
||
119 | */ |
||
120 | public function assertNoContentEditLink($title) { |
||
125 | |||
126 | /** |
||
127 | * Assert string in HTTP response header. |
||
128 | * |
||
129 | * @Then I should see in the header :header::value |
||
130 | */ |
||
131 | public function iShouldSeeInTheHeader($header, $value) { |
||
137 | |||
138 | /** |
||
139 | * Checks that the given element is of the given type. |
||
140 | * |
||
141 | * @param NodeElement $element |
||
142 | * The element to check. |
||
143 | * @param string $type |
||
144 | * The expected type. |
||
145 | * |
||
146 | * @throws ExpectationException |
||
147 | * Thrown when the given element is not of the expected type. |
||
148 | */ |
||
149 | public function assertElementType(NodeElement $element, $type) { |
||
154 | |||
155 | /** |
||
156 | * Get the edit link for a node. |
||
157 | * |
||
158 | * @param string $link |
||
159 | * The link name. |
||
160 | * @param string $title |
||
161 | * The node title. |
||
162 | * |
||
163 | * @return \Behat\Mink\Element\NodeElement|null |
||
164 | * The link if found. |
||
165 | * |
||
166 | * @throws \Exception |
||
167 | */ |
||
168 | public function getContentEditLink($link, $title) { |
||
188 | |||
189 | /** |
||
190 | * Set browser size to mobile. |
||
191 | * |
||
192 | * @BeforeScenario @javascript&&@mobile |
||
193 | */ |
||
194 | public function beforeMobileScenario() { |
||
197 | |||
198 | /** |
||
199 | * Reset browser size. |
||
200 | * |
||
201 | * @AfterScenario @javascript |
||
202 | */ |
||
203 | public function afterJavascriptScenario() { |
||
206 | |||
207 | /** |
||
208 | * Resize the browser window. |
||
209 | * |
||
210 | * @BeforeStep |
||
211 | */ |
||
212 | public function adjustScreenSizeBeforeStep() { |
||
220 | |||
221 | /** |
||
222 | * Assert presence of given field on the page. |
||
223 | * |
||
224 | * @Then I should see the field :field |
||
225 | */ |
||
226 | View Code Duplication | public function iShouldSeeTheField($field) { |
|
243 | |||
244 | /** |
||
245 | * Assert absence of given field. |
||
246 | * |
||
247 | * @Then I should not see the field :field |
||
248 | */ |
||
249 | View Code Duplication | public function iShouldNotSeeTheField($field) { |
|
266 | |||
267 | /** |
||
268 | * Visit taxonomy term page given its type and name. |
||
269 | * |
||
270 | * @Given I am visiting the :type term :title |
||
271 | * @Given I visit the :type term :title |
||
272 | */ |
||
273 | public function iAmViewingTheTerm($type, $title) { |
||
276 | |||
277 | /** |
||
278 | * Visit taxonomy term edit page given its type and name. |
||
279 | * |
||
280 | * @Given I am editing the :type term :title |
||
281 | * @Given I edit the :type term :title |
||
282 | */ |
||
283 | public function iAmEditingTheTerm($type, $title) { |
||
286 | |||
287 | /** |
||
288 | * Provides a common step definition callback for node pages. |
||
289 | * |
||
290 | * @param string $op |
||
291 | * The operation being performed: 'view', 'edit', 'delete'. |
||
292 | * @param string $type |
||
293 | * The node type either as id or as label. |
||
294 | * @param string $title |
||
295 | * The node title. |
||
296 | * |
||
297 | * @throws ExpectationException |
||
298 | * When the node does not exist. |
||
299 | */ |
||
300 | protected function visitTermPage($op, $type, $title) { |
||
320 | |||
321 | /** |
||
322 | * Assert first element precedes second one. |
||
323 | * |
||
324 | * @Then :first should precede :second |
||
325 | */ |
||
326 | public function shouldPrecede($first, $second) { |
||
340 | |||
341 | } |
||
342 |
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.