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 EmailContext 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 EmailContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class EmailContext extends BehatContext |
||
24 | { |
||
25 | protected $context; |
||
26 | |||
27 | protected $mailer; |
||
28 | |||
29 | /** |
||
30 | * Stored to simplify later assertions |
||
31 | */ |
||
32 | protected $lastMatchedEmail; |
||
33 | |||
34 | /** |
||
35 | * Initializes context. |
||
36 | * Every scenario gets it's own context object. |
||
37 | * |
||
38 | * @param array $parameters context parameters (set them up through behat.yml) |
||
39 | */ |
||
40 | public function __construct(array $parameters) |
||
41 | { |
||
42 | // Initialize your context here |
||
43 | $this->context = $parameters; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Get Mink session from MinkContext |
||
48 | */ |
||
49 | public function getSession($name = null) |
||
53 | |||
54 | /** |
||
55 | * @BeforeScenario |
||
56 | */ |
||
57 | public function before(ScenarioEvent $event) |
||
58 | { |
||
59 | // Also set through the 'supportbehat' extension |
||
60 | // to ensure its available both in CLI execution and the tested browser session |
||
61 | $this->mailer = new \SilverStripe\BehatExtension\Utility\TestMailer(); |
||
62 | \Email::set_mailer($this->mailer); |
||
63 | \Config::inst()->update("Email","send_all_emails_to", null); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @Given /^there should (not |)be an email (to|from) "([^"]*)"$/ |
||
68 | */ |
||
69 | public function thereIsAnEmailFromTo($negate, $direction, $email) |
||
81 | |||
82 | /** |
||
83 | * @Given /^there should (not |)be an email (to|from) "([^"]*)" titled "([^"]*)"$/ |
||
84 | */ |
||
85 | public function thereIsAnEmailFromToTitled($negate, $direction, $email, $subject) |
||
86 | { |
||
87 | $to = ($direction == 'to') ? $email : null; |
||
88 | $from = ($direction == 'from') ? $email : null; |
||
89 | $match = $this->mailer->findEmail($to, $from, $subject); |
||
90 | $allMails = $this->mailer->findEmails($to, $from); |
||
91 | $allTitles = $allMails ? '"' . implode('","', array_map(function($email) {return $email->Subject;}, $allMails)) . '"' : null; |
||
92 | if(trim($negate)) { |
||
93 | assertNull($match); |
||
94 | } else { |
||
95 | $msg = sprintf( |
||
96 | 'Could not find email %s "%s" titled "%s".', |
||
97 | $direction, |
||
98 | $email, |
||
99 | $subject |
||
100 | ); |
||
101 | if($allTitles) { |
||
102 | $msg .= ' Existing emails: ' . $allTitles; |
||
103 | } |
||
104 | assertNotNull($match,$msg); |
||
105 | } |
||
106 | $this->lastMatchedEmail = $match; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Example: Given the email should contain "Thank you for registering!". |
||
111 | * Assumes an email has been identified by a previous step, |
||
112 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
113 | * |
||
114 | * @Given /^the email should (not |)contain "([^"]*)"$/ |
||
115 | */ |
||
116 | public function thereTheEmailContains($negate, $content) |
||
136 | |||
137 | /** |
||
138 | * Example: Given the email contains "Thank you for <strong>registering!<strong>". |
||
139 | * Then the email should contain plain text "Thank you for registering!" |
||
140 | * Assumes an email has been identified by a previous step, |
||
141 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
142 | * |
||
143 | * @Given /^the email should contain plain text "([^"]*)"$/ |
||
144 | */ |
||
145 | public function thereTheEmailContainsPlainText($content) |
||
158 | |||
159 | /** |
||
160 | * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/ |
||
161 | */ |
||
162 | View Code Duplication | public function iGoToInTheEmailTo($linkSelector, $direction, $email) |
|
177 | |||
178 | /** |
||
179 | * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)" titled "([^"]*)"$/ |
||
180 | */ |
||
181 | View Code Duplication | public function iGoToInTheEmailToTitled($linkSelector, $direction, $email, $title) |
|
195 | |||
196 | /** |
||
197 | * Assumes an email has been identified by a previous step, |
||
198 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
199 | * |
||
200 | * @When /^I click on the "([^"]*)" link in the email"$/ |
||
201 | */ |
||
202 | public function iGoToInTheEmail($linkSelector) |
||
217 | |||
218 | /** |
||
219 | * @Given /^I clear all emails$/ |
||
220 | */ |
||
221 | public function iClearAllEmails() |
||
226 | |||
227 | /** |
||
228 | * Example: Then the email should contain the following data: |
||
229 | * | row1 | |
||
230 | * | row2 | |
||
231 | * Assumes an email has been identified by a previous step. |
||
232 | * @Then /^the email should (not |)contain the following data:$/ |
||
233 | */ |
||
234 | public function theEmailContainFollowingData($negate, TableNode $table) { |
||
262 | |||
263 | /** |
||
264 | * @Then /^there should (not |)be an email titled "([^"]*)"$/ |
||
265 | */ |
||
266 | public function thereIsAnEmailTitled($negate, $subject) |
||
280 | |||
281 | /** |
||
282 | * @Then /^the email should (not |)be sent from "([^"]*)"$/ |
||
283 | */ |
||
284 | View Code Duplication | public function theEmailSentFrom($negate, $from) |
|
297 | |||
298 | /** |
||
299 | * @Then /^the email should (not |)be sent to "([^"]*)"$/ |
||
300 | */ |
||
301 | View Code Duplication | public function theEmailSentTo($negate, $to) |
|
314 | } |
||
315 |
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: