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:
Complex classes like DrupalContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DrupalContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class DrupalContext extends RawDrupalContext implements TranslatableContext { |
||
14 | |||
15 | /** |
||
16 | * Returns list of definition translation resources paths. |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | public static function getTranslationResources() { |
||
23 | |||
24 | /** |
||
25 | * @Given I am an anonymous user |
||
26 | * @Given I am not logged in |
||
27 | */ |
||
28 | public function assertAnonymousUser() { |
||
34 | |||
35 | /** |
||
36 | * Creates and authenticates a user with the given role(s). |
||
37 | * |
||
38 | * @Given I am logged in as a user with the :role role(s) |
||
39 | * @Given I am logged in as a/an :role |
||
40 | */ |
||
41 | public function assertAuthenticatedByRole($role) { |
||
67 | |||
68 | /** |
||
69 | * Creates and authenticates a user with the given role(s) and given fields. |
||
70 | * | field_user_name | John | |
||
71 | * | field_user_surname | Smith | |
||
72 | * | ... | ... | |
||
73 | * |
||
74 | * @Given I am logged in as a user with the :role role(s) and I have the following fields: |
||
75 | */ |
||
76 | public function assertAuthenticatedByRoleWithGivenFields($role, TableNode $fields) { |
||
107 | |||
108 | |||
109 | /** |
||
110 | * @Given I am logged in as :name |
||
111 | */ |
||
112 | public function assertLoggedInByName($name) { |
||
113 | $manager = $this->getUserManager(); |
||
114 | |||
115 | // Change internal current user. |
||
116 | $manager->setCurrentUser($manager->getUser($name)); |
||
117 | |||
118 | // Login. |
||
119 | $this->login($manager->getUser($name)); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @Given I am logged in as a user with the :permissions permission(s) |
||
124 | */ |
||
125 | public function assertLoggedInWithPermissions($permissions) { |
||
143 | |||
144 | /** |
||
145 | * Retrieve a table row containing specified text from a given element. |
||
146 | * |
||
147 | * @param \Behat\Mink\Element\Element |
||
148 | * @param string |
||
149 | * The text to search for in the table row. |
||
150 | * |
||
151 | * @return \Behat\Mink\Element\NodeElement |
||
152 | * |
||
153 | * @throws \Exception |
||
154 | */ |
||
155 | public function getTableRow(Element $element, $search) { |
||
167 | |||
168 | /** |
||
169 | * Find text in a table row containing given text. |
||
170 | * |
||
171 | * @Then I should see (the text ):text in the :rowText row |
||
172 | */ |
||
173 | public function assertTextInTableRow($text, $rowText) { |
||
179 | |||
180 | /** |
||
181 | * Asset text not in a table row containing given text. |
||
182 | * |
||
183 | * @Then I should not see (the text ):text in the :rowText row |
||
184 | */ |
||
185 | public function assertTextNotInTableRow($text, $rowText) { |
||
186 | $row = $this->getTableRow($this->getSession()->getPage(), $rowText); |
||
187 | View Code Duplication | if (strpos($row->getText(), $text) !== FALSE) { |
|
188 | throw new \Exception(sprintf('Found a row containing "%s", but it contained the text "%s".', $rowText, $text)); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Attempts to find a link in a table row containing giving text. This is for |
||
194 | * administrative pages such as the administer content types screen found at |
||
195 | * `admin/structure/types`. |
||
196 | * |
||
197 | * @Given I click :link in the :rowText row |
||
198 | * @Then I (should )see the :link in the :rowText row |
||
199 | */ |
||
200 | public function assertClickInTableRow($link, $rowText) { |
||
201 | $page = $this->getSession()->getPage(); |
||
202 | if ($link_element = $this->getTableRow($page, $rowText)->findLink($link)) { |
||
203 | // Click the link and return. |
||
204 | $link_element->click(); |
||
205 | return; |
||
206 | } |
||
207 | throw new \Exception(sprintf('Found a row containing "%s", but no "%s" link on the page %s', $rowText, $link, $this->getSession()->getCurrentUrl())); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @Given the cache has been cleared |
||
212 | */ |
||
213 | public function assertCacheClear() { |
||
216 | |||
217 | /** |
||
218 | * @Given I run cron |
||
219 | */ |
||
220 | public function assertCron() { |
||
223 | |||
224 | /** |
||
225 | * Creates content of the given type. |
||
226 | * |
||
227 | * @Given I am viewing a/an :type (content )with the title :title |
||
228 | * @Given a/an :type (content )with the title :title |
||
229 | */ |
||
230 | public function createNode($type, $title) { |
||
240 | |||
241 | /** |
||
242 | * Creates content authored by the current user. |
||
243 | * |
||
244 | * @Given I am viewing my :type (content )with the title :title |
||
245 | */ |
||
246 | public function createMyNode($type, $title) { |
||
262 | |||
263 | /** |
||
264 | * Creates content of a given type provided in the form: |
||
265 | * | title | author | status | created | |
||
266 | * | My title | Joe Editor | 1 | 2014-10-17 8:00am | |
||
267 | * | ... | ... | ... | ... | |
||
268 | * |
||
269 | * @Given :type content: |
||
270 | */ |
||
271 | public function createNodes($type, TableNode $nodesTable) { |
||
278 | |||
279 | /** |
||
280 | * Creates content of the given type, provided in the form: |
||
281 | * | title | My node | |
||
282 | * | Field One | My field value | |
||
283 | * | author | Joe Editor | |
||
284 | * | status | 1 | |
||
285 | * | ... | ... | |
||
286 | * |
||
287 | * @Given I am viewing a/an :type( content): |
||
288 | */ |
||
289 | public function assertViewingNode($type, TableNode $fields) { |
||
302 | |||
303 | /** |
||
304 | * Asserts that a given content type is editable. |
||
305 | * |
||
306 | * @Then I should be able to edit a/an :type( content) |
||
307 | */ |
||
308 | public function assertEditNodeOfType($type) { |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Creates a term on an existing vocabulary. |
||
325 | * |
||
326 | * @Given I am viewing a/an :vocabulary term with the name :name |
||
327 | * @Given a/an :vocabulary term with the name :name |
||
328 | */ |
||
329 | public function createTerm($vocabulary, $name) { |
||
341 | |||
342 | /** |
||
343 | * Creates multiple users. |
||
344 | * |
||
345 | * Provide user data in the following format: |
||
346 | * |
||
347 | * | name | mail | roles | |
||
348 | * | user foo | [email protected] | role1, role2 | |
||
349 | * |
||
350 | * @Given users: |
||
351 | */ |
||
352 | public function createUsers(TableNode $usersTable) { |
||
376 | |||
377 | /** |
||
378 | * Creates one or more terms on an existing vocabulary. |
||
379 | * |
||
380 | * Provide term data in the following format: |
||
381 | * |
||
382 | * | name | parent | description | weight | taxonomy_field_image | |
||
383 | * | Snook | Fish | Marine fish | 10 | snook-123.jpg | |
||
384 | * | ... | ... | ... | ... | ... | |
||
385 | * |
||
386 | * Only the 'name' field is required. |
||
387 | * |
||
388 | * @Given :vocabulary terms: |
||
389 | */ |
||
390 | public function createTerms($vocabulary, TableNode $termsTable) { |
||
397 | |||
398 | /** |
||
399 | * Creates one or more languages. |
||
400 | * |
||
401 | * @Given the/these (following )languages are available: |
||
402 | * |
||
403 | * Provide language data in the following format: |
||
404 | * |
||
405 | * | langcode | |
||
406 | * | en | |
||
407 | * | fr | |
||
408 | * |
||
409 | * @param TableNode $langcodesTable |
||
410 | * The table listing languages by their ISO code. |
||
411 | */ |
||
412 | public function createLanguages(TableNode $langcodesTable) { |
||
420 | |||
421 | /** |
||
422 | * Pauses the scenario until the user presses a key. Useful when debugging a scenario. |
||
423 | * |
||
424 | * @Then (I )break |
||
425 | */ |
||
426 | public function iPutABreakpoint() |
||
451 | |||
452 | } |
||
453 |
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.