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 |
||
20 | final class YamlConvertCommandTest extends CompatibleTestsCase |
||
21 | { |
||
22 | private $app; |
||
23 | private $buffer; |
||
24 | private $cwd; |
||
25 | |||
26 | protected function setUp() |
||
27 | { |
||
28 | $this->app = new Application(); |
||
29 | $this->app->setAutoExit(false); |
||
30 | $this->app->add(new YamlConvertCommand()); |
||
31 | $this->buffer = new BufferedOutput(); |
||
32 | |||
33 | $this->cwd = getcwd(); |
||
34 | chdir(sys_get_temp_dir()); |
||
35 | $this->removeFiles(); |
||
36 | } |
||
37 | |||
38 | protected function tearDown() |
||
39 | { |
||
40 | chdir($this->cwd); |
||
41 | $this->removeFiles(); |
||
42 | } |
||
43 | |||
44 | public function testDefaultInputFileDoesNotExists() |
||
45 | { |
||
46 | $this->app->run(new ArrayInput(['yaml-convert']), $this->buffer); |
||
47 | $this->assertContains('The input file "composer.yaml" does not exist.', $this->buffer->fetch()); |
||
48 | } |
||
49 | |||
50 | public function testCustomInputFileDoesNotExists() |
||
51 | { |
||
52 | $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->buffer); |
||
53 | $this->assertContains('The input file "composer.json" does not exist.', $this->buffer->fetch()); |
||
54 | } |
||
55 | |||
56 | public function testInvalidFormat() |
||
57 | { |
||
58 | $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.neon']), $this->buffer); |
||
59 | $this->assertContains('Invalid input format "neon", must be one of: yaml, yml, json.', $this->buffer->fetch()); |
||
60 | } |
||
61 | |||
62 | public function testSameFormat() |
||
63 | { |
||
64 | file_put_contents('composer.yml', 'name: package'); |
||
65 | $this->app->run(new ArrayInput(['yaml-convert', 'output' => 'composer.yaml']), $this->buffer); |
||
66 | $this->assertContains('Input format "yaml" is same as output format.', $this->buffer->fetch()); |
||
67 | } |
||
68 | |||
69 | public function testConvertYamlToJson() |
||
70 | { |
||
71 | file_put_contents('composer.yaml', 'name: package'); |
||
72 | $this->app->run(new ArrayInput(['yaml-convert']), $this->buffer); |
||
73 | $this->assertContains('Converted "composer.yaml" to "composer.json"', $this->buffer->fetch()); |
||
74 | $this->assertFileExists('composer.json'); |
||
75 | $this->assertEquals("{\n \"name\": \"package\"\n}\n", file_get_contents('composer.json')); |
||
76 | } |
||
77 | |||
78 | public function testConvertYmlToJson() |
||
86 | |||
87 | View Code Duplication | public function testConvertJsonToYaml() |
|
|
|||
88 | { |
||
89 | file_put_contents('composer.json', "{\n \"name\": \"package\"\n}"); |
||
90 | $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->buffer); |
||
91 | $this->assertContains('Converted "composer.json" to "composer.yaml"', $this->buffer->fetch()); |
||
92 | $this->assertFileExists('composer.yaml'); |
||
93 | $this->assertEquals("name: package\n", file_get_contents('composer.yaml')); |
||
95 | |||
96 | View Code Duplication | public function testConvertJsonToYml() |
|
104 | |||
105 | private function removeFiles() |
||
114 | } |
||
115 |
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.