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) |
||
44 | |||
45 | /** |
||
46 | * Get Mink session from MinkContext |
||
47 | */ |
||
48 | public function getSession($name = null) |
||
52 | |||
53 | /** |
||
54 | * @BeforeScenario |
||
55 | */ |
||
56 | public function before(ScenarioEvent $event) |
||
64 | |||
65 | /** |
||
66 | * @Given /^there should (not |)be an email (to|from) "([^"]*)"$/ |
||
67 | */ |
||
68 | public function thereIsAnEmailFromTo($negate, $direction, $email) |
||
80 | |||
81 | /** |
||
82 | * @Given /^there should (not |)be an email (to|from) "([^"]*)" titled "([^"]*)"$/ |
||
83 | */ |
||
84 | public function thereIsAnEmailFromToTitled($negate, $direction, $email, $subject) |
||
109 | |||
110 | /** |
||
111 | * Example: Given the email should contain "Thank you for registering!". |
||
112 | * Assumes an email has been identified by a previous step, |
||
113 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
114 | * |
||
115 | * @Given /^the email should (not |)contain "([^"]*)"$/ |
||
116 | */ |
||
117 | public function thereTheEmailContains($negate, $content) |
||
137 | |||
138 | /** |
||
139 | * Example: Given the email contains "Thank you for <strong>registering!<strong>". |
||
140 | * Then the email should contain plain text "Thank you for registering!" |
||
141 | * Assumes an email has been identified by a previous step, |
||
142 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
143 | * |
||
144 | * @Given /^the email should contain plain text "([^"]*)"$/ |
||
145 | */ |
||
146 | public function thereTheEmailContainsPlainText($content) |
||
159 | |||
160 | /** |
||
161 | * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/ |
||
162 | */ |
||
163 | View Code Duplication | public function iGoToInTheEmailTo($linkSelector, $direction, $email) |
|
178 | |||
179 | /** |
||
180 | * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)" titled "([^"]*)"$/ |
||
181 | */ |
||
182 | View Code Duplication | public function iGoToInTheEmailToTitled($linkSelector, $direction, $email, $title) |
|
196 | |||
197 | /** |
||
198 | * Assumes an email has been identified by a previous step, |
||
199 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
200 | * |
||
201 | * @When /^I click on the "([^"]*)" link in the email"$/ |
||
202 | */ |
||
203 | public function iGoToInTheEmail($linkSelector) |
||
218 | |||
219 | /** |
||
220 | * @Given /^I clear all emails$/ |
||
221 | */ |
||
222 | public function iClearAllEmails() |
||
227 | |||
228 | /** |
||
229 | * Example: Then the email should contain the following data: |
||
230 | * | row1 | |
||
231 | * | row2 | |
||
232 | * Assumes an email has been identified by a previous step. |
||
233 | * @Then /^the email should (not |)contain the following data:$/ |
||
234 | */ |
||
235 | public function theEmailContainFollowingData($negate, TableNode $table) |
||
264 | |||
265 | /** |
||
266 | * @Then /^there should (not |)be an email titled "([^"]*)"$/ |
||
267 | */ |
||
268 | public function thereIsAnEmailTitled($negate, $subject) |
||
282 | |||
283 | /** |
||
284 | * @Then /^the email should (not |)be sent from "([^"]*)"$/ |
||
285 | */ |
||
286 | View Code Duplication | public function theEmailSentFrom($negate, $from) |
|
299 | |||
300 | /** |
||
301 | * @Then /^the email should (not |)be sent to "([^"]*)"$/ |
||
302 | */ |
||
303 | View Code Duplication | public function theEmailSentTo($negate, $to) |
|
316 | } |
||
317 |
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: