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) { |
||
123 | |||
124 | /** |
||
125 | * @Given I am logged in as a user with the :permissions permission(s) |
||
126 | */ |
||
127 | public function assertLoggedInWithPermissions($permissions) { |
||
145 | |||
146 | /** |
||
147 | * Retrieve a table row containing specified text from a given element. |
||
148 | * |
||
149 | * @param \Behat\Mink\Element\Element |
||
150 | * @param string |
||
151 | * The text to search for in the table row. |
||
152 | * |
||
153 | * @return \Behat\Mink\Element\NodeElement |
||
154 | * |
||
155 | * @throws \Exception |
||
156 | */ |
||
157 | public function getTableRow(Element $element, $search) { |
||
169 | |||
170 | /** |
||
171 | * Find text in a table row containing given text. |
||
172 | * |
||
173 | * @Then I should see (the text ):text in the ":rowText" row |
||
174 | */ |
||
175 | public function assertTextInTableRow($text, $rowText) { |
||
181 | |||
182 | /** |
||
183 | * Attempts to find a link in a table row containing giving text. This is for |
||
184 | * administrative pages such as the administer content types screen found at |
||
185 | * `admin/structure/types`. |
||
186 | * |
||
187 | * @Given I click :link in the :rowText row |
||
188 | * @Then I (should )see the :link in the :rowText row |
||
189 | */ |
||
190 | public function assertClickInTableRow($link, $rowText) { |
||
199 | |||
200 | /** |
||
201 | * @Given the cache has been cleared |
||
202 | */ |
||
203 | public function assertCacheClear() { |
||
206 | |||
207 | /** |
||
208 | * @Given I run cron |
||
209 | */ |
||
210 | public function assertCron() { |
||
213 | |||
214 | /** |
||
215 | * Creates content of the given type. |
||
216 | * |
||
217 | * @Given I am viewing a/an :type (content )with the title :title |
||
218 | * @Given a/an :type (content )with the title :title |
||
219 | */ |
||
220 | View Code Duplication | public function createNode($type, $title) { |
|
231 | |||
232 | /** |
||
233 | * Creates content authored by the current user. |
||
234 | * |
||
235 | * @Given I am viewing my :type (content )with the title :title |
||
236 | */ |
||
237 | public function createMyNode($type, $title) { |
||
253 | |||
254 | /** |
||
255 | * Creates content of a given type provided in the form: |
||
256 | * | title | author | status | created | |
||
257 | * | My title | Joe Editor | 1 | 2014-10-17 8:00am | |
||
258 | * | ... | ... | ... | ... | |
||
259 | * |
||
260 | * @Given :type content: |
||
261 | */ |
||
262 | public function createNodes($type, TableNode $nodesTable) { |
||
269 | |||
270 | /** |
||
271 | * Creates content of the given type, provided in the form: |
||
272 | * | title | My node | |
||
273 | * | Field One | My field value | |
||
274 | * | author | Joe Editor | |
||
275 | * | status | 1 | |
||
276 | * | ... | ... | |
||
277 | * |
||
278 | * @Given I am viewing a/an :type( content): |
||
279 | */ |
||
280 | public function assertViewingNode($type, TableNode $fields) { |
||
293 | |||
294 | /** |
||
295 | * Asserts that a given content type is editable. |
||
296 | * |
||
297 | * @Then I should be able to edit a/an :type( content) |
||
298 | */ |
||
299 | View Code Duplication | public function assertEditNodeOfType($type) { |
|
309 | |||
310 | |||
311 | /** |
||
312 | * Creates a term on an existing vocabulary. |
||
313 | * |
||
314 | * @Given I am viewing a/an :vocabulary term with the name :name |
||
315 | * @Given a/an :vocabulary term with the name :name |
||
316 | */ |
||
317 | View Code Duplication | public function createTerm($vocabulary, $name) { |
|
329 | |||
330 | /** |
||
331 | * Creates multiple users. |
||
332 | * |
||
333 | * Provide user data in the following format: |
||
334 | * |
||
335 | * | name | mail | roles | |
||
336 | * | user foo | [email protected] | role1, role2 | |
||
337 | * |
||
338 | * @Given users: |
||
339 | */ |
||
340 | public function createUsers(TableNode $usersTable) { |
||
364 | |||
365 | /** |
||
366 | * Creates one or more terms on an existing vocabulary. |
||
367 | * |
||
368 | * @Given :vocabulary terms: |
||
369 | */ |
||
370 | public function createTerms($vocabulary, TableNode $termsTable) { |
||
377 | |||
378 | /** |
||
379 | * Creates one or more languages. |
||
380 | * |
||
381 | * @Given the/these (following )languages are available: |
||
382 | * |
||
383 | * Provide language data in the following format: |
||
384 | * |
||
385 | * | langcode | |
||
386 | * | en | |
||
387 | * | fr | |
||
388 | * |
||
389 | * @param TableNode $langcodesTable |
||
390 | * The table listing languages by their ISO code. |
||
391 | */ |
||
392 | public function createLanguages(TableNode $langcodesTable) { |
||
400 | |||
401 | /** |
||
402 | * Installs the given module. |
||
403 | * |
||
404 | * @Given the :module module is enabled/installed |
||
405 | * |
||
406 | * @param string $module |
||
407 | * The name of the module to install. |
||
408 | */ |
||
409 | public function installModule($module) { |
||
412 | |||
413 | /** |
||
414 | * Installs the given modules. |
||
415 | * |
||
416 | * @Given the following modules are enabled/installed: |
||
417 | * |
||
418 | * Provide the list of modules in the following format: |
||
419 | * | modules | |
||
420 | * | comment | |
||
421 | * | forum | |
||
422 | * |
||
423 | * @param TableNode $modulesTable |
||
424 | * The list of modules to install. |
||
425 | */ |
||
426 | View Code Duplication | public function installModuleList(TableNode $modulesTable) { |
|
433 | |||
434 | /** |
||
435 | * Uninstalls the given module. |
||
436 | * |
||
437 | * @Given the :module module is disabled/uninstalled |
||
438 | * |
||
439 | * @param string $module |
||
440 | * The name of the module to uninstall. |
||
441 | */ |
||
442 | public function uninstallModule($module) { |
||
445 | |||
446 | /** |
||
447 | * Uninstalls the given modules. |
||
448 | * |
||
449 | * @Given the following modules are disabled/uninstalled: |
||
450 | * |
||
451 | * Provide the list of modules in the following format: |
||
452 | * | modules | |
||
453 | * | comment | |
||
454 | * | forum | |
||
455 | * |
||
456 | * @param TableNode $modulesTable |
||
457 | * The list of modules to uninstall. |
||
458 | */ |
||
459 | View Code Duplication | public function uninstallModuleList(TableNode $modulesTable) { |
|
466 | |||
467 | /** |
||
468 | * Checks if the given module is active. |
||
469 | * |
||
470 | * @Then the :module module should be enabled/active |
||
471 | * |
||
472 | * @param string $module |
||
473 | * The name of the module to check. |
||
474 | * |
||
475 | * @throws \Exception |
||
476 | * Thrown when the module is not active. |
||
477 | */ |
||
478 | public function assertModuleActive($module) { |
||
484 | |||
485 | /** |
||
486 | * Checks if the given module is not active. |
||
487 | * |
||
488 | * @Then the :module module should not be enabled/active |
||
489 | * |
||
490 | * @param string $module |
||
491 | * The name of the module to check. |
||
492 | * |
||
493 | * @throws \Exception |
||
494 | * Thrown when the module is active. |
||
495 | */ |
||
496 | public function assertModuleNotActive($module) { |
||
502 | |||
503 | /** |
||
504 | * Pauses the scenario until the user presses a key. Useful when debugging a scenario. |
||
505 | * |
||
506 | * @Then (I )break |
||
507 | */ |
||
508 | public function iPutABreakpoint() |
||
515 | |||
516 | } |
||
517 |
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.