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