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 |
||
9 | class SystemContext implements Context |
||
10 | { |
||
11 | private $root; |
||
12 | private $output; |
||
13 | private $lastExecutionTime; |
||
14 | private $lastReturnCode; |
||
15 | private $createdFiles = []; |
||
16 | |||
17 | public function __construct($root = '.') |
||
21 | |||
22 | public static function getTranslationResources() |
||
26 | |||
27 | /** |
||
28 | * Uploads a file using the specified input field |
||
29 | * |
||
30 | * @When (I )put the file :file into :field |
||
31 | */ |
||
32 | public function putFileIntoField($file, $field) |
||
40 | |||
41 | /** |
||
42 | * Execute a command |
||
43 | * |
||
44 | * @Given (I )execute :command |
||
45 | */ |
||
46 | public function iExecute($cmd) |
||
54 | |||
55 | /** |
||
56 | * Execute a command from project root |
||
57 | * |
||
58 | * @Given (I )execute :command from project root |
||
59 | */ |
||
60 | public function iExecuteFromProjectRoot($cmd) |
||
65 | |||
66 | /** |
||
67 | * Command should succeed |
||
68 | * |
||
69 | * @Then command should succeed |
||
70 | */ |
||
71 | public function commandShouldSucceed() { |
||
76 | |||
77 | /** |
||
78 | * Command should fail |
||
79 | * |
||
80 | * @Then command should fail |
||
81 | */ |
||
82 | public function commandShouldFail() { |
||
87 | |||
88 | /** |
||
89 | * Command should last less than |
||
90 | * |
||
91 | * @Then command should last less than :seconds seconds |
||
92 | */ |
||
93 | public function commandShouldLastLessThan($seconds) |
||
99 | |||
100 | /** |
||
101 | * Command should last more than |
||
102 | * |
||
103 | * @Then command should last more than :seconds seconds |
||
104 | */ |
||
105 | public function commandShouldMoreLessThan($seconds) |
||
111 | |||
112 | /** |
||
113 | * Checks, that output contains specified text. |
||
114 | * |
||
115 | * @Then output should contain :text |
||
116 | */ |
||
117 | public function outputShouldContain($text) |
||
133 | |||
134 | /** |
||
135 | * Checks, that output not contains specified text. |
||
136 | * |
||
137 | * @Then output should not contain :text |
||
138 | */ |
||
139 | public function ouputShouldNotContain($text) |
||
149 | |||
150 | /** |
||
151 | * @Given output should be: |
||
152 | */ |
||
153 | public function outputShouldBe(PyStringNode $string) |
||
162 | |||
163 | /** |
||
164 | * @Given output should not be: |
||
165 | */ |
||
166 | public function outputShouldNotBe(PyStringNode $string) |
||
182 | |||
183 | /** |
||
184 | * @Given (I )create the file :filename containing: |
||
185 | * @Given (I )create the file :filename contening: |
||
186 | */ |
||
187 | public function iCreateTheFileContaining($filename, PyStringNode $string) |
||
197 | |||
198 | /** |
||
199 | * @Then print the content of :filename file |
||
200 | */ |
||
201 | public function printTheContentOfFile($filename) |
||
210 | |||
211 | /** |
||
212 | * @AfterScenario |
||
213 | */ |
||
214 | public function after() |
||
220 | } |
||
221 |
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.