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 |
||
| 5 | class HSPHPTest extends PHPUnit_Framework_TestCase |
||
|
|
|||
| 6 | { |
||
| 7 | public function testFetchArray() |
||
| 8 | { |
||
| 9 | $hs = new HSAL('localhost', 'hstest', HSAL::DRIVER_HSPHP); |
||
| 10 | |||
| 11 | $result = $hs->fetchArray('test', ['id','name','cnt'], [HSAL::INDEX_PRIMARY => 3]); |
||
| 12 | |||
| 13 | $this->assertTrue(is_array($result)); |
||
| 14 | $this->assertEquals(3, count($result)); |
||
| 15 | $this->assertEquals(3, $result[0]); |
||
| 16 | $this->assertEquals('page 3', $result[1]); |
||
| 17 | $this->assertEquals(0, $result[2]); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function testFetchAssoc() |
||
| 21 | { |
||
| 22 | $hs = new HSAL('localhost', 'hstest', HSAL::DRIVER_HSPHP); |
||
| 23 | |||
| 24 | $result = $hs->fetchAssoc('test', ['id','name','cnt'], [HSAL::INDEX_PRIMARY => 3]); |
||
| 25 | |||
| 26 | $this->assertTrue(is_array($result)); |
||
| 27 | $this->assertEquals(3, count($result)); |
||
| 28 | $this->assertEquals(3, $result['id']); |
||
| 29 | $this->assertEquals('page 3', $result['name']); |
||
| 30 | $this->assertEquals(0, $result['cnt']); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function testFetchColumn() |
||
| 41 | |||
| 42 | public function testFetchAll() |
||
| 57 | |||
| 58 | public function testDelete() |
||
| 59 | { |
||
| 70 | |||
| 71 | public function testInsert() |
||
| 83 | |||
| 84 | View Code Duplication | public function testUpdate() |
|
| 96 | |||
| 97 | View Code Duplication | public function testIncrement() |
|
| 109 | |||
| 110 | View Code Duplication | public function testDecrement() |
|
| 122 | |||
| 123 | } |
||
| 124 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.