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 CAPICOMTest extends \PHPUnit_Framework_TestCase { |
||
8 | |||
9 | public static function provideGenerate() { |
||
10 | $data = array(); |
||
11 | for ($i = 0; $i < 100; $i += 25) { |
||
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::MEDIUM); |
||
22 | $actual = CAPICOM::getStrength(); |
||
23 | $this->assertEquals($actual, $strength); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @dataProvider provideGenerate |
||
28 | * @group slow |
||
29 | */ |
||
30 | public function testGenerate($length, $not) { |
||
31 | $rand = new CAPICOM; |
||
32 | $stub = $rand->generate($length); |
||
33 | $this->assertEquals($length, strlen($stub)); |
||
34 | } |
||
35 | |||
36 | } |
||
37 |