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 MinkContext 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 MinkContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class MinkContext extends MinkExtension implements TranslatableContext |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Returns list of definition translation resources paths. |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | public static function getTranslationResources() |
||
24 | |||
25 | /** |
||
26 | * Return a region from the current page. |
||
27 | * |
||
28 | * @throws \Exception |
||
29 | * If region cannot be found. |
||
30 | * |
||
31 | * @param string $region |
||
32 | * The machine name of the region to return. |
||
33 | * |
||
34 | * @return \Behat\Mink\Element\NodeElement |
||
35 | */ |
||
36 | View Code Duplication | public function getRegion($region) |
|
46 | |||
47 | /** |
||
48 | * Visit a given path, and additionally check for HTTP response code 200. |
||
49 | * |
||
50 | * @Given I am at :path |
||
51 | * @When I visit :path |
||
52 | * |
||
53 | * @throws UnsupportedDriverActionException |
||
54 | */ |
||
55 | public function assertAtPath($path) |
||
67 | |||
68 | /** |
||
69 | * @When I click :link |
||
70 | */ |
||
71 | public function assertClick($link) |
||
76 | |||
77 | /** |
||
78 | * @Given for :field I enter :value |
||
79 | * @Given I enter :value for :field |
||
80 | */ |
||
81 | public function assertEnterField($field, $value) |
||
86 | |||
87 | /** |
||
88 | * For javascript enabled scenarios, always wait for AJAX before clicking. |
||
89 | * |
||
90 | * @BeforeStep |
||
91 | */ |
||
92 | View Code Duplication | public function beforeJavascriptStep($event) |
|
104 | |||
105 | /** |
||
106 | * For javascript enabled scenarios, always wait for AJAX after clicking. |
||
107 | * |
||
108 | * @AfterStep |
||
109 | */ |
||
110 | View Code Duplication | public function afterJavascriptStep($event) |
|
122 | |||
123 | /** |
||
124 | * Wait for AJAX to finish. |
||
125 | * |
||
126 | * @see \Drupal\FunctionalJavascriptTests\JSWebAssert::assertWaitOnAjaxRequest() |
||
127 | * |
||
128 | * @Given I wait for AJAX to finish |
||
129 | */ |
||
130 | public function iWaitForAjaxToFinish() |
||
155 | /** |
||
156 | * Presses button with specified id|name|title|alt|value. |
||
157 | * |
||
158 | * @When I press the :button button |
||
159 | */ |
||
160 | public function pressButton($button) |
||
178 | |||
179 | /** |
||
180 | * @Given I press the :char key in the :field field |
||
181 | * |
||
182 | * @param mixed $char could be either char ('b') or char-code (98) |
||
183 | * @throws \Exception |
||
184 | */ |
||
185 | public function pressKey($char, $field) |
||
233 | |||
234 | /** |
||
235 | * @Then I should see the link :link |
||
236 | */ |
||
237 | View Code Duplication | public function assertLinkVisible($link) |
|
256 | |||
257 | /** |
||
258 | * Links are not loaded on the page. |
||
259 | * |
||
260 | * @Then I should not see the link :link |
||
261 | */ |
||
262 | View Code Duplication | public function assertNotLinkVisible($link) |
|
281 | |||
282 | /** |
||
283 | * Links are loaded but not visually visible (e.g they have display: hidden applied). |
||
284 | * |
||
285 | * @Then I should not visibly see the link :link |
||
286 | */ |
||
287 | View Code Duplication | public function assertNotLinkVisuallyVisible($link) |
|
306 | |||
307 | /** |
||
308 | * @Then I (should )see the heading :heading |
||
309 | */ |
||
310 | View Code Duplication | public function assertHeading($heading) |
|
323 | |||
324 | /** |
||
325 | * @Then I (should )not see the heading :heading |
||
326 | */ |
||
327 | View Code Duplication | public function assertNotHeading($heading) |
|
339 | |||
340 | /** |
||
341 | * @Then I (should ) see the button :button |
||
342 | * @Then I (should ) see the :button button |
||
343 | */ |
||
344 | View Code Duplication | public function assertButton($button) |
|
352 | |||
353 | /** |
||
354 | * @Then I should not see the button :button |
||
355 | * @Then I should not see the :button button |
||
356 | */ |
||
357 | View Code Duplication | public function assertNotButton($button) |
|
365 | |||
366 | /** |
||
367 | * @When I follow/click :link in the :region( region) |
||
368 | * |
||
369 | * @throws \Exception |
||
370 | * If region or link within it cannot be found. |
||
371 | */ |
||
372 | View Code Duplication | public function assertRegionLinkFollow($link, $region) |
|
383 | |||
384 | /** |
||
385 | * Checks, if a button with id|name|title|alt|value exists or not and pressess the same |
||
386 | * |
||
387 | * @Given I press :button in the :region( region) |
||
388 | * |
||
389 | * @param $button |
||
390 | * string The id|name|title|alt|value of the button to be pressed |
||
391 | * @param $region |
||
392 | * string The region in which the button should be pressed |
||
393 | * |
||
394 | * @throws \Exception |
||
395 | * If region or button within it cannot be found. |
||
396 | */ |
||
397 | View Code Duplication | public function assertRegionPressButton($button, $region) |
|
407 | |||
408 | /** |
||
409 | * Fills in a form field with id|name|title|alt|value in the specified region. |
||
410 | * |
||
411 | * @Given I fill in :value for :field in the :region( region) |
||
412 | * @Given I fill in :field with :value in the :region( region) |
||
413 | * |
||
414 | * @throws \Exception |
||
415 | * If region cannot be found. |
||
416 | */ |
||
417 | public function regionFillField($field, $value, $region) |
||
424 | |||
425 | /** |
||
426 | * Find a heading in a specific region. |
||
427 | * |
||
428 | * @Then I should see the heading :heading in the :region( region) |
||
429 | * @Then I should see the :heading heading in the :region( region) |
||
430 | * |
||
431 | * @throws \Exception |
||
432 | * If region or header within it cannot be found. |
||
433 | */ |
||
434 | public function assertRegionHeading($heading, $region) |
||
451 | |||
452 | /** |
||
453 | * @Then I should see the link :link in the :region( region) |
||
454 | * |
||
455 | * @throws \Exception |
||
456 | * If region or link within it cannot be found. |
||
457 | */ |
||
458 | View Code Duplication | public function assertLinkRegion($link, $region) |
|
467 | |||
468 | /** |
||
469 | * @Then I should not see the link :link in the :region( region) |
||
470 | * |
||
471 | * @throws \Exception |
||
472 | * If region or link within it cannot be found. |
||
473 | */ |
||
474 | View Code Duplication | public function assertNotLinkRegion($link, $region) |
|
483 | |||
484 | /** |
||
485 | * @Then I should see( the text) :text in the :region( region) |
||
486 | * |
||
487 | * @throws \Exception |
||
488 | * If region or text within it cannot be found. |
||
489 | */ |
||
490 | View Code Duplication | public function assertRegionText($text, $region) |
|
500 | |||
501 | /** |
||
502 | * @Then I should not see( the text) :text in the :region( region) |
||
503 | * |
||
504 | * @throws \Exception |
||
505 | * If region or text within it cannot be found. |
||
506 | */ |
||
507 | View Code Duplication | public function assertNotRegionText($text, $region) |
|
517 | |||
518 | /** |
||
519 | * @Then I (should )see the text :text |
||
520 | */ |
||
521 | public function assertTextVisible($text) |
||
526 | |||
527 | /** |
||
528 | * @Then I should not see the text :text |
||
529 | */ |
||
530 | public function assertNotTextVisible($text) |
||
535 | |||
536 | /** |
||
537 | * @Then I should get a :code HTTP response |
||
538 | */ |
||
539 | public function assertHttpResponse($code) |
||
544 | |||
545 | /** |
||
546 | * @Then I should not get a :code HTTP response |
||
547 | */ |
||
548 | public function assertNotHttpResponse($code) |
||
553 | |||
554 | /** |
||
555 | * @Given I check the box :checkbox |
||
556 | */ |
||
557 | public function assertCheckBox($checkbox) |
||
562 | |||
563 | /** |
||
564 | * @Given I uncheck the box :checkbox |
||
565 | */ |
||
566 | public function assertUncheckBox($checkbox) |
||
571 | |||
572 | /** |
||
573 | * @When I select the radio button :label with the id :id |
||
574 | * @When I select the radio button :label |
||
575 | * |
||
576 | * @TODO convert to mink extension. |
||
577 | */ |
||
578 | public function assertSelectRadioById($label, $id = '') |
||
593 | |||
594 | /** |
||
595 | * @} End of defgroup "mink extensions" |
||
596 | */ |
||
597 | } |
||
598 |
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.