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 UniqIDTest extends \PHPUnit_Framework_TestCase { |
||
| 8 | |||
| 9 | public static function provideGenerate() { |
||
| 10 | $data = array(); |
||
| 11 | for ($i = 0; $i < 100; $i += 5) { |
||
| 12 | $not = $i > 0 ? str_repeat(chr(0), $i) : chr(0); |
||
| 13 | $data[] = array($i, $not); |
||
| 14 | } |
||
| 15 | return $data; |
||
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | */ |
||
| 20 | public function testGetStrength() { |
||
| 21 | $strength = new Strength(Strength::LOW); |
||
| 22 | $actual = UniqID::getStrength(); |
||
| 23 | $this->assertEquals($actual, $strength); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @dataProvider provideGenerate |
||
| 28 | */ |
||
| 29 | public function testGenerate($length, $not) { |
||
| 30 | $rand = new UniqID; |
||
| 31 | $stub = $rand->generate($length); |
||
| 32 | $this->assertEquals($length, strlen($stub)); |
||
| 33 | $this->assertNotEquals($not, $stub); |
||
| 34 | } |
||
| 35 | |||
| 36 | } |
||
| 37 |