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) |
||
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) |
||
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) |
||
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 | * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/ |
||
139 | */ |
||
140 | View Code Duplication | public function iGoToInTheEmailTo($linkSelector, $direction, $email) |
|
155 | |||
156 | /** |
||
157 | * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)" titled "([^"]*)"$/ |
||
158 | */ |
||
159 | View Code Duplication | public function iGoToInTheEmailToTitled($linkSelector, $direction, $email, $title) |
|
173 | |||
174 | /** |
||
175 | * Assumes an email has been identified by a previous step, |
||
176 | * e.g. through 'Given there should be an email to "[email protected]"'. |
||
177 | * |
||
178 | * @When /^I click on the "([^"]*)" link in the email"$/ |
||
179 | */ |
||
180 | public function iGoToInTheEmail($linkSelector) |
||
195 | |||
196 | /** |
||
197 | * @Given /^I clear all emails$/ |
||
198 | */ |
||
199 | public function iClearAllEmails() |
||
204 | |||
205 | /** |
||
206 | * Example: Then the email should contain the following data: |
||
207 | * | row1 | |
||
208 | * | row2 | |
||
209 | * Assumes an email has been identified by a previous step. |
||
210 | * @Then /^the email should (not |)contain the following data:$/ |
||
211 | */ |
||
212 | public function theEmailContainFollowingData($negate, TableNode $table) { |
||
239 | |||
240 | /** |
||
241 | * @Then /^there should (not |)be an email titled "([^"]*)"$/ |
||
242 | */ |
||
243 | public function thereIsAnEmailTitled($negate, $subject) |
||
257 | |||
258 | /** |
||
259 | * @Then /^the email should (not |)be sent from "([^"]*)"$/ |
||
260 | */ |
||
261 | View Code Duplication | public function theEmailSentFrom($negate, $from) |
|
274 | |||
275 | /** |
||
276 | * @Then /^the email should (not |)be sent to "([^"]*)"$/ |
||
277 | */ |
||
278 | View Code Duplication | public function theEmailSentTo($negate, $to) |
|
291 | } |
||
292 |
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: