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 |
||
11 | class FormstoneContext extends BaseContext |
||
12 | { |
||
13 | /** |
||
14 | * Choose an option in selecter field |
||
15 | * |
||
16 | * @When /^(?:|I )fill in selecter "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
||
17 | * @When /^(?:|I )fill in selecter "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/ |
||
18 | */ |
||
19 | public function iFillInSelecter($field, $value) |
||
23 | |||
24 | /** |
||
25 | * Choose an option in dropdown component field |
||
26 | * |
||
27 | * @When /^(?:|I )fill in dropdown "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
||
28 | * @When /^(?:|I )fill in dropdown "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/ |
||
29 | */ |
||
30 | public function iFillInDropdown($field, $value) |
||
34 | |||
35 | /** |
||
36 | * Select value in component |
||
37 | * |
||
38 | * @param string $component |
||
39 | * @param string $field |
||
40 | * @param string $value |
||
41 | * @throws \Exception |
||
42 | */ |
||
43 | private function selectValue($component, $field, $value) |
||
44 | { |
||
45 | $page = $this->getSession()->getPage(); |
||
46 | |||
47 | $this->openComponent($page, $component, $field); |
||
48 | $this->selectComponentValue($page, $component, $field, $value); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Open component choice list |
||
53 | * |
||
54 | * @param DocumentElement $page |
||
55 | * @param string $component |
||
56 | * @param string $field |
||
57 | * @throws \Exception |
||
58 | */ |
||
59 | View Code Duplication | private function openComponent(DocumentElement $page, $component, $field) |
|
|
|||
60 | { |
||
61 | $select = $page->find('css', sprintf('#%s', $field)); |
||
62 | if (!$select) { |
||
63 | throw new \Exception(sprintf('No select "%s" found', $field)); |
||
64 | } |
||
65 | |||
66 | $fieldName = sprintf('.fs-%1$s .fs-%1$s-selected', $component); |
||
67 | $choices = $select->getParent()->find('css', $fieldName); |
||
68 | if (!$choices) { |
||
69 | throw new \Exception(sprintf('No field "%s" found', $field)); |
||
70 | } |
||
71 | |||
72 | $choices->press(); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Select value in choice list |
||
77 | * |
||
78 | * @param DocumentElement $page |
||
79 | * @param string $component |
||
80 | * @param string $field |
||
81 | * @param string $value |
||
82 | * @throws \Exception |
||
83 | */ |
||
84 | private function selectComponentValue(DocumentElement $page, $component, $field, $value) |
||
102 | } |
||
103 |
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.