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 BasicContext 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 BasicContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 26 | class BasicContext extends BehatContext | ||
| 27 | { | ||
| 28 | protected $context; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * Date format in date() syntax | ||
| 32 | * @var String | ||
| 33 | */ | ||
| 34 | protected $dateFormat = 'Y-m-d'; | ||
| 35 | |||
| 36 | /** | ||
| 37 | * Time format in date() syntax | ||
| 38 | * @var String | ||
| 39 | */ | ||
| 40 | protected $timeFormat = 'H:i:s'; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Date/time format in date() syntax | ||
| 44 | * @var String | ||
| 45 | */ | ||
| 46 | protected $datetimeFormat = 'Y-m-d H:i:s'; | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Initializes context. | ||
| 50 | * Every scenario gets it's own context object. | ||
| 51 | * | ||
| 52 | * @param array $parameters context parameters (set them up through behat.yml) | ||
| 53 | */ | ||
| 54 | public function __construct(array $parameters) | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Get Mink session from MinkContext | ||
| 62 | * | ||
| 63 | * @return \Behat\Mink\Session | ||
| 64 | */ | ||
| 65 | public function getSession($name = null) | ||
| 69 | |||
| 70 | /** | ||
| 71 | * @AfterStep ~@modal | ||
| 72 | * | ||
| 73 | * Excluding scenarios with @modal tag is required, | ||
| 74 | * because modal dialogs stop any JS interaction | ||
| 75 | */ | ||
| 76 | public function appendErrorHandlerBeforeStep(StepEvent $event) | ||
| 101 | |||
| 102 | /** | ||
| 103 | * @AfterStep ~@modal | ||
| 104 | * | ||
| 105 | * Excluding scenarios with @modal tag is required, | ||
| 106 | * because modal dialogs stop any JS interaction | ||
| 107 | */ | ||
| 108 | public function readErrorHandlerAfterStep(StepEvent $event) | ||
| 132 | |||
| 133 | /** | ||
| 134 | * Hook into jQuery ajaxStart, ajaxSuccess and ajaxComplete events. | ||
| 135 | * Prepare __ajaxStatus() functions and attach them to these handlers. | ||
| 136 | * Event handlers are removed after one run. | ||
| 137 | * | ||
| 138 | * @BeforeStep | ||
| 139 | */ | ||
| 140 | View Code Duplication | public function handleAjaxBeforeStep(StepEvent $event) | |
| 179 | |||
| 180 | /** | ||
| 181 | * Wait for the __ajaxStatus()to return anything but 'waiting'. | ||
| 182 | * Don't wait longer than 5 seconds. | ||
| 183 | * | ||
| 184 | * Don't unregister handler if we're dealing with modal windows | ||
| 185 | * | ||
| 186 | * @AfterStep ~@modal | ||
| 187 | */ | ||
| 188 | View Code Duplication | public function handleAjaxAfterStep(StepEvent $event) | |
| 212 | |||
| 213 | public function handleAjaxTimeout() | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Take screenshot when step fails. | ||
| 228 | * Works only with Selenium2Driver. | ||
| 229 | * | ||
| 230 | * @AfterStep | ||
| 231 | */ | ||
| 232 | public function takeScreenshotAfterFailedStep(StepEvent $event) | ||
| 242 | |||
| 243 | /** | ||
| 244 | * Close modal dialog if test scenario fails on CMS page | ||
| 245 | * | ||
| 246 | * @AfterScenario | ||
| 247 | */ | ||
| 248 | public function closeModalDialog(ScenarioEvent $event) | ||
| 268 | |||
| 269 | /** | ||
| 270 | * Delete any created files and folders from assets directory | ||
| 271 | * | ||
| 272 | * @AfterScenario @assets | ||
| 273 | */ | ||
| 274 | public function cleanAssetsAfterScenario(ScenarioEvent $event) | ||
| 281 | |||
| 282 | public function takeScreenshot(StepEvent $event) | ||
| 322 | |||
| 323 | /** | ||
| 324 | * @Then /^I should be redirected to "([^"]+)"/ | ||
| 325 | */ | ||
| 326 | public function stepIShouldBeRedirectedTo($url) | ||
| 338 | |||
| 339 | /** | ||
| 340 | * @Given /^the page can't be found/ | ||
| 341 | */ | ||
| 342 | public function stepPageCantBeFound() | ||
| 352 | |||
| 353 | /** | ||
| 354 | * @Given /^I wait (?:for )?([\d\.]+) second(?:s?)$/ | ||
| 355 | */ | ||
| 356 | public function stepIWaitFor($secs) | ||
| 360 | |||
| 361 | /** | ||
| 362 | * @Given /^I press the "([^"]*)" button$/ | ||
| 363 | */ | ||
| 364 | public function stepIPressTheButton($button) | ||
| 377 | |||
| 378 | /** | ||
| 379 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. | ||
| 380 | * Example1: I press the "Remove current combo" button, confirming the dialog | ||
| 381 | * Example2: I follow the "Remove current combo" link, confirming the dialog | ||
| 382 | * | ||
| 383 | * @Given /^I (?:press|follow) the "([^"]*)" (?:button|link), confirming the dialog$/ | ||
| 384 | */ | ||
| 385 | public function stepIPressTheButtonConfirmingTheDialog($button) | ||
| 390 | |||
| 391 | /** | ||
| 392 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. | ||
| 393 | * Example: I follow the "Remove current combo" link, dismissing the dialog | ||
| 394 | * | ||
| 395 | * @Given /^I (?:press|follow) the "([^"]*)" (?:button|link), dismissing the dialog$/ | ||
| 396 | */ | ||
| 397 | public function stepIPressTheButtonDismissingTheDialog($button) | ||
| 402 | |||
| 403 | /** | ||
| 404 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element$/ | ||
| 405 | */ | ||
| 406 | public function iClickInTheElement($clickType, $text, $selector) | ||
| 420 | |||
| 421 | /** | ||
| 422 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. | ||
| 423 | * Example: I click "Delete" in the ".actions" element, confirming the dialog | ||
| 424 | * | ||
| 425 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, confirming the dialog$/ | ||
| 426 | */ | ||
| 427 | public function iClickInTheElementConfirmingTheDialog($clickType, $text, $selector) | ||
| 432 | /** | ||
| 433 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. | ||
| 434 | * Example: I click "Delete" in the ".actions" element, dismissing the dialog | ||
| 435 | * | ||
| 436 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, dismissing the dialog$/ | ||
| 437 | */ | ||
| 438 | public function iClickInTheElementDismissingTheDialog($clickType, $text, $selector) | ||
| 443 | |||
| 444 | /** | ||
| 445 | * @Given /^I type "([^"]*)" into the dialog$/ | ||
| 446 | */ | ||
| 447 | public function iTypeIntoTheDialog($data) | ||
| 454 | |||
| 455 | /** | ||
| 456 | * @Given /^I confirm the dialog$/ | ||
| 457 | */ | ||
| 458 | public function iConfirmTheDialog() | ||
| 463 | |||
| 464 | /** | ||
| 465 | * @Given /^I dismiss the dialog$/ | ||
| 466 | */ | ||
| 467 | public function iDismissTheDialog() | ||
| 472 | |||
| 473 | /** | ||
| 474 | * @Given /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)" with HTML5$/ | ||
| 475 | */ | ||
| 476 | public function iAttachTheFileTo($field, $path) | ||
| 491 | |||
| 492 | /** | ||
| 493 | * Select an individual input from within a group, matched by the top-most label. | ||
| 494 | * | ||
| 495 | * @Given /^I select "([^"]*)" from "([^"]*)" input group$/ | ||
| 496 | */ | ||
| 497 | public function iSelectFromInputGroup($value, $labelText) | ||
| 535 | |||
| 536 | /** | ||
| 537 | * Pauses the scenario until the user presses a key. Useful when debugging a scenario. | ||
| 538 | * | ||
| 539 | * @Then /^(?:|I )put a breakpoint$/ | ||
| 540 | */ | ||
| 541 | public function iPutABreakpoint() | ||
| 550 | |||
| 551 | /** | ||
| 552 | * Transforms relative time statements compatible with strtotime(). | ||
| 553 | * Example: "time of 1 hour ago" might return "22:00:00" if its currently "23:00:00". | ||
| 554 |      * Customize through {@link setTimeFormat()}. | ||
| 555 | * | ||
| 556 | * @Transform /^(?:(the|a)) time of (?<val>.*)$/ | ||
| 557 | */ | ||
| 558 | View Code Duplication | public function castRelativeToAbsoluteTime($prefix, $val) | |
| 569 | |||
| 570 | /** | ||
| 571 | * Transforms relative date and time statements compatible with strtotime(). | ||
| 572 | * Example: "datetime of 2 days ago" might return "2013-10-10 22:00:00" if its currently | ||
| 573 |      * the 12th of October 2013. Customize through {@link setDatetimeFormat()}. | ||
| 574 | * | ||
| 575 | * @Transform /^(?:(the|a)) datetime of (?<val>.*)$/ | ||
| 576 | */ | ||
| 577 | View Code Duplication | public function castRelativeToAbsoluteDatetime($prefix, $val) | |
| 588 | |||
| 589 | /** | ||
| 590 | * Transforms relative date statements compatible with strtotime(). | ||
| 591 | * Example: "date 2 days ago" might return "2013-10-10" if its currently | ||
| 592 |      * the 12th of October 2013. Customize through {@link setDateFormat()}. | ||
| 593 | * | ||
| 594 | * @Transform /^(?:(the|a)) date of (?<val>.*)$/ | ||
| 595 | */ | ||
| 596 | View Code Duplication | public function castRelativeToAbsoluteDate($prefix, $val) | |
| 607 | |||
| 608 | public function getDateFormat() | ||
| 612 | |||
| 613 | public function setDateFormat($format) | ||
| 617 | |||
| 618 | public function getTimeFormat() | ||
| 622 | |||
| 623 | public function setTimeFormat($format) | ||
| 627 | |||
| 628 | public function getDatetimeFormat() | ||
| 632 | |||
| 633 | public function setDatetimeFormat($format) | ||
| 637 | |||
| 638 | /** | ||
| 639 | * Checks that field with specified in|name|label|value is disabled. | ||
| 640 | * Example: Then the field "Email" should be disabled | ||
| 641 | * Example: Then the "Email" field should be disabled | ||
| 642 | * | ||
| 643 | * @Then /^the "(?P<name>(?:[^"]|\\")*)" (?P<type>(?:(field|button))) should (?P<negate>(?:(not |)))be disabled/ | ||
| 644 | * @Then /^the (?P<type>(?:(field|button))) "(?P<name>(?:[^"]|\\")*)" should (?P<negate>(?:(not |)))be disabled/ | ||
| 645 | */ | ||
| 646 | public function stepFieldShouldBeDisabled($name, $type, $negate) | ||
| 666 | |||
| 667 | /** | ||
| 668 | * Checks that checkbox with specified in|name|label|value is enabled. | ||
| 669 | * Example: Then the field "Email" should be enabled | ||
| 670 | * Example: Then the "Email" field should be enabled | ||
| 671 | * | ||
| 672 | * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should be enabled/ | ||
| 673 | * @Then /^the field "(?P<field>(?:[^"]|\\")*)" should be enabled/ | ||
| 674 | */ | ||
| 675 | public function stepFieldShouldBeEnabled($field) | ||
| 685 | |||
| 686 | /** | ||
| 687 | * Clicks a link in a specific region (an element identified by a CSS selector, a "data-title" attribute, | ||
| 688 | * or a named region mapped to a CSS selector via Behat configuration). | ||
| 689 | * | ||
| 690 | * Example: Given I follow "Select" in the "header .login-form" region | ||
| 691 | * Example: Given I follow "Select" in the "My Login Form" region | ||
| 692 | * | ||
| 693 | * @Given /^I (?:follow|click) "(?P<link>[^"]*)" in the "(?P<region>[^"]*)" region$/ | ||
| 694 | */ | ||
| 695 | View Code Duplication | public function iFollowInTheRegion($link, $region) | |
| 709 | |||
| 710 | /** | ||
| 711 | * Fills in a field in a specfic region similar to (@see iFollowInTheRegion or @see iSeeTextInRegion) | ||
| 712 | * | ||
| 713 | * Example: Given I fill in "Hello" with "World" | ||
| 714 | * | ||
| 715 | * @Given /^I fill in "(?P<field>[^"]*)" with "(?P<value>[^"]*)" in the "(?P<region>[^"]*)" region$/ | ||
| 716 | */ | ||
| 717 | View Code Duplication | public function iFillinTheRegion($field, $value, $region) | |
| 731 | |||
| 732 | |||
| 733 | /** | ||
| 734 | * Asserts text in a specific region (an element identified by a CSS selector, a "data-title" attribute, | ||
| 735 | * or a named region mapped to a CSS selector via Behat configuration). | ||
| 736 | * Supports regular expressions in text value. | ||
| 737 | * | ||
| 738 | * Example: Given I should see "My Text" in the "header .login-form" region | ||
| 739 | * Example: Given I should not see "My Text" in the "My Login Form" region | ||
| 740 | * | ||
| 741 | * @Given /^I should (?P<negate>(?:(not |)))see "(?P<text>[^"]*)" in the "(?P<region>[^"]*)" region$/ | ||
| 742 | */ | ||
| 743 | public function iSeeTextInRegion($negate, $text, $region) | ||
| 777 | |||
| 778 | /** | ||
| 779 | * Selects the specified radio button | ||
| 780 | * | ||
| 781 | * @Given /^I select the "([^"]*)" radio button$/ | ||
| 782 | */ | ||
| 783 | public function iSelectTheRadioButton($radioLabel) | ||
| 792 | |||
| 793 | /** | ||
| 794 | * @Then /^the "([^"]*)" table should contain "([^"]*)"$/ | ||
| 795 | */ | ||
| 796 | View Code Duplication | public function theTableShouldContain($selector, $text) | |
| 803 | |||
| 804 | /** | ||
| 805 | * @Then /^the "([^"]*)" table should not contain "([^"]*)"$/ | ||
| 806 | */ | ||
| 807 | View Code Duplication | public function theTableShouldNotContain($selector, $text) | |
| 814 | |||
| 815 | /** | ||
| 816 | * @Given /^I click on "([^"]*)" in the "([^"]*)" table$/ | ||
| 817 | */ | ||
| 818 | View Code Duplication | public function iClickOnInTheTable($text, $selector) | |
| 826 | |||
| 827 | /** | ||
| 828 | * Finds the first visible table by various factors: | ||
| 829 | * - table[id] | ||
| 830 | * - table[title] | ||
| 831 | * - table *[class=title] | ||
| 832 | * - fieldset[data-name] table | ||
| 833 | * - table caption | ||
| 834 | * | ||
| 835 | * @return Behat\Mink\Element\NodeElement | ||
| 836 | */ | ||
| 837 | protected function getTable($selector) | ||
| 872 | |||
| 873 | /** | ||
| 874 | * Checks the order of two texts. | ||
| 875 | * Assumptions: the two texts appear in their conjunct parent element once | ||
| 876 | * @Then /^I should see the text "(?P<textBefore>(?:[^"]|\\")*)" (before|after) the text "(?P<textAfter>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/ | ||
| 877 | */ | ||
| 878 | public function theTextBeforeAfter($textBefore, $order, $textAfter, $element) | ||
| 896 | |||
| 897 | /** | ||
| 898 | * Wait until a certain amount of seconds till I see an element identified by a CSS selector. | ||
| 899 | * | ||
| 900 | * Example: Given I wait for 10 seconds until I see the ".css_element" element | ||
| 901 | * | ||
| 902 | * @Given /^I wait for (\d+) seconds until I see the "([^"]*)" element$/ | ||
| 903 | **/ | ||
| 904 | View Code Duplication | public function iWaitXUntilISee($wait, $selector) | |
| 918 | |||
| 919 | /** | ||
| 920 | * Wait until a particular element is visible, using a CSS selector. Useful for content loaded via AJAX, or only | ||
| 921 | * populated after JS execution. | ||
| 922 | * | ||
| 923 | * Example: Given I wait until I see the "header .login-form" element | ||
| 924 | * | ||
| 925 | * @Given /^I wait until I see the "([^"]*)" element$/ | ||
| 926 | */ | ||
| 927 | View Code Duplication | public function iWaitUntilISee($selector) | |
| 939 | |||
| 940 | /** | ||
| 941 | * Wait until a particular string is found on the page. Useful for content loaded via AJAX, or only populated after | ||
| 942 | * JS execution. | ||
| 943 | * | ||
| 944 | * Example: Given I wait until I see the text "Welcome back, John!" | ||
| 945 | * | ||
| 946 | * @Given /^I wait until I see the text "([^"]*)"$/ | ||
| 947 | */ | ||
| 948 | public function iWaitUntilISeeText($text) | ||
| 965 | |||
| 966 | /** | ||
| 967 | * @Given /^I scroll to the bottom$/ | ||
| 968 | */ | ||
| 969 | public function iScrollToBottom() | ||
| 974 | |||
| 975 | /** | ||
| 976 | * @Given /^I scroll to the top$/ | ||
| 977 | */ | ||
| 978 | public function iScrollToTop() | ||
| 982 | |||
| 983 | /** | ||
| 984 | * Scroll to a certain element by label. | ||
| 985 | * Requires an "id" attribute to uniquely identify the element in the document. | ||
| 986 | * | ||
| 987 | * Example: Given I scroll to the "Submit" button | ||
| 988 | * Example: Given I scroll to the "My Date" field | ||
| 989 | * | ||
| 990 | * @Given /^I scroll to the "([^"]*)" (field|link|button)$/ | ||
| 991 | */ | ||
| 992 | public function iScrollToField($locator, $type) | ||
| 1006 | |||
| 1007 | /** | ||
| 1008 | * Scroll to a certain element by CSS selector. | ||
| 1009 | * Requires an "id" attribute to uniquely identify the element in the document. | ||
| 1010 | * | ||
| 1011 | * Example: Given I scroll to the ".css_element" element | ||
| 1012 | * | ||
| 1013 | * @Given /^I scroll to the "(?P<locator>(?:[^"]|\\")*)" element$/ | ||
| 1014 | */ | ||
| 1015 | public function iScrollToElement($locator) | ||
| 1028 | |||
| 1029 | /** | ||
| 1030 | * Continuously poll the dom until callback returns true, code copied from | ||
| 1031 | * (@link http://docs.behat.org/cookbook/using_spin_functions.html) | ||
| 1032 | * If not found within a given wait period, timeout and throw error | ||
| 1033 | * | ||
| 1034 | * @param callback $lambda The function to run continuously | ||
| 1035 | * @param integer $wait Timeout in seconds | ||
| 1036 | * @return bool Returns true if the lambda returns successfully | ||
| 1037 | * @throws \Exception Thrown if the wait threshold is exceeded without the lambda successfully returning | ||
| 1038 | */ | ||
| 1039 | public function spin($lambda, $wait = 60) | ||
| 1061 | |||
| 1062 | |||
| 1063 | |||
| 1064 | /** | ||
| 1065 | * We have to catch exceptions and log somehow else otherwise behat falls over | ||
| 1066 | */ | ||
| 1067 | protected function logException($e) | ||
| 1071 | } | ||
| 1072 | 
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: