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 |
||
| 7 | class FTPTest extends \PHPUnit_Framework_TestCase |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var FTP |
||
| 11 | */ |
||
| 12 | protected $object; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Test config data. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $config = [ |
||
| 20 | 'server' => 'localhost', |
||
| 21 | 'username' => 'jens', |
||
| 22 | 'password' => 'pw123', /*, |
||
|
|
|||
| 23 | 'port' => 21, |
||
| 24 | 'passive' => false*/ |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Sets up the fixture, for example, opens a network connection. |
||
| 29 | * This method is called before a test is executed. |
||
| 30 | */ |
||
| 31 | protected function setUp() |
||
| 32 | { |
||
| 33 | //$this->object = new FTP($this->config['server'], $this->config['username'], $this->config['password']); |
||
| 34 | $this->generator = new \PHPUnit_Framework_MockObject_Generator(); |
||
| 35 | $mock = $this->generator->getMock( |
||
| 36 | // classname |
||
| 37 | 'FTPMock', |
||
| 38 | // methods |
||
| 39 | [ |
||
| 40 | 'upload', |
||
| 41 | 'download', |
||
| 42 | 'deleteFile', |
||
| 43 | 'renameOrMove', |
||
| 44 | 'createDirectory', |
||
| 45 | 'isDir', |
||
| 46 | 'isFile', |
||
| 47 | 'deleteDirectory', |
||
| 48 | 'setPermissions', |
||
| 49 | 'fileSize', |
||
| 50 | 'getDirectoryContent', |
||
| 51 | ], |
||
| 52 | // constructor args |
||
| 53 | [], |
||
| 54 | // classname |
||
| 55 | '', |
||
| 56 | // invoke (parent)constructor |
||
| 57 | false |
||
| 58 | ); |
||
| 59 | |||
| 60 | $this->object = $mock; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Tears down the fixture, for example, closes a network connection. |
||
| 65 | * This method is called after a test is executed. |
||
| 66 | */ |
||
| 67 | protected function tearDown() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @covers Koch\Request\FTP::upload |
||
| 74 | */ |
||
| 75 | public function testUpload() |
||
| 76 | { |
||
| 77 | // setup method mock |
||
| 78 | $this->object->expects($this->any())->method('upload')->will($this->returnValue(true)); |
||
| 79 | |||
| 80 | $sourceFile = 'file.txt'; |
||
| 81 | $destinationFile = 'file.txt'; |
||
| 82 | $transferMode = '1'; // FTP_ASCII = 1, FTP_BINARY = 2 |
||
| 83 | |||
| 84 | $result = $this->object->upload($sourceFile, $destinationFile, $transferMode); |
||
| 85 | //var_dump($this->object->errors); |
||
| 86 | $this->assertTrue($result); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @covers Koch\Request\FTP::download |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function testDownload() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @covers Koch\Request\FTP::deleteFile |
||
| 107 | */ |
||
| 108 | public function testDeleteFile() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @covers Koch\Request\FTP::renameOrMove |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function testRenameOrMove() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @covers Koch\Request\FTP::createDirectory |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function testCreateDirectory() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @covers Koch\Request\FTP::deleteDirectory |
||
| 152 | * |
||
| 153 | * @todo Implement testDeleteDirectory(). |
||
| 154 | */ |
||
| 155 | public function testDeleteDirectory() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function setPermissionsDataprovider() |
||
| 173 | { |
||
| 174 | return [ |
||
| 175 | ['111', '111'], |
||
| 176 | ['110', '110'], |
||
| 177 | ['444', '555'], |
||
| 178 | ['412', '512'], |
||
| 179 | ['641', '751'], |
||
| 180 | ['666', '777'], |
||
| 181 | ['400', '500'], |
||
| 182 | ['040', '050'], |
||
| 183 | ['004', '005'], |
||
| 184 | ]; |
||
| 185 | } |
||
| 186 | /** |
||
| 187 | * @dataProvider setPermissionsDataprovider |
||
| 188 | * @covers Koch\Request\FTP::setPermissions |
||
| 189 | */ |
||
| 190 | public function testSetPermissions($set, $expect) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @covers Koch\Request\FTP::isFile |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function testIsFile() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @covers Koch\Request\FTP::fileSize |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function testFileSize() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @covers Koch\Request\FTP::isDir |
||
| 232 | * |
||
| 233 | * @todo Implement testIsDir(). |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function testIsDir() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @covers Koch\Request\FTP::getDirectoryContent |
||
| 248 | */ |
||
| 249 | public function testGetDirectoryContent() |
||
| 256 | } |
||
| 257 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.