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 SilverStripeContext 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 SilverStripeContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class SilverStripeContext extends MinkContext implements SilverStripeAwareContextInterface |
||
31 | { |
||
32 | protected $databaseName; |
||
33 | |||
34 | /** |
||
35 | * @var Array Partial string match for step names |
||
36 | * that are considered to trigger Ajax request in the CMS, |
||
37 | * and hence need special timeout handling. |
||
38 | * @see \SilverStripe\BehatExtension\Context\BasicContext->handleAjaxBeforeStep(). |
||
39 | */ |
||
40 | protected $ajaxSteps; |
||
41 | |||
42 | /** |
||
43 | * @var Int Timeout in milliseconds, after which the interface assumes |
||
44 | * that an Ajax request has timed out, and continues with assertions. |
||
45 | */ |
||
46 | protected $ajaxTimeout; |
||
47 | |||
48 | /** |
||
49 | * @var String Relative URL to the SilverStripe admin interface. |
||
50 | */ |
||
51 | protected $adminUrl; |
||
52 | |||
53 | /** |
||
54 | * @var String Relative URL to the SilverStripe login form. |
||
55 | */ |
||
56 | protected $loginUrl; |
||
57 | |||
58 | /** |
||
59 | * @var String Relative path to a writeable folder where screenshots can be stored. |
||
60 | * If set to NULL, no screenshots will be stored. |
||
61 | */ |
||
62 | protected $screenshotPath; |
||
63 | |||
64 | protected $context; |
||
65 | |||
66 | protected $testSessionEnvironment; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Initializes context. |
||
71 | * Every scenario gets it's own context object. |
||
72 | * |
||
73 | * @param array $parameters context parameters (set them up through behat.yml) |
||
74 | */ |
||
75 | public function __construct(array $parameters) |
||
81 | |||
82 | public function setDatabase($databaseName) |
||
86 | |||
87 | public function setAjaxSteps($ajaxSteps) |
||
93 | |||
94 | public function getAjaxSteps() |
||
98 | |||
99 | public function setAjaxTimeout($ajaxTimeout) |
||
103 | |||
104 | public function getAjaxTimeout() |
||
108 | |||
109 | public function setAdminUrl($adminUrl) |
||
113 | |||
114 | public function getAdminUrl() |
||
118 | |||
119 | public function setLoginUrl($loginUrl) |
||
123 | |||
124 | public function getLoginUrl() |
||
128 | |||
129 | public function setScreenshotPath($screenshotPath) |
||
133 | |||
134 | public function getScreenshotPath() |
||
138 | |||
139 | public function getRegionMap() |
||
143 | |||
144 | public function setRegionMap($regionMap) |
||
148 | |||
149 | /** |
||
150 | * Returns MinkElement based off region defined in .yml file. |
||
151 | * Also supports direct CSS selectors and regions identified by a "data-title" attribute. |
||
152 | * When using the "data-title" attribute, ensure not to include double quotes. |
||
153 | * |
||
154 | * @param String $region Region name or CSS selector |
||
155 | * @return MinkElement|null |
||
156 | */ |
||
157 | public function getRegionObj($region) |
||
200 | |||
201 | /** |
||
202 | * @BeforeScenario |
||
203 | */ |
||
204 | public function before(ScenarioEvent $event) |
||
238 | |||
239 | /** |
||
240 | * Returns a parameter map of state to set within the test session. |
||
241 | * Takes TESTSESSION_PARAMS environment variable into account for run-specific configurations. |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | public function getTestSessionState() |
||
257 | |||
258 | /** |
||
259 | * Parses given URL and returns its components |
||
260 | * |
||
261 | * @param $url |
||
262 | * @return array|mixed Parsed URL |
||
263 | */ |
||
264 | public function parseUrl($url) |
||
277 | |||
278 | /** |
||
279 | * Checks whether current URL is close enough to the given URL. |
||
280 | * Unless specified in $url, get vars will be ignored |
||
281 | * Unless specified in $url, fragment identifiers will be ignored |
||
282 | * |
||
283 | * @param $url string URL to compare to current URL |
||
284 | * @return boolean Returns true if the current URL is close enough to the given URL, false otherwise. |
||
285 | */ |
||
286 | public function isCurrentUrlSimilarTo($url) |
||
307 | |||
308 | /** |
||
309 | * Returns base URL parameter set in MinkExtension. |
||
310 | * It simplifies configuration by allowing to specify this parameter |
||
311 | * once but makes code dependent on MinkExtension. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function getBaseUrl() |
||
319 | |||
320 | /** |
||
321 | * Joins URL parts into an URL using forward slash. |
||
322 | * Forward slash usages are normalised to one between parts. |
||
323 | * This method takes variable number of parameters. |
||
324 | * |
||
325 | * @param $... |
||
326 | * @return string |
||
327 | * @throws \InvalidArgumentException |
||
328 | */ |
||
329 | public function joinUrlParts() |
||
343 | |||
344 | public function canIntercept() |
||
358 | |||
359 | /** |
||
360 | * @Given /^(.*) without redirection$/ |
||
361 | */ |
||
362 | public function theRedirectionsAreIntercepted($step) |
||
370 | |||
371 | /** |
||
372 | * Fills in form field with specified id|name|label|value. |
||
373 | * Overwritten to select the first *visible* element, see https://github.com/Behat/Mink/issues/311 |
||
374 | */ |
||
375 | View Code Duplication | public function fillField($field, $value) |
|
397 | |||
398 | /** |
||
399 | * Overwritten to click the first *visable* link the DOM. |
||
400 | */ |
||
401 | View Code Duplication | public function clickLink($link) |
|
422 | |||
423 | /** |
||
424 | * Sets the current date. Relies on the underlying functionality using |
||
425 | * {@link SS_Datetime::now()} rather than PHP's system time methods like date(). |
||
426 | * Supports ISO fomat: Y-m-d |
||
427 | * Example: Given the current date is "2009-10-31" |
||
428 | * |
||
429 | * @Given /^the current date is "([^"]*)"$/ |
||
430 | */ |
||
431 | View Code Duplication | public function givenTheCurrentDateIs($date) |
|
446 | |||
447 | /** |
||
448 | * Sets the current time. Relies on the underlying functionality using |
||
449 | * {@link \SS_Datetime::now()} rather than PHP's system time methods like date(). |
||
450 | * Supports ISO fomat: H:i:s |
||
451 | * Example: Given the current time is "20:31:50" |
||
452 | * |
||
453 | * @Given /^the current time is "([^"]*)"$/ |
||
454 | */ |
||
455 | View Code Duplication | public function givenTheCurrentTimeIs($time) |
|
470 | |||
471 | /** |
||
472 | * Selects option in select field with specified id|name|label|value. |
||
473 | * |
||
474 | * @override /^(?:|I )select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/ |
||
475 | */ |
||
476 | public function selectOption($select, $option) |
||
491 | |||
492 | /** |
||
493 | * Selects option in select field with specified id|name|label|value using javascript |
||
494 | * This method uses javascript to allow selection of options that may be |
||
495 | * overridden by javascript libraries, and thus hide the element. |
||
496 | * |
||
497 | * @When /^(?:|I )select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)" with javascript$/ |
||
498 | */ |
||
499 | public function selectOptionWithJavascript($select, $option) |
||
544 | } |
||
545 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: