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 |
||
| 28 | class FindEncodingTest extends AbstractFileTestCase |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var FindEncoding |
||
| 32 | */ |
||
| 33 | protected $findEncoding; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ProcessFactory|MockInterface |
||
| 37 | */ |
||
| 38 | protected $processFactory; |
||
| 39 | |||
| 40 | public function setUp() |
||
| 46 | |||
| 47 | View Code Duplication | public function testGetFileEncodingForASCIIFile() |
|
| 58 | |||
| 59 | View Code Duplication | public function testGetFileEncodingForUtf8File() |
|
| 70 | |||
| 71 | public function testGetFileEncodingForCompressedFile() |
||
| 87 | |||
| 88 | View Code Duplication | public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() |
|
| 89 | { |
||
| 90 | $process = m::mock(Process::class)->makePartial(); |
||
| 91 | $process->shouldReceive('isSuccessful')->andReturn(false); |
||
| 92 | $process->shouldReceive('getCommandLine')->andReturn('cmd'); |
||
| 93 | $process->shouldReceive('getExitCode')->andReturn(1); |
||
| 94 | $process->shouldReceive('getExitCodeText')->andReturn('failed'); |
||
| 95 | $process->shouldReceive('getWorkingDirectory')->andReturn('bla'); |
||
| 96 | $process->shouldReceive('isOutputDisabled')->andReturn(true); |
||
| 97 | $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process)); |
||
| 98 | |||
| 99 | $this->processFactory->shouldReceive('createProcess') |
||
| 100 | ->andReturn($process); |
||
| 101 | |||
| 102 | $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test'); |
||
| 103 | $file->put('random stuff and things 2!'); |
||
| 104 | |||
| 105 | $this->expectException(ProcessFailedException::class); |
||
| 106 | |||
| 107 | $this->findEncoding->getEncoding($file); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function testWhenTheProcessReturnsAnUnknownFileNullIsReturned() |
||
| 111 | { |
||
| 112 | $process = m::mock(Process::class)->makePartial(); |
||
| 113 | $process->shouldReceive('mustRun'); |
||
| 114 | $process->shouldReceive('getOutput')->andReturn('some random stuff with no charset'); |
||
| 115 | |||
| 116 | $this->processFactory->shouldReceive('createProcess') |
||
| 117 | ->andReturn($process); |
||
| 118 | |||
| 119 | $file = new LocalFile(static::$dir . 'unknown_compression.test'); |
||
| 120 | $file->put('random stuff and things 2!'); |
||
| 121 | |||
| 122 | static::assertNull($this->findEncoding->getEncoding($file)); |
||
| 123 | } |
||
| 124 | |||
| 125 | View Code Duplication | public function testCanModifyCanModifyLocalFiles() |
|
| 132 | |||
| 133 | View Code Duplication | public function testUnableToModifyNonLocalFiles() |
|
| 141 | |||
| 142 | public function testModifyWillSetTheEncoding() |
||
| 150 | } |
||
| 151 |
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.