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() |
||
226 | |||
227 | /** |
||
228 | * Take screenshot when step fails. |
||
229 | * Works only with Selenium2Driver. |
||
230 | * |
||
231 | * @AfterStep |
||
232 | */ |
||
233 | public function takeScreenshotAfterFailedStep(StepEvent $event) |
||
243 | |||
244 | /** |
||
245 | * Close modal dialog if test scenario fails on CMS page |
||
246 | * |
||
247 | * @AfterScenario |
||
248 | */ |
||
249 | public function closeModalDialog(ScenarioEvent $event) |
||
269 | |||
270 | /** |
||
271 | * Delete any created files and folders from assets directory |
||
272 | * |
||
273 | * @AfterScenario @assets |
||
274 | */ |
||
275 | public function cleanAssetsAfterScenario(ScenarioEvent $event) |
||
282 | |||
283 | public function takeScreenshot(StepEvent $event) |
||
323 | |||
324 | /** |
||
325 | * @Then /^I should be redirected to "([^"]+)"/ |
||
326 | */ |
||
327 | public function stepIShouldBeRedirectedTo($url) |
||
339 | |||
340 | /** |
||
341 | * @Given /^the page can't be found/ |
||
342 | */ |
||
343 | public function stepPageCantBeFound() |
||
353 | |||
354 | /** |
||
355 | * @Given /^I wait (?:for )?([\d\.]+) second(?:s?)$/ |
||
356 | */ |
||
357 | public function stepIWaitFor($secs) |
||
361 | |||
362 | /** |
||
363 | * @Given /^I press the "([^"]*)" button$/ |
||
364 | */ |
||
365 | public function stepIPressTheButton($button) |
||
378 | |||
379 | /** |
||
380 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
381 | * Example1: I press the "Remove current combo" button, confirming the dialog |
||
382 | * Example2: I follow the "Remove current combo" link, confirming the dialog |
||
383 | * |
||
384 | * @Given /^I (?:press|follow) the "([^"]*)" (?:button|link), confirming the dialog$/ |
||
385 | */ |
||
386 | public function stepIPressTheButtonConfirmingTheDialog($button) |
||
391 | |||
392 | /** |
||
393 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
394 | * Example: I follow the "Remove current combo" link, dismissing the dialog |
||
395 | * |
||
396 | * @Given /^I (?:press|follow) the "([^"]*)" (?:button|link), dismissing the dialog$/ |
||
397 | */ |
||
398 | public function stepIPressTheButtonDismissingTheDialog($button) |
||
403 | |||
404 | /** |
||
405 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element$/ |
||
406 | */ |
||
407 | public function iClickInTheElement($clickType, $text, $selector) |
||
421 | |||
422 | /** |
||
423 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
424 | * Example: I click "Delete" in the ".actions" element, confirming the dialog |
||
425 | * |
||
426 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, confirming the dialog$/ |
||
427 | */ |
||
428 | public function iClickInTheElementConfirmingTheDialog($clickType, $text, $selector) |
||
433 | /** |
||
434 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
435 | * Example: I click "Delete" in the ".actions" element, dismissing the dialog |
||
436 | * |
||
437 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, dismissing the dialog$/ |
||
438 | */ |
||
439 | public function iClickInTheElementDismissingTheDialog($clickType, $text, $selector) |
||
444 | |||
445 | /** |
||
446 | * @Given /^I type "([^"]*)" into the dialog$/ |
||
447 | */ |
||
448 | public function iTypeIntoTheDialog($data) |
||
455 | |||
456 | /** |
||
457 | * @Given /^I confirm the dialog$/ |
||
458 | */ |
||
459 | public function iConfirmTheDialog() |
||
464 | |||
465 | /** |
||
466 | * @Given /^I dismiss the dialog$/ |
||
467 | */ |
||
468 | public function iDismissTheDialog() |
||
473 | |||
474 | /** |
||
475 | * @Given /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)" with HTML5$/ |
||
476 | */ |
||
477 | public function iAttachTheFileTo($field, $path) |
||
492 | |||
493 | /** |
||
494 | * Select an individual input from within a group, matched by the top-most label. |
||
495 | * |
||
496 | * @Given /^I select "([^"]*)" from "([^"]*)" input group$/ |
||
497 | */ |
||
498 | public function iSelectFromInputGroup($value, $labelText) |
||
536 | |||
537 | /** |
||
538 | * Pauses the scenario until the user presses a key. Useful when debugging a scenario. |
||
539 | * |
||
540 | * @Then /^(?:|I )put a breakpoint$/ |
||
541 | */ |
||
542 | public function iPutABreakpoint() |
||
551 | |||
552 | /** |
||
553 | * Transforms relative time statements compatible with strtotime(). |
||
554 | * Example: "time of 1 hour ago" might return "22:00:00" if its currently "23:00:00". |
||
555 | * Customize through {@link setTimeFormat()}. |
||
556 | * |
||
557 | * @Transform /^(?:(the|a)) time of (?<val>.*)$/ |
||
558 | */ |
||
559 | View Code Duplication | public function castRelativeToAbsoluteTime($prefix, $val) |
|
570 | |||
571 | /** |
||
572 | * Transforms relative date and time statements compatible with strtotime(). |
||
573 | * Example: "datetime of 2 days ago" might return "2013-10-10 22:00:00" if its currently |
||
574 | * the 12th of October 2013. Customize through {@link setDatetimeFormat()}. |
||
575 | * |
||
576 | * @Transform /^(?:(the|a)) datetime of (?<val>.*)$/ |
||
577 | */ |
||
578 | View Code Duplication | public function castRelativeToAbsoluteDatetime($prefix, $val) |
|
589 | |||
590 | /** |
||
591 | * Transforms relative date statements compatible with strtotime(). |
||
592 | * Example: "date 2 days ago" might return "2013-10-10" if its currently |
||
593 | * the 12th of October 2013. Customize through {@link setDateFormat()}. |
||
594 | * |
||
595 | * @Transform /^(?:(the|a)) date of (?<val>.*)$/ |
||
596 | */ |
||
597 | View Code Duplication | public function castRelativeToAbsoluteDate($prefix, $val) |
|
608 | |||
609 | public function getDateFormat() |
||
613 | |||
614 | public function setDateFormat($format) |
||
618 | |||
619 | public function getTimeFormat() |
||
623 | |||
624 | public function setTimeFormat($format) |
||
628 | |||
629 | public function getDatetimeFormat() |
||
633 | |||
634 | public function setDatetimeFormat($format) |
||
638 | |||
639 | /** |
||
640 | * Checks that field with specified in|name|label|value is disabled. |
||
641 | * Example: Then the field "Email" should be disabled |
||
642 | * Example: Then the "Email" field should be disabled |
||
643 | * |
||
644 | * @Then /^the "(?P<name>(?:[^"]|\\")*)" (?P<type>(?:(field|button))) should (?P<negate>(?:(not |)))be disabled/ |
||
645 | * @Then /^the (?P<type>(?:(field|button))) "(?P<name>(?:[^"]|\\")*)" should (?P<negate>(?:(not |)))be disabled/ |
||
646 | */ |
||
647 | public function stepFieldShouldBeDisabled($name, $type, $negate) |
||
667 | |||
668 | /** |
||
669 | * Checks that checkbox with specified in|name|label|value is enabled. |
||
670 | * Example: Then the field "Email" should be enabled |
||
671 | * Example: Then the "Email" field should be enabled |
||
672 | * |
||
673 | * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should be enabled/ |
||
674 | * @Then /^the field "(?P<field>(?:[^"]|\\")*)" should be enabled/ |
||
675 | */ |
||
676 | public function stepFieldShouldBeEnabled($field) |
||
686 | |||
687 | /** |
||
688 | * Clicks a link in a specific region (an element identified by a CSS selector, a "data-title" attribute, |
||
689 | * or a named region mapped to a CSS selector via Behat configuration). |
||
690 | * |
||
691 | * Example: Given I follow "Select" in the "header .login-form" region |
||
692 | * Example: Given I follow "Select" in the "My Login Form" region |
||
693 | * |
||
694 | * @Given /^I (?:follow|click) "(?P<link>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
695 | */ |
||
696 | View Code Duplication | public function iFollowInTheRegion($link, $region) |
|
710 | |||
711 | /** |
||
712 | * Fills in a field in a specfic region similar to (@see iFollowInTheRegion or @see iSeeTextInRegion) |
||
713 | * |
||
714 | * Example: Given I fill in "Hello" with "World" |
||
715 | * |
||
716 | * @Given /^I fill in "(?P<field>[^"]*)" with "(?P<value>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
717 | */ |
||
718 | View Code Duplication | public function iFillinTheRegion($field, $value, $region) |
|
732 | |||
733 | |||
734 | /** |
||
735 | * Asserts text in a specific region (an element identified by a CSS selector, a "data-title" attribute, |
||
736 | * or a named region mapped to a CSS selector via Behat configuration). |
||
737 | * Supports regular expressions in text value. |
||
738 | * |
||
739 | * Example: Given I should see "My Text" in the "header .login-form" region |
||
740 | * Example: Given I should not see "My Text" in the "My Login Form" region |
||
741 | * |
||
742 | * @Given /^I should (?P<negate>(?:(not |)))see "(?P<text>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
743 | */ |
||
744 | public function iSeeTextInRegion($negate, $text, $region) |
||
778 | |||
779 | /** |
||
780 | * Selects the specified radio button |
||
781 | * |
||
782 | * @Given /^I select the "([^"]*)" radio button$/ |
||
783 | */ |
||
784 | public function iSelectTheRadioButton($radioLabel) |
||
793 | |||
794 | /** |
||
795 | * @Then /^the "([^"]*)" table should contain "([^"]*)"$/ |
||
796 | */ |
||
797 | View Code Duplication | public function theTableShouldContain($selector, $text) |
|
804 | |||
805 | /** |
||
806 | * @Then /^the "([^"]*)" table should not contain "([^"]*)"$/ |
||
807 | */ |
||
808 | View Code Duplication | public function theTableShouldNotContain($selector, $text) |
|
815 | |||
816 | /** |
||
817 | * @Given /^I click on "([^"]*)" in the "([^"]*)" table$/ |
||
818 | */ |
||
819 | View Code Duplication | public function iClickOnInTheTable($text, $selector) |
|
827 | |||
828 | /** |
||
829 | * Finds the first visible table by various factors: |
||
830 | * - table[id] |
||
831 | * - table[title] |
||
832 | * - table *[class=title] |
||
833 | * - fieldset[data-name] table |
||
834 | * - table caption |
||
835 | * |
||
836 | * @return Behat\Mink\Element\NodeElement |
||
837 | */ |
||
838 | protected function getTable($selector) |
||
874 | |||
875 | /** |
||
876 | * Checks the order of two texts. |
||
877 | * Assumptions: the two texts appear in their conjunct parent element once |
||
878 | * @Then /^I should see the text "(?P<textBefore>(?:[^"]|\\")*)" (before|after) the text "(?P<textAfter>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/ |
||
879 | */ |
||
880 | public function theTextBeforeAfter($textBefore, $order, $textAfter, $element) |
||
898 | |||
899 | /** |
||
900 | * Wait until a certain amount of seconds till I see an element identified by a CSS selector. |
||
901 | * |
||
902 | * Example: Given I wait for 10 seconds until I see the ".css_element" element |
||
903 | * |
||
904 | * @Given /^I wait for (\d+) seconds until I see the "([^"]*)" element$/ |
||
905 | **/ |
||
906 | View Code Duplication | public function iWaitXUntilISee($wait, $selector) |
|
920 | |||
921 | /** |
||
922 | * Wait until a particular element is visible, using a CSS selector. Useful for content loaded via AJAX, or only |
||
923 | * populated after JS execution. |
||
924 | * |
||
925 | * Example: Given I wait until I see the "header .login-form" element |
||
926 | * |
||
927 | * @Given /^I wait until I see the "([^"]*)" element$/ |
||
928 | */ |
||
929 | View Code Duplication | public function iWaitUntilISee($selector) |
|
941 | |||
942 | /** |
||
943 | * Wait until a particular string is found on the page. Useful for content loaded via AJAX, or only populated after |
||
944 | * JS execution. |
||
945 | * |
||
946 | * Example: Given I wait until I see the text "Welcome back, John!" |
||
947 | * |
||
948 | * @Given /^I wait until I see the text "([^"]*)"$/ |
||
949 | */ |
||
950 | public function iWaitUntilISeeText($text) |
||
967 | |||
968 | /** |
||
969 | * @Given /^I scroll to the bottom$/ |
||
970 | */ |
||
971 | public function iScrollToBottom() |
||
976 | |||
977 | /** |
||
978 | * @Given /^I scroll to the top$/ |
||
979 | */ |
||
980 | public function iScrollToTop() |
||
984 | |||
985 | /** |
||
986 | * Scroll to a certain element by label. |
||
987 | * Requires an "id" attribute to uniquely identify the element in the document. |
||
988 | * |
||
989 | * Example: Given I scroll to the "Submit" button |
||
990 | * Example: Given I scroll to the "My Date" field |
||
991 | * |
||
992 | * @Given /^I scroll to the "([^"]*)" (field|link|button)$/ |
||
993 | */ |
||
994 | public function iScrollToField($locator, $type) |
||
1008 | |||
1009 | /** |
||
1010 | * Scroll to a certain element by CSS selector. |
||
1011 | * Requires an "id" attribute to uniquely identify the element in the document. |
||
1012 | * |
||
1013 | * Example: Given I scroll to the ".css_element" element |
||
1014 | * |
||
1015 | * @Given /^I scroll to the "(?P<locator>(?:[^"]|\\")*)" element$/ |
||
1016 | */ |
||
1017 | public function iScrollToElement($locator) |
||
1030 | |||
1031 | /** |
||
1032 | * Continuously poll the dom until callback returns true, code copied from |
||
1033 | * (@link http://docs.behat.org/cookbook/using_spin_functions.html) |
||
1034 | * If not found within a given wait period, timeout and throw error |
||
1035 | * |
||
1036 | * @param callback $lambda The function to run continuously |
||
1037 | * @param integer $wait Timeout in seconds |
||
1038 | * @return bool Returns true if the lambda returns successfully |
||
1039 | * @throws \Exception Thrown if the wait threshold is exceeded without the lambda successfully returning |
||
1040 | */ |
||
1041 | public function spin($lambda, $wait = 60) |
||
1063 | |||
1064 | |||
1065 | |||
1066 | /** |
||
1067 | * We have to catch exceptions and log somehow else otherwise behat falls over |
||
1068 | */ |
||
1069 | protected function logException($e) |
||
1073 | } |
||
1074 |
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: