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 |
||
21 | class PriorityCollectionTest extends \PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | public function testPriorities() |
||
24 | { |
||
25 | $collection = new PriorityCollection(); |
||
26 | $collection->insert(0, 0); |
||
27 | $collection->insert(1, 1); |
||
28 | $collection->insert(-1, -1); |
||
29 | |||
30 | $this->assertEquals([1, 0, -1], iterator_to_array($collection)); |
||
31 | } |
||
32 | |||
33 | public function testRemoval() |
||
34 | { |
||
35 | $collection = new PriorityCollection(); |
||
36 | $collection->insert(0, 0); |
||
37 | $collection->insert(-1, -1); |
||
38 | $collection->insert(-1, 1); |
||
39 | $collection->remove(-1); |
||
40 | |||
41 | $this->assertEquals([0], iterator_to_array($collection)); |
||
42 | } |
||
43 | |||
44 | public function testCount() |
||
45 | { |
||
46 | $collection = new PriorityCollection(); |
||
47 | $this->assertEquals(0, count($collection)); |
||
48 | $collection->insert(0, 0); |
||
49 | $collection->insert(-1, -1); |
||
50 | $this->assertEquals(2, count($collection)); |
||
51 | } |
||
52 | } |
||
53 |