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 |
||
22 | final class TestContext implements Context |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private static $workingDir; |
||
28 | |||
29 | /** |
||
30 | * @var Filesystem |
||
31 | */ |
||
32 | private static $filesystem; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private static $phpBin; |
||
38 | |||
39 | /** |
||
40 | * @var Process |
||
41 | */ |
||
42 | private $process; |
||
43 | |||
44 | /** |
||
45 | * @BeforeFeature |
||
46 | */ |
||
47 | public static function beforeFeature() |
||
48 | { |
||
49 | self::$workingDir = sprintf('%s/%s/', sys_get_temp_dir(), uniqid('', true)); |
||
50 | self::$filesystem = new Filesystem(); |
||
51 | self::$phpBin = self::findPhpBinary(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @BeforeScenario |
||
56 | */ |
||
57 | public function beforeScenario() |
||
58 | { |
||
59 | self::$filesystem->remove(self::$workingDir); |
||
60 | self::$filesystem->mkdir(self::$workingDir, 0777); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @AfterScenario |
||
65 | */ |
||
66 | public function afterScenario() |
||
70 | |||
71 | /** |
||
72 | * @Given /^a Behat configuration containing(?: "([^"]+)"|:)$/ |
||
73 | */ |
||
74 | public function thereIsConfiguration($content) |
||
78 | |||
79 | /** |
||
80 | * @Given /^a (?:.+ |)file "([^"]+)" containing(?: "([^"]+)"|:)$/ |
||
81 | */ |
||
82 | public function thereIsFile($file, $content) |
||
83 | { |
||
84 | self::$filesystem->dumpFile(self::$workingDir . '/' . $file, (string) $content); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @Given /^a feature file containing(?: "([^"]+)"|:)$/ |
||
89 | */ |
||
90 | public function thereIsFeatureFile($content) |
||
94 | |||
95 | /** |
||
96 | * @Given /^a feature file with passing scenario$/ |
||
97 | */ |
||
98 | public function thereIsFeatureFileWithPassingScenario() |
||
119 | |||
120 | /** |
||
121 | * @Given /^a feature file with failing scenario$/ |
||
122 | */ |
||
123 | public function thereIsFeatureFileWithFailingScenario() |
||
144 | |||
145 | /** |
||
146 | * @Given /^a feature file with scenario with missing step$/ |
||
147 | */ |
||
148 | public function thereIsFeatureFileWithScenarioWithMissingStep() |
||
165 | |||
166 | /** |
||
167 | * @Given /^a feature file with scenario with pending step$/ |
||
168 | */ |
||
169 | public function thereIsFeatureFileWithScenarioWithPendingStep() |
||
190 | |||
191 | /** |
||
192 | * @When /^I run Behat$/ |
||
193 | */ |
||
194 | public function iRunBehat() |
||
201 | |||
202 | /** |
||
203 | * @Then /^it should pass$/ |
||
204 | */ |
||
205 | View Code Duplication | public function itShouldPass() |
|
215 | |||
216 | /** |
||
217 | * @Then /^it should pass with(?: "([^"]+)"|:)$/ |
||
218 | */ |
||
219 | public function itShouldPassWith($expectedOutput) |
||
224 | |||
225 | /** |
||
226 | * @Then /^it should fail$/ |
||
227 | */ |
||
228 | View Code Duplication | public function itShouldFail() |
|
238 | |||
239 | /** |
||
240 | * @Then /^it should fail with(?: "([^"]+)"|:)$/ |
||
241 | */ |
||
242 | public function itShouldFailWith($expectedOutput) |
||
247 | |||
248 | /** |
||
249 | * @Then /^it should end with(?: "([^"]+)"|:)$/ |
||
250 | */ |
||
251 | public function itShouldEndWith($expectedOutput) |
||
255 | |||
256 | /** |
||
257 | * @param string $expectedOutput |
||
258 | */ |
||
259 | private function assertOutputMatches($expectedOutput) |
||
277 | |||
278 | /** |
||
279 | * @return string |
||
280 | */ |
||
281 | private function getProcessOutput() |
||
287 | |||
288 | /** |
||
289 | * @return int |
||
290 | */ |
||
291 | private function getProcessExitCode() |
||
297 | |||
298 | /** |
||
299 | * @throws \BadMethodCallException |
||
300 | */ |
||
301 | private function assertProcessIsAvailable() |
||
307 | |||
308 | /** |
||
309 | * @return string |
||
310 | * |
||
311 | * @throws \RuntimeException |
||
312 | */ |
||
313 | private static function findPhpBinary() |
||
322 | } |
||
323 |
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.