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 BrowserContext 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 BrowserContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class BrowserContext extends BaseContext |
||
13 | { |
||
14 | private $timeout; |
||
15 | private $dateFormat = 'dmYHi'; |
||
16 | private $timerStartedAt; |
||
17 | |||
18 | public function __construct($timeout = 1) |
||
22 | |||
23 | /** |
||
24 | * @AfterScenario |
||
25 | */ |
||
26 | public function closeBrowser() |
||
30 | |||
31 | /** |
||
32 | * @BeforeScenario |
||
33 | * |
||
34 | * @When (I )start timing now |
||
35 | */ |
||
36 | public function startTimer() |
||
40 | |||
41 | /** |
||
42 | * Set login / password for next HTTP authentication |
||
43 | * |
||
44 | * @When I set basic authentication with :user and :password |
||
45 | */ |
||
46 | public function iSetBasicAuthenticationWithAnd($user, $password) |
||
50 | |||
51 | /** |
||
52 | * Open url with various parameters |
||
53 | * |
||
54 | * @Given (I )am on url composed by: |
||
55 | */ |
||
56 | public function iAmOnUrlComposedBy(TableNode $tableNode) |
||
66 | |||
67 | /** |
||
68 | * Clicks on the nth CSS element |
||
69 | * |
||
70 | * @When (I )click on the :index :element element |
||
71 | */ |
||
72 | public function iClickOnTheNthElement($index, $element) |
||
77 | |||
78 | /** |
||
79 | * Click on the nth specified link |
||
80 | * |
||
81 | * @When (I )follow the :index :link link |
||
82 | */ |
||
83 | public function iFollowTheNthLink($index, $link) |
||
89 | |||
90 | /** |
||
91 | * Presses the nth specified button |
||
92 | * |
||
93 | * @When (I )press the :index :button button |
||
94 | */ |
||
95 | public function pressTheNthButton($index, $button) |
||
101 | |||
102 | /** |
||
103 | * Fills in form field with current date |
||
104 | * |
||
105 | * @When (I )fill in :field with the current date |
||
106 | */ |
||
107 | public function iFillInWithTheCurrentDate($field) |
||
111 | |||
112 | /** |
||
113 | * Fills in form field with current date and strtotime modifier |
||
114 | * |
||
115 | * @When (I )fill in :field with the current date and modifier :modifier |
||
116 | */ |
||
117 | public function iFillInWithTheCurrentDateAndModifier($field, $modifier) |
||
122 | |||
123 | /** |
||
124 | * Mouse over a CSS element |
||
125 | * |
||
126 | * @When (I )hover :element |
||
127 | */ |
||
128 | View Code Duplication | public function iHoverIShouldSeeIn($element) |
|
136 | |||
137 | /** |
||
138 | * Save value of the field in parameters array |
||
139 | * |
||
140 | * @When (I )save the value of :field in the :parameter parameter |
||
141 | */ |
||
142 | public function iSaveTheValueOfInTheParameter($field, $parameter) |
||
152 | |||
153 | /** |
||
154 | * Checks, that the page should contains specified text after given timeout |
||
155 | * |
||
156 | * @Then (I )wait :count second(s) until I see :text |
||
157 | */ |
||
158 | public function iWaitSecondsUntilISee($count, $text) |
||
162 | |||
163 | /** |
||
164 | * Checks, that the page should not contain specified text before given timeout |
||
165 | * |
||
166 | * @Then (I )should not see :text within :count second(s) |
||
167 | */ |
||
168 | public function iDontSeeInSeconds($count, $text) |
||
180 | |||
181 | /** |
||
182 | * Checks, that the page should contains specified text after timeout |
||
183 | * |
||
184 | * @Then (I )wait until I see :text |
||
185 | */ |
||
186 | public function iWaitUntilISee($text) |
||
190 | |||
191 | /** |
||
192 | * Checks, that the element contains specified text after timeout |
||
193 | * |
||
194 | * @Then (I )wait :count second(s) until I see :text in the :element element |
||
195 | */ |
||
196 | public function iWaitSecondsUntilISeeInTheElement($count, $text, $element) |
||
224 | |||
225 | /** |
||
226 | * @Then (I )wait :count second(s) |
||
227 | */ |
||
228 | public function iWaitSeconds($count) |
||
232 | |||
233 | /** |
||
234 | * Checks, that the element contains specified text after timeout |
||
235 | * |
||
236 | * @Then (I )wait until I see :text in the :element element |
||
237 | */ |
||
238 | public function iWaitUntilISeeInTheElement($text, $element) |
||
242 | |||
243 | /** |
||
244 | * Checks, that the page should contains specified element after timeout |
||
245 | * |
||
246 | * @Then (I )wait for :element element |
||
247 | */ |
||
248 | public function iWaitForElement($element) |
||
252 | |||
253 | /** |
||
254 | * Wait for a element |
||
255 | * |
||
256 | * @Then (I )wait :count second(s) for :element element |
||
257 | */ |
||
258 | public function iWaitSecondsForElement($count, $element) |
||
281 | |||
282 | /** |
||
283 | * @Then /^(?:|I )should see (?P<count>\d+) "(?P<element>[^"]*)" in the (?P<index>\d+)(?:st|nd|rd|th) "(?P<parent>[^"]*)"$/ |
||
284 | */ |
||
285 | View Code Duplication | public function iShouldSeeNElementInTheNthParent($count, $element, $index, $parent) |
|
292 | |||
293 | /** |
||
294 | * @Then (I )should see less than :count :element in the :index :parent |
||
295 | */ |
||
296 | View Code Duplication | public function iShouldSeeLessThanNElementInTheNthParent($count, $element, $index, $parent) |
|
303 | |||
304 | /** |
||
305 | * @Then (I )should see more than :count :element in the :index :parent |
||
306 | */ |
||
307 | View Code Duplication | public function iShouldSeeMoreThanNElementInTheNthParent($count, $element, $index, $parent) |
|
314 | |||
315 | /** |
||
316 | * Checks, that element with given CSS is enabled |
||
317 | * |
||
318 | * @Then the element :element should be enabled |
||
319 | */ |
||
320 | View Code Duplication | public function theElementShouldBeEnabled($element) |
|
331 | |||
332 | /** |
||
333 | * Checks, that element with given CSS is disabled |
||
334 | * |
||
335 | * @Then the element :element should be disabled |
||
336 | */ |
||
337 | public function theElementShouldBeDisabled($element) |
||
343 | |||
344 | /** |
||
345 | * Checks, that given select box contains the specified option |
||
346 | * |
||
347 | * @Then the :select select box should contain :option |
||
348 | */ |
||
349 | public function theSelectBoxShouldContain($select, $option) |
||
365 | |||
366 | /** |
||
367 | * Checks, that given select box does not contain the specified option |
||
368 | * |
||
369 | * @Then the :select select box should not contain :option |
||
370 | */ |
||
371 | public function theSelectBoxShouldNotContain($select, $option) |
||
377 | |||
378 | /** |
||
379 | * Checks, that the specified CSS element is visible |
||
380 | * |
||
381 | * @Then the :element element should be visible |
||
382 | */ |
||
383 | public function theElementShouldBeVisible($element) |
||
394 | |||
395 | /** |
||
396 | * Checks, that the specified CSS element is not visible |
||
397 | * |
||
398 | * @Then the :element element should not be visible |
||
399 | */ |
||
400 | public function theElementShouldNotBeVisible($element) |
||
408 | |||
409 | /** |
||
410 | * Select a frame by its name or ID. |
||
411 | * |
||
412 | * @When (I )switch to iframe :name |
||
413 | * @When (I )switch to frame :name |
||
414 | */ |
||
415 | public function switchToIFrame($name) |
||
419 | |||
420 | /** |
||
421 | * Go back to main document frame. |
||
422 | * |
||
423 | * @When (I )switch to main frame |
||
424 | */ |
||
425 | public function switchToMainFrame() |
||
429 | |||
430 | /** |
||
431 | * test time from when the scenario started |
||
432 | * |
||
433 | * @Then (the )total elapsed time should be :comparison than :expected seconds |
||
434 | * @Then (the )total elapsed time should be :comparison to :expected seconds |
||
435 | */ |
||
436 | public function elapsedTime($comparison, $expected) |
||
457 | } |
||
458 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.