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:
1 | <?php |
||
14 | class VisibilityContext extends RawMinkContext { |
||
15 | |||
16 | /** |
||
17 | * Assert presence of given element on the page. |
||
18 | * |
||
19 | * @Then the element :tag with text :text should be visible |
||
20 | */ |
||
21 | View Code Duplication | public function assertElementVisibility($tag, $text) { |
|
31 | |||
32 | /** |
||
33 | * Assert absence of given element on the page. |
||
34 | * |
||
35 | * @Then the element :tag with text :text should not be visible |
||
36 | */ |
||
37 | View Code Duplication | public function assertElementNonVisibility($tag, $text) { |
|
47 | |||
48 | /** |
||
49 | * Assert presence of given field on the page. |
||
50 | * |
||
51 | * @Then I should see the field :field |
||
52 | */ |
||
53 | public function iShouldSeeTheField($field) { |
||
58 | |||
59 | /** |
||
60 | * Assert absence of given field. |
||
61 | * |
||
62 | * @Then I should not see the field :field |
||
63 | */ |
||
64 | public function iShouldNotSeeTheField($field) { |
||
68 | |||
69 | /** |
||
70 | * Assert visibility of an element. |
||
71 | * |
||
72 | * @param string $element |
||
73 | * Element selector or a string that describes it. |
||
74 | * @param \Behat\Mink\Element\NodeElement $node |
||
75 | * Node representing the element above, if any. |
||
76 | * |
||
77 | * @throws \Behat\Mink\Exception\ExpectationException |
||
78 | * Throws exception if element not found. |
||
79 | */ |
||
80 | View Code Duplication | protected function assertElementVisible($element, NodeElement $node) { |
|
95 | |||
96 | /** |
||
97 | * Assert non visibility of an element. |
||
98 | * |
||
99 | * @param string $element |
||
100 | * Element selector or a string that describes it. |
||
101 | * @param \Behat\Mink\Element\NodeElement $node |
||
102 | * Node representing the element above, if any. |
||
103 | * |
||
104 | * @throws \Behat\Mink\Exception\ExpectationException |
||
105 | * Throws exception if element is found. |
||
106 | */ |
||
107 | View Code Duplication | protected function assertElementNotVisible($element, NodeElement $node) { |
|
122 | |||
123 | } |
||
124 |
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.