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 |
||
| 27 | class BasicContext extends BehatContext |
||
| 28 | { |
||
| 29 | protected $context; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Date format in date() syntax |
||
| 33 | * @var String |
||
| 34 | */ |
||
| 35 | protected $dateFormat = 'Y-m-d'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Time format in date() syntax |
||
| 39 | * @var String |
||
| 40 | */ |
||
| 41 | protected $timeFormat = 'H:i:s'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Date/time format in date() syntax |
||
| 45 | * @var String |
||
| 46 | */ |
||
| 47 | protected $datetimeFormat = 'Y-m-d H:i:s'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Initializes context. |
||
| 51 | * Every scenario gets it's own context object. |
||
| 52 | * |
||
| 53 | * @param array $parameters context parameters (set them up through behat.yml) |
||
| 54 | */ |
||
| 55 | 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) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @AfterStep ~@modal |
||
| 71 | * |
||
| 72 | * Excluding scenarios with @modal tag is required, |
||
| 73 | * because modal dialogs stop any JS interaction |
||
| 74 | */ |
||
| 75 | public function appendErrorHandlerBeforeStep(StepEvent $event) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @AfterStep ~@modal |
||
| 102 | * |
||
| 103 | * Excluding scenarios with @modal tag is required, |
||
| 104 | * because modal dialogs stop any JS interaction |
||
| 105 | */ |
||
| 106 | public function readErrorHandlerAfterStep(StepEvent $event) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Hook into jQuery ajaxStart, ajaxSuccess and ajaxComplete events. |
||
| 132 | * Prepare __ajaxStatus() functions and attach them to these handlers. |
||
| 133 | * Event handlers are removed after one run. |
||
| 134 | * |
||
| 135 | * @BeforeStep |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function handleAjaxBeforeStep(StepEvent $event) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Wait for the __ajaxStatus()to return anything but 'waiting'. |
||
| 178 | * Don't wait longer than 5 seconds. |
||
| 179 | * |
||
| 180 | * Don't unregister handler if we're dealing with modal windows |
||
| 181 | * |
||
| 182 | * @AfterStep ~@modal |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function handleAjaxAfterStep(StepEvent $event) { |
|
| 207 | |||
| 208 | public function handleAjaxTimeout() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Take screenshot when step fails. |
||
| 222 | * Works only with Selenium2Driver. |
||
| 223 | * |
||
| 224 | * @AfterStep |
||
| 225 | */ |
||
| 226 | public function takeScreenshotAfterFailedStep(StepEvent $event) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Close modal dialog if test scenario fails on CMS page |
||
| 238 | * |
||
| 239 | * @AfterScenario |
||
| 240 | */ |
||
| 241 | public function closeModalDialog(ScenarioEvent $event) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Delete any created files and folders from assets directory |
||
| 263 | * |
||
| 264 | * @AfterScenario @assets |
||
| 265 | */ |
||
| 266 | public function cleanAssetsAfterScenario(ScenarioEvent $event) { |
||
| 272 | |||
| 273 | public function takeScreenshot(StepEvent $event) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @Then /^I should be redirected to "([^"]+)"/ |
||
| 313 | */ |
||
| 314 | public function stepIShouldBeRedirectedTo($url) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @Given /^the page can't be found/ |
||
| 328 | */ |
||
| 329 | public function stepPageCantBeFound() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @Given /^I wait (?:for )?([\d\.]+) second(?:s?)$/ |
||
| 341 | */ |
||
| 342 | public function stepIWaitFor($secs) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @Given /^I press the "([^"]*)" button$/ |
||
| 348 | */ |
||
| 349 | public function stepIPressTheButton($button) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
| 362 | * Example1: I press the "Remove current combo" button, confirming the dialog |
||
| 363 | * Example2: I follow the "Remove current combo" link, confirming the dialog |
||
| 364 | * |
||
| 365 | * @Given /^I (?:press|follow) the "([^"]*)" (?:button|link), confirming the dialog$/ |
||
| 366 | */ |
||
| 367 | public function stepIPressTheButtonConfirmingTheDialog($button) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
| 374 | * Example: I follow the "Remove current combo" link, dismissing the dialog |
||
| 375 | * |
||
| 376 | * @Given /^I (?:press|follow) the "([^"]*)" (?:button|link), dismissing the dialog$/ |
||
| 377 | */ |
||
| 378 | public function stepIPressTheButtonDismissingTheDialog($button) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element$/ |
||
| 385 | */ |
||
| 386 | public function iClickInTheElement($clickType, $text, $selector) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
| 402 | * Example: I click "Delete" in the ".actions" element, confirming the dialog |
||
| 403 | * |
||
| 404 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, confirming the dialog$/ |
||
| 405 | */ |
||
| 406 | public function iClickInTheElementConfirmingTheDialog($clickType, $text, $selector) { |
||
| 410 | /** |
||
| 411 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
| 412 | * Example: I click "Delete" in the ".actions" element, dismissing the dialog |
||
| 413 | * |
||
| 414 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, dismissing the dialog$/ |
||
| 415 | */ |
||
| 416 | public function iClickInTheElementDismissingTheDialog($clickType, $text, $selector) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @Given /^I type "([^"]*)" into the dialog$/ |
||
| 423 | */ |
||
| 424 | public function iTypeIntoTheDialog($data) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @Given /^I confirm the dialog$/ |
||
| 433 | */ |
||
| 434 | public function iConfirmTheDialog() { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @Given /^I dismiss the dialog$/ |
||
| 441 | */ |
||
| 442 | public function iDismissTheDialog() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @Given /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)" with HTML5$/ |
||
| 449 | */ |
||
| 450 | public function iAttachTheFileTo($field, $path) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Select an individual input from within a group, matched by the top-most label. |
||
| 467 | * |
||
| 468 | * @Given /^I select "([^"]*)" from "([^"]*)" input group$/ |
||
| 469 | */ |
||
| 470 | public function iSelectFromInputGroup($value, $labelText) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Pauses the scenario until the user presses a key. Useful when debugging a scenario. |
||
| 496 | * |
||
| 497 | * @Then /^(?:|I )put a breakpoint$/ |
||
| 498 | */ |
||
| 499 | public function iPutABreakpoint() { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Transforms relative time statements compatible with strtotime(). |
||
| 509 | * Example: "time of 1 hour ago" might return "22:00:00" if its currently "23:00:00". |
||
| 510 | * Customize through {@link setTimeFormat()}. |
||
| 511 | * |
||
| 512 | * @Transform /^(?:(the|a)) time of (?<val>.*)$/ |
||
| 513 | */ |
||
| 514 | View Code Duplication | public function castRelativeToAbsoluteTime($prefix, $val) { |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Transforms relative date and time statements compatible with strtotime(). |
||
| 527 | * Example: "datetime of 2 days ago" might return "2013-10-10 22:00:00" if its currently |
||
| 528 | * the 12th of October 2013. Customize through {@link setDatetimeFormat()}. |
||
| 529 | * |
||
| 530 | * @Transform /^(?:(the|a)) datetime of (?<val>.*)$/ |
||
| 531 | */ |
||
| 532 | View Code Duplication | public function castRelativeToAbsoluteDatetime($prefix, $val) { |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Transforms relative date statements compatible with strtotime(). |
||
| 545 | * Example: "date 2 days ago" might return "2013-10-10" if its currently |
||
| 546 | * the 12th of October 2013. Customize through {@link setDateFormat()}. |
||
| 547 | * |
||
| 548 | * @Transform /^(?:(the|a)) date of (?<val>.*)$/ |
||
| 549 | */ |
||
| 550 | View Code Duplication | public function castRelativeToAbsoluteDate($prefix, $val) { |
|
| 560 | |||
| 561 | public function getDateFormat() { |
||
| 564 | |||
| 565 | public function setDateFormat($format) { |
||
| 568 | |||
| 569 | public function getTimeFormat() { |
||
| 572 | |||
| 573 | public function setTimeFormat($format) { |
||
| 576 | |||
| 577 | public function getDatetimeFormat() { |
||
| 580 | |||
| 581 | public function setDatetimeFormat($format) { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Checks that field with specified in|name|label|value is disabled. |
||
| 587 | * Example: Then the field "Email" should be disabled |
||
| 588 | * Example: Then the "Email" field should be disabled |
||
| 589 | * |
||
| 590 | * @Then /^the "(?P<name>(?:[^"]|\\")*)" (?P<type>(?:(field|button))) should (?P<negate>(?:(not |)))be disabled/ |
||
| 591 | * @Then /^the (?P<type>(?:(field|button))) "(?P<name>(?:[^"]|\\")*)" should (?P<negate>(?:(not |)))be disabled/ |
||
| 592 | */ |
||
| 593 | public function stepFieldShouldBeDisabled($name, $type, $negate) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Checks that checkbox with specified in|name|label|value is enabled. |
||
| 615 | * Example: Then the field "Email" should be enabled |
||
| 616 | * Example: Then the "Email" field should be enabled |
||
| 617 | * |
||
| 618 | * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should be enabled/ |
||
| 619 | * @Then /^the field "(?P<field>(?:[^"]|\\")*)" should be enabled/ |
||
| 620 | */ |
||
| 621 | public function stepFieldShouldBeEnabled($field) { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Clicks a link in a specific region (an element identified by a CSS selector, a "data-title" attribute, |
||
| 633 | * or a named region mapped to a CSS selector via Behat configuration). |
||
| 634 | * |
||
| 635 | * Example: Given I follow "Select" in the "header .login-form" region |
||
| 636 | * Example: Given I follow "Select" in the "My Login Form" region |
||
| 637 | * |
||
| 638 | * @Given /^I (?:follow|click) "(?P<link>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
| 639 | */ |
||
| 640 | View Code Duplication | public function iFollowInTheRegion($link, $region) { |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Fills in a field in a specfic region similar to (@see iFollowInTheRegion or @see iSeeTextInRegion) |
||
| 656 | * |
||
| 657 | * Example: Given I fill in "Hello" with "World" |
||
| 658 | * |
||
| 659 | * @Given /^I fill in "(?P<field>[^"]*)" with "(?P<value>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
| 660 | */ |
||
| 661 | View Code Duplication | public function iFillinTheRegion($field, $value, $region){ |
|
| 674 | |||
| 675 | |||
| 676 | /** |
||
| 677 | * Asserts text in a specific region (an element identified by a CSS selector, a "data-title" attribute, |
||
| 678 | * or a named region mapped to a CSS selector via Behat configuration). |
||
| 679 | * Supports regular expressions in text value. |
||
| 680 | * |
||
| 681 | * Example: Given I should see "My Text" in the "header .login-form" region |
||
| 682 | * Example: Given I should not see "My Text" in the "My Login Form" region |
||
| 683 | * |
||
| 684 | * @Given /^I should (?P<negate>(?:(not |)))see "(?P<text>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
| 685 | */ |
||
| 686 | public function iSeeTextInRegion($negate, $text, $region) { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Selects the specified radio button |
||
| 723 | * |
||
| 724 | * @Given /^I select the "([^"]*)" radio button$/ |
||
| 725 | */ |
||
| 726 | public function iSelectTheRadioButton($radioLabel) { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @Then /^the "([^"]*)" table should contain "([^"]*)"$/ |
||
| 737 | */ |
||
| 738 | public function theTableShouldContain($selector, $text) { |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @Then /^the "([^"]*)" table should not contain "([^"]*)"$/ |
||
| 747 | */ |
||
| 748 | public function theTableShouldNotContain($selector, $text) { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @Given /^I click on "([^"]*)" in the "([^"]*)" table$/ |
||
| 757 | */ |
||
| 758 | public function iClickOnInTheTable($text, $selector) { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Finds the first visible table by various factors: |
||
| 768 | * - table[id] |
||
| 769 | * - table[title] |
||
| 770 | * - table *[class=title] |
||
| 771 | * - fieldset[data-name] table |
||
| 772 | * - table caption |
||
| 773 | * |
||
| 774 | * @return Behat\Mink\Element\NodeElement |
||
| 775 | */ |
||
| 776 | protected function getTable($selector) { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Checks the order of two texts. |
||
| 813 | * Assumptions: the two texts appear in their conjunct parent element once |
||
| 814 | * @Then /^I should see the text "(?P<textBefore>(?:[^"]|\\")*)" (before|after) the text "(?P<textAfter>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/ |
||
| 815 | */ |
||
| 816 | public function theTextBeforeAfter($textBefore, $order, $textAfter, $element) { |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Wait until a certain amount of seconds till I see an element identified by a CSS selector. |
||
| 836 | * |
||
| 837 | * Example: Given I wait for 10 seconds until I see the ".css_element" element |
||
| 838 | * |
||
| 839 | * @Given /^I wait for (\d+) seconds until I see the "([^"]*)" element$/ |
||
| 840 | **/ |
||
| 841 | View Code Duplication | public function iWaitXUntilISee($wait, $selector) { |
|
| 854 | |||
| 855 | /** |
||
| 856 | * Wait until a particular element is visible, using a CSS selector. Useful for content loaded via AJAX, or only |
||
| 857 | * populated after JS execution. |
||
| 858 | * |
||
| 859 | * Example: Given I wait until I see the "header .login-form" element |
||
| 860 | * |
||
| 861 | * @Given /^I wait until I see the "([^"]*)" element$/ |
||
| 862 | */ |
||
| 863 | View Code Duplication | public function iWaitUntilISee($selector) { |
|
| 874 | |||
| 875 | /** |
||
| 876 | * Wait until a particular string is found on the page. Useful for content loaded via AJAX, or only populated after |
||
| 877 | * JS execution. |
||
| 878 | * |
||
| 879 | * Example: Given I wait until I see the text "Welcome back, John!" |
||
| 880 | * |
||
| 881 | * @Given /^I wait until I see the text "([^"]*)"$/ |
||
| 882 | */ |
||
| 883 | public function iWaitUntilISeeText($text){ |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @Given /^I scroll to the bottom$/ |
||
| 902 | */ |
||
| 903 | public function iScrollToBottom() { |
||
| 907 | |||
| 908 | /** |
||
| 909 | * @Given /^I scroll to the top$/ |
||
| 910 | */ |
||
| 911 | public function iScrollToTop() { |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Scroll to a certain element by label. |
||
| 917 | * Requires an "id" attribute to uniquely identify the element in the document. |
||
| 918 | * |
||
| 919 | * Example: Given I scroll to the "Submit" button |
||
| 920 | * Example: Given I scroll to the "My Date" field |
||
| 921 | * |
||
| 922 | * @Given /^I scroll to the "([^"]*)" (field|link|button)$/ |
||
| 923 | */ |
||
| 924 | public function iScrollToField($locator, $type) { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Scroll to a certain element by CSS selector. |
||
| 940 | * Requires an "id" attribute to uniquely identify the element in the document. |
||
| 941 | * |
||
| 942 | * Example: Given I scroll to the ".css_element" element |
||
| 943 | * |
||
| 944 | * @Given /^I scroll to the "(?P<locator>(?:[^"]|\\")*)" element$/ |
||
| 945 | */ |
||
| 946 | public function iScrollToElement($locator) { |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Continuously poll the dom until callback returns true, code copied from |
||
| 961 | * (@link http://docs.behat.org/cookbook/using_spin_functions.html) |
||
| 962 | * If not found within a given wait period, timeout and throw error |
||
| 963 | * |
||
| 964 | * @param callback $lambda The function to run continuously |
||
| 965 | * @param integer $wait Timeout in seconds |
||
| 966 | * @return bool Returns true if the lambda returns successfully |
||
| 967 | * @throws \Exception Thrown if the wait threshold is exceeded without the lambda successfully returning |
||
| 968 | */ |
||
| 969 | public function spin($lambda, $wait = 60) { |
||
| 990 | |||
| 991 | |||
| 992 | |||
| 993 | /** |
||
| 994 | * We have to catch exceptions and log somehow else otherwise behat falls over |
||
| 995 | */ |
||
| 996 | protected function logException($e){ |
||
| 999 | |||
| 1000 | } |
||
| 1001 |
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: