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 |
||
22 | class EmailContext extends BehatContext |
||
23 | { |
||
24 | protected $context; |
||
25 | |||
26 | protected $mailer; |
||
27 | |||
28 | /** |
||
29 | * Stored to simplify later assertions |
||
30 | */ |
||
31 | protected $lastMatchedEmail; |
||
32 | |||
33 | /** |
||
34 | * Initializes context. |
||
35 | * Every scenario gets it's own context object. |
||
36 | * |
||
37 | * @param array $parameters context parameters (set them up through behat.yml) |
||
38 | */ |
||
39 | public function __construct(array $parameters) |
||
40 | { |
||
41 | // Initialize your context here |
||
42 | $this->context = $parameters; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Get Mink session from MinkContext |
||
47 | */ |
||
48 | public function getSession($name = null) |
||
49 | { |
||
50 | return $this->getMainContext()->getSession($name); |
||
|
|||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @BeforeScenario |
||
55 | */ |
||
56 | public function before(ScenarioEvent $event) |
||
57 | { |
||
58 | // Also set through the 'supportbehat' extension |
||
59 | // to ensure its available both in CLI execution and the tested browser session |
||
60 | $this->mailer = new \SilverStripe\BehatExtension\Utility\TestMailer(); |
||
61 | \Email::set_mailer($this->mailer); |
||
62 | \Config::inst()->update("Email","send_all_emails_to", null); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @Given /^there should (not |)be an email (to|from) "([^"]*)"$/ |
||
67 | */ |
||
68 | public function thereIsAnEmailFromTo($negate, $direction, $email) |
||
69 | { |
||
70 | $to = ($direction == 'to') ? $email : null; |
||
71 | $from = ($direction == 'from') ? $email : null; |
||
72 | $match = $this->mailer->findEmail($to, $from); |
||
73 | if(trim($negate)) { |
||
74 | assertNull($match); |
||
75 | } else { |
||
76 | assertNotNull($match); |
||
77 | } |
||
78 | $this->lastMatchedEmail = $match; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @Given /^there should (not |)be an email (to|from) "([^"]*)" titled "([^"]*)"$/ |
||
83 | */ |
||
84 | public function thereIsAnEmailFromToTitled($negate, $direction, $email, $subject) |
||
85 | { |
||
86 | $to = ($direction == 'to') ? $email : null; |
||
87 | $from = ($direction == 'from') ? $email : null; |
||
88 | $match = $this->mailer->findEmail($to, $from, $subject); |
||
89 | $allMails = $this->mailer->findEmails($to, $from); |
||
90 | $allTitles = $allMails ? '"' . implode('","', array_map(function($email) {return $email->Subject;}, $allMails)) . '"' : null; |
||
91 | if(trim($negate)) { |
||
92 | assertNull($match); |
||
93 | } else { |
||
94 | $msg = sprintf( |
||
95 | 'Could not find email %s "%s" titled "%s".', |
||
96 | $direction, |
||
97 | $email, |
||
98 | $subject |
||
99 | ); |
||
100 | if($allTitles) { |
||
101 | $msg .= ' Existing emails: ' . $allTitles; |
||
102 | } |
||
103 | assertNotNull($match,$msg); |
||
104 | } |
||
105 | $this->lastMatchedEmail = $match; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Example: Given the email should contain "Thank you for registering!". |
||
110 | * Assumes an email has been identified by a previous step, |
||
111 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
112 | * |
||
113 | * @Given /^the email should (not |)contain "([^"]*)"$/ |
||
114 | */ |
||
115 | public function thereTheEmailContains($negate, $content) |
||
116 | { |
||
117 | if(!$this->lastMatchedEmail) { |
||
118 | throw new \LogicException('No matched email found from previous step'); |
||
119 | } |
||
120 | |||
121 | $email = $this->lastMatchedEmail; |
||
122 | $emailContent = null; |
||
123 | if($email->Content) { |
||
124 | $emailContent = $email->Content; |
||
125 | } else { |
||
126 | $emailContent = $email->PlainContent; |
||
127 | } |
||
128 | |||
129 | if(trim($negate)) { |
||
130 | assertNotContains($content, $emailContent); |
||
131 | } else { |
||
132 | assertContains($content, $emailContent); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Example: Given the email contains "Thank you for <strong>registering!<strong>". |
||
138 | * Then the email should contain plain text "Thank you for registering!" |
||
139 | * Assumes an email has been identified by a previous step, |
||
140 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
141 | * |
||
142 | * @Given /^the email should contain plain text "([^"]*)"$/ |
||
143 | */ |
||
144 | public function thereTheEmailContainsPlainText($content) |
||
157 | |||
158 | /** |
||
159 | * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/ |
||
160 | */ |
||
161 | View Code Duplication | public function iGoToInTheEmailTo($linkSelector, $direction, $email) |
|
162 | { |
||
163 | $to = ($direction == 'to') ? $email : null; |
||
164 | $from = ($direction == 'from') ? $email : null; |
||
165 | $match = $this->mailer->findEmail($to, $from); |
||
166 | assertNotNull($match); |
||
167 | |||
168 | $crawler = new Crawler($match->Content); |
||
169 | $linkEl = $crawler->selectLink($linkSelector); |
||
170 | assertNotNull($linkEl); |
||
171 | $link = $linkEl->attr('href'); |
||
172 | assertNotNull($link); |
||
173 | |||
174 | return new Step\When(sprintf('I go to "%s"', $link)); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)" titled "([^"]*)"$/ |
||
179 | */ |
||
180 | View Code Duplication | public function iGoToInTheEmailToTitled($linkSelector, $direction, $email, $title) |
|
194 | |||
195 | /** |
||
196 | * Assumes an email has been identified by a previous step, |
||
197 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
198 | * |
||
199 | * @When /^I click on the "([^"]*)" link in the email"$/ |
||
200 | */ |
||
201 | public function iGoToInTheEmail($linkSelector) |
||
202 | { |
||
203 | if(!$this->lastMatchedEmail) { |
||
204 | throw new \LogicException('No matched email found from previous step'); |
||
205 | } |
||
206 | |||
207 | $match = $this->lastMatchedEmail; |
||
208 | $crawler = new Crawler($match->Content); |
||
209 | $linkEl = $crawler->selectLink($linkSelector); |
||
210 | assertNotNull($linkEl); |
||
211 | $link = $linkEl->attr('href'); |
||
212 | assertNotNull($link); |
||
213 | |||
214 | return new Step\When(sprintf('I go to "%s"', $link)); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @Given /^I clear all emails$/ |
||
219 | */ |
||
220 | public function iClearAllEmails() |
||
221 | { |
||
222 | $this->lastMatchedEmail = null; |
||
223 | return $this->mailer->clearEmails(); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Example: Then the email should contain the following data: |
||
228 | * | row1 | |
||
229 | * | row2 | |
||
230 | * Assumes an email has been identified by a previous step. |
||
231 | * @Then /^the email should (not |)contain the following data:$/ |
||
232 | */ |
||
233 | public function theEmailContainFollowingData($negate, TableNode $table) { |
||
261 | |||
262 | /** |
||
263 | * @Then /^there should (not |)be an email titled "([^"]*)"$/ |
||
264 | */ |
||
265 | public function thereIsAnEmailTitled($negate, $subject) |
||
279 | |||
280 | /** |
||
281 | * @Then /^the email should (not |)be sent from "([^"]*)"$/ |
||
282 | */ |
||
283 | View Code Duplication | public function theEmailSentFrom($negate, $from) |
|
296 | |||
297 | /** |
||
298 | * @Then /^the email should (not |)be sent to "([^"]*)"$/ |
||
299 | */ |
||
300 | View Code Duplication | public function theEmailSentTo($negate, $to) |
|
313 | |||
314 | /** |
||
315 | * The link text is the link address itself which contains special characters |
||
316 | * e.g. http://localhost/Security/changepassword?m=199&title=reset |
||
317 | * Example: When I click on the http link "changepassword" in the email |
||
318 | * @When /^I click on the http link "([^"]*)" in the email$/ |
||
319 | */ |
||
320 | public function iClickOnHttpLinkInEmail($httpText) { |
||
321 | if(!$this->lastMatchedEmail) { |
||
322 | throw new \LogicException('No matched email found from previous step'); |
||
343 | } |
||
344 |
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: