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 MarkupContext 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 MarkupContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class MarkupContext extends RawMinkContext |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Return a region from the current page. |
||
15 | * |
||
16 | * @throws \Exception |
||
17 | * If region cannot be found. |
||
18 | * |
||
19 | * @param string $region |
||
20 | * The machine name of the region to return. |
||
21 | * |
||
22 | * @return \Behat\Mink\Element\NodeElement |
||
23 | * |
||
24 | * @todo this should be a trait when PHP 5.3 support is dropped. |
||
25 | */ |
||
26 | public function getRegion($region) |
||
36 | |||
37 | /** |
||
38 | * Checks if a button with id|name|title|alt|value exists in a region |
||
39 | * |
||
40 | * @Then I should see the button :button in the :region( region) |
||
41 | * @Then I should see the :button button in the :region( region) |
||
42 | * |
||
43 | * @param $button |
||
44 | * string The id|name|title|alt|value of the button |
||
45 | * @param $region |
||
46 | * string The region in which the button should be found |
||
47 | * |
||
48 | * @throws \Exception |
||
49 | * If region or button within it cannot be found. |
||
50 | */ |
||
51 | View Code Duplication | public function assertRegionButton($button, $region) |
|
60 | |||
61 | /** |
||
62 | * Asserts that a button does not exists in a region. |
||
63 | * |
||
64 | * @Then I should not see the button :button in the :region( region) |
||
65 | * @Then I should not see the :button button in the :region( region) |
||
66 | * |
||
67 | * @param $button |
||
68 | * string The id|name|title|alt|value of the button |
||
69 | * @param $region |
||
70 | * string The region in which the button should not be found |
||
71 | * |
||
72 | * @throws \Exception |
||
73 | * If region is not found or the button is found within the region. |
||
74 | */ |
||
75 | View Code Duplication | public function assertNotRegionButton($button, $region) |
|
84 | |||
85 | /** |
||
86 | * @Then I( should) see the :tag element in the :region( region) |
||
87 | */ |
||
88 | View Code Duplication | public function assertRegionElement($tag, $region) |
|
97 | |||
98 | /** |
||
99 | * @Then I( should) not see the :tag element in the :region( region) |
||
100 | */ |
||
101 | View Code Duplication | public function assertNotRegionElement($tag, $region) |
|
109 | |||
110 | /** |
||
111 | * @Then I( should) not see :text in the :tag element in the :region( region) |
||
112 | */ |
||
113 | public function assertNotRegionElementText($text, $tag, $region) |
||
125 | |||
126 | /** |
||
127 | * @Then I( should) see the :tag element with the :attribute attribute set to :value in the :region( region) |
||
128 | */ |
||
129 | public function assertRegionElementAttribute($tag, $attribute, $value, $region) |
||
158 | |||
159 | /** |
||
160 | * @Then I( should) see :text in the :tag element with the :attribute attribute set to :value in the :region( region) |
||
161 | */ |
||
162 | public function assertRegionElementTextAttribute($text, $tag, $attribute, $value, $region) |
||
191 | |||
192 | /** |
||
193 | * @Then I( should) see :text in the :tag element with the :property CSS property set to :value in the :region( region) |
||
194 | */ |
||
195 | public function assertRegionElementTextCss($text, $tag, $property, $value, $region) |
||
232 | } |
||
233 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.