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 |
||
6 | class DoublyLinkedListTest extends TestCase { |
||
|
|||
7 | private $list; |
||
8 | |||
9 | public function setUp() { |
||
12 | |||
13 | View Code Duplication | public function testSize() { |
|
19 | |||
20 | public function testEmpty() { |
||
23 | |||
24 | View Code Duplication | public function testPush() { |
|
25 | $this->list->push(20); |
||
26 | $this->assertEquals(1, $this->list->size()); |
||
27 | $this->assertEquals(20, $this->list->get(0)); |
||
28 | $this->list->push(true); |
||
29 | |||
30 | $this->assertEquals(2, $this->list->size()); |
||
31 | |||
32 | $this->assertTrue($this->list->get(1)); |
||
33 | |||
34 | $this->list->push(30); |
||
35 | $this->assertEquals(20, $this->list->get(0)); |
||
36 | $this->assertEquals(30, $this->list->get(1)); |
||
37 | $this->assertEquals(true, $this->list->get(2)); |
||
38 | } |
||
39 | |||
40 | View Code Duplication | public function testGetLast() { |
|
47 | |||
48 | View Code Duplication | public function testInsert() { |
|
49 | $this->list->insert(0, 100); |
||
50 | $this->assertEquals(100, $this->list->get(0)); |
||
51 | $this->assertEquals(1, $this->list->size()); |
||
52 | |||
53 | $this->list->insert(0, 200); |
||
54 | $this->assertEquals(2, $this->list->size()); |
||
55 | $this->assertEquals(200, $this->list->get(0)); |
||
56 | $this->assertEquals(100, $this->list->get(1)); |
||
57 | $this->assertEquals(100, $this->list->getLast()); |
||
58 | |||
59 | $this->list->insert(1, 300); |
||
60 | $this->assertEquals(3, $this->list->size()); |
||
61 | $this->assertEquals(200, $this->list->get(0)); |
||
62 | $this->assertEquals(300, $this->list->get(1)); |
||
63 | $this->assertEquals(100, $this->list->get(2)); |
||
64 | $this->list->insert(2, 1000); |
||
65 | $this->assertEquals(1000, $this->list->get(2)); |
||
66 | $this->assertEquals(100, $this->list->get(3)); |
||
67 | $this->list->insert(6, true); |
||
68 | $this->assertTrue($this->list->get(4)); |
||
69 | $this->assertEquals(5, $this->list->size()); |
||
70 | } |
||
71 | |||
72 | public function testDeleteException() { |
||
76 | |||
77 | View Code Duplication | public function testDelete() { |
|
78 | $this->list->push(20); |
||
92 | |||
93 | View Code Duplication | public function testShift() { |
|
103 | |||
104 | View Code Duplication | public function testPop() { |
|
120 | |||
121 | public function testPopException() { |
||
125 | |||
126 | View Code Duplication | public function testUnshift() { |
|
143 | |||
144 | View Code Duplication | public function testGet() { |
|
156 | |||
157 | public function testGetWithException() { |
||
161 | |||
162 | public function testGetOutOfBound() { |
||
166 | |||
167 | View Code Duplication | public function testGetAll() { |
|
184 | |||
185 | View Code Duplication | public function testToArray() { |
|
195 | |||
196 | View Code Duplication | public function testClear() { |
|
206 | |||
207 | public function testIterator() { |
||
226 | } |
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.