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 |
||
30 | class FindCompressionTest extends AbstractFileTestCase |
||
31 | { |
||
32 | /** |
||
33 | * @var FindCompression |
||
34 | */ |
||
35 | protected $findCompression; |
||
36 | |||
37 | /** |
||
38 | * @var ProcessFactory|MockInterface |
||
39 | */ |
||
40 | protected $processFactory; |
||
41 | |||
42 | /** |
||
43 | * @var CompressionFactory|MockInterface |
||
44 | */ |
||
45 | protected $compressionFactory; |
||
46 | |||
47 | public function setUp() |
||
62 | |||
63 | View Code Duplication | public function testGetFileCompressionForNonCompressedFile() |
|
74 | |||
75 | View Code Duplication | public function testGetFileCompressionForGzipFile() |
|
88 | |||
89 | View Code Duplication | public function testGetFileCompressionForZipFile() |
|
102 | |||
103 | public function testWhenTheProcessReturnsAnUnknownCompressionUnknownTypeIsReturned() |
||
104 | { |
||
105 | $process = m::mock(Process::class)->makePartial(); |
||
106 | $process->shouldReceive('mustRun'); |
||
107 | $process->shouldReceive('getOutput') |
||
108 | ->andReturn( |
||
109 | 'text/plain; charset=utf-8 compressed-encoding=application/lzop; charset=binary; charset=binary' |
||
110 | ); |
||
111 | |||
112 | $this->processFactory->shouldReceive('createProcess') |
||
113 | ->andReturn($process); |
||
114 | |||
115 | $file = new LocalFile(static::$dir . 'unknown_compression.test'); |
||
116 | $file->put('random stuff and things 2!'); |
||
117 | |||
118 | static::assertEquals(CompressionFactory::TYPE_UNKNOWN, $this->findCompression->getCompression($file)); |
||
119 | } |
||
120 | |||
121 | View Code Duplication | public function testWhenTheProcessFailsAnExceptionIsThrownOnFindCompression() |
|
122 | { |
||
123 | $process = m::mock(Process::class)->makePartial(); |
||
124 | $process->shouldReceive('isSuccessful')->andReturn(false); |
||
125 | $process->shouldReceive('getCommandLine')->andReturn('cmd'); |
||
126 | $process->shouldReceive('getExitCode')->andReturn(1); |
||
127 | $process->shouldReceive('getExitCodeText')->andReturn('failed'); |
||
128 | $process->shouldReceive('getWorkingDirectory')->andReturn('bla'); |
||
129 | $process->shouldReceive('isOutputDisabled')->andReturn(true); |
||
130 | $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process)); |
||
131 | |||
132 | $this->processFactory->shouldReceive('createProcess') |
||
133 | ->andReturn($process); |
||
134 | |||
135 | $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test'); |
||
136 | $file->put('random stuff and things 2!'); |
||
137 | |||
138 | $this->expectException(ProcessFailedException::class); |
||
139 | |||
140 | $this->findCompression->getCompression($file); |
||
141 | } |
||
142 | |||
143 | View Code Duplication | public function testCanModifyCanModifyLocalFiles() |
|
150 | |||
151 | View Code Duplication | public function testUnableToModifyNonLocalFiles() |
|
159 | |||
160 | View Code Duplication | public function testModifyWillSetTheCompression() |
|
171 | } |
||
172 |
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.