Completed
Push — master ( d060d7...4b3449 )
by Siro Díaz
02:14
created

CircularLinkedListTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 202
Duplicated Lines 67.82 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 137
loc 202
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testSize() 6 6 1
A testEmpty() 0 3 1
A testPush() 15 15 1
A testGetLast() 7 7 1
A testInsert() 0 22 1
A testDeleteException() 0 4 1
A testDelete() 16 16 1
A testShift() 10 10 1
A testPop() 16 16 1
A testPopException() 0 4 1
A testUnshift() 17 17 1
A testGet() 12 12 1
A testGetWithException() 0 4 1
A testGetOutOfBound() 0 4 1
A testGetAll() 17 17 3
A testToArray() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace DataStructures\Tests\Lists;
4
5
use DataStructures\Lists\CircularLinkedList;
6
use PHPUnit\Framework\TestCase;
7
8
class CircularLinkedListTest extends TestCase {
9
    private $list;
10
11
    public function setUp() {
12
        $this->list = new CircularLinkedList();
13
    }
14
15 View Code Duplication
    public function testSize() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
        $this->assertEquals($this->list->size(), 0);
17
        $this->list->push(true);
18
        $this->list->push(2);
19
        $this->assertEquals($this->list->size(), 2);
20
    }
21
22
    public function testEmpty() {
23
        $this->assertTrue($this->list->empty());
24
    }
25
26 View Code Duplication
    public function testPush() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
        $this->list->push(20);
28
        $this->assertEquals(1, $this->list->size());
29
        $this->assertEquals(20, $this->list->get(0));
30
        $this->list->push(true);
31
        
32
        $this->assertEquals(2, $this->list->size());
33
        
34
        $this->assertTrue($this->list->get(1));
35
        
36
        $this->list->push(30);
37
        $this->assertEquals(20, $this->list->get(0));
38
        $this->assertEquals(30, $this->list->get(1));
39
        $this->assertEquals(true, $this->list->get(2));
40
    }
41
    
42 View Code Duplication
    public function testGetLast() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
        $this->assertNull($this->list->getLast());
44
        $this->list->push(true);
45
        $this->list->push(50);
46
        $this->list->push("string");
47
        $this->assertEquals("string", $this->list->getLast());
48
    }
49
50
    public function testInsert() {
51
        $this->list->insert(0, 100);
52
        $this->assertEquals(100, $this->list->get(0));
53
        $this->assertEquals(1, $this->list->size());
54
        
55
        $this->list->insert(0, 200);
56
        $this->assertEquals(2, $this->list->size());
57
        $this->assertEquals(200, $this->list->get(0));
58
        $this->assertEquals(100, $this->list->get(1));
59
        $this->assertEquals($this->list->getLast(), 100);
60
        
61
        $this->list->insert(1, 300);
62
        $this->assertEquals(3, $this->list->size());
63
        $this->assertEquals(200, $this->list->get(0));
64
        $this->assertEquals(300, $this->list->get(1));
65
        $this->assertEquals(100, $this->list->get(2));
66
        $this->list->insert(2, 1000);
67
        $this->assertEquals($this->list->get(2), 1000);
68
        $this->assertEquals($this->list->get(3), 100);
69
        $this->list->insert(6, true);
70
        $this->assertTrue($this->list->get(4));
71
    }
72
    
73
    public function testDeleteException() {
74
        $this->expectException(\OutOfBoundsException::class);
75
        $this->list->delete(10);
76
    }
77
78 View Code Duplication
    public function testDelete() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
        $this->list->push(20);
80
        $this->list->push(true);
81
        $this->list->push(15);
82
        $this->list->push(3.14);
83
        $this->list->push("string");
84
        
85
        // echo $this->list->get(0) . $this->list->get(1) . $this->list->get(2) . $this->list->get(3) . $this->list->get(4) . PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
        $this->assertEquals($this->list->delete(4), "string");
87
        $this->assertEquals(3.14, $this->list->delete(3));
88
        
89
        $this->assertEquals(3, $this->list->size());
90
        $this->assertEquals(true, $this->list->delete(1));
91
        $this->assertEquals(false, $this->list->empty());
92
        $this->assertEquals(20, $this->list->delete(0));
93
    }
94
    
95 View Code Duplication
    public function testShift() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
        $this->assertNull($this->list->shift());
97
        $this->list->push(20);
98
        $this->list->push(true);
99
        $this->assertEquals(20, $this->list->shift());
100
        $this->assertEquals(1, $this->list->size());
101
        $this->assertEquals(true, $this->list->shift());
102
        $this->assertEquals(true, $this->list->empty());
103
        $this->assertNull($this->list->shift());
104
    }
105
    
106 View Code Duplication
    public function testPop() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
        $this->list->push(20);
108
        $this->list->push(true);
109
        $this->list->push(15);
110
        $this->list->push(3.14);
111
        $this->list->push("string");
112
        
113
        $this->assertEquals($this->list->pop(), "string");
114
        $this->assertEquals(3.14, $this->list->pop());
115
        $this->list->insert(1, ['hello']);
116
        $this->assertEquals(15, $this->list->pop());
117
        $this->assertTrue($this->list->pop());
118
        $this->assertSame($this->list->pop(), ['hello']);
119
        $this->assertSame($this->list->pop(), 20);
120
        $this->assertTrue($this->list->empty());
121
    }
122
123
    public function testPopException() {
124
        $this->expectException(\OutOfBoundsException::class);
125
        $this->list->pop();
126
    }
127
    
128 View Code Duplication
    public function testUnshift() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
        $this->list->unshift(999);
130
        $this->assertEquals(1, $this->list->size());
131
        $this->assertEquals(999, $this->list->get(0));
132
        $this->assertEquals($this->list->getLast(), 999);
133
        $this->list->unshift(888);
134
        $this->assertEquals(2, $this->list->size());
135
        $this->assertEquals(888, $this->list->get(0));
136
        $this->assertEquals(999, $this->list->get(1));
137
        $this->assertEquals($this->list->getLast(), 999);
138
        $this->list->unshift(777);
139
        $this->assertEquals(3, $this->list->size());
140
        $this->assertEquals(777, $this->list->get(0));
141
        $this->assertEquals(888, $this->list->get(1));
142
        $this->assertEquals(999, $this->list->get(2));
143
        $this->assertEquals($this->list->getLast(), 999);
144
    }
145
    
146 View Code Duplication
    public function testGet() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
        $this->list->push(20);
148
        $this->list->push(true);
149
        $this->list->push(15);
150
        $this->list->push(3.14);
151
        $this->list->push("string");
152
        $this->assertEquals($this->list->get(0), 20);
153
        $this->assertTrue($this->list->get(1));
154
        $this->assertEquals($this->list->get(2), 15);
155
        $this->assertEquals($this->list->get(3), 3.14);
156
        $this->assertEquals($this->list->get(4), "string");
157
    }
158
159
    public function testGetWithException() {
160
        $this->expectException(\OutOfBoundsException::class);
161
        $this->list->get(5);
162
    }
163
164
    public function testGetOutOfBound() {
165
        $this->expectException(\OutOfBoundsException::class);
166
        $this->list->get(-1);
167
    }
168
169 View Code Duplication
    public function testGetAll() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
        foreach($this->list->getAll() as $node) {
171
            $this->assertNull($node);
172
        }
173
        
174
        $this->list->push(20);
175
        $this->list->push(true);
176
        $this->list->push(15);
177
        $this->list->push(3.14);
178
        $this->list->push("string");
179
        $data = [];
180
        foreach($this->list->getAll() as $node) {
181
            $data[] = $node;
182
        }
183
        $this->assertCount(5, $data);
184
        $this->assertSame([20, true, 15, 3.14, "string"], $data);
185
    }
186
    
187 View Code Duplication
    public function testToArray() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
        $this->list->push(20);
189
        $this->list->push(true);
190
        $this->list->push(15);
191
        $this->list->push(3.14);
192
        $this->list->push("string");
193
        $this->list->pop();
194
        $nodes = $this->list->toArray();
195
        $this->assertCount(4, $nodes);
196
        $this->assertSame([20, true, 15, 3.14], $nodes);
197
    }
198
    
199 View Code Duplication
    public function testClear() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
        $this->list->push(20);
201
        $this->list->push(true);
202
        $this->list->push(15);
203
        $this->list->push(3.14);
204
        $this->list->push("string");
205
        $this->list->clear();
206
        $this->assertEmpty($this->list->toArray());
207
        $this->assertEquals(0, $this->list->size());
208
    }
209
}