Completed
Push — master ( 023774...de379e )
by Siro Díaz
02:14
created

ArrayListTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 224
Duplicated Lines 54.46 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

18 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() 23 23 1
A testDeleteException() 0 4 1
A testDelete() 15 15 1
A testShift() 0 8 1
A testPop() 0 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() 10 10 1
A testClear() 0 10 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
use DataStructures\Lists\ArrayList;
4
use PHPUnit\Framework\TestCase;
5
6
class ArrayListTest extends TestCase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
    private $list;
8
9
    public function setUp() {
10
        $this->list = new ArrayList();
11
    }
12
13 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...
14
        $this->assertEquals($this->list->size(), 0);
15
        $this->list->push(true);
16
        $this->list->push(2);
17
        $this->assertEquals($this->list->size(), 2);
18
    }
19
20
    public function testEmpty() {
21
        $this->assertTrue($this->list->empty());
22
    }
23
24 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...
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() {
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...
41
        $this->assertNull($this->list->getLast());
42
        $this->list->push(true);
43
        $this->list->push(50);
44
        $this->list->push("string");
45
        $this->assertEquals("string", $this->list->getLast());
46
    }
47
48 View Code Duplication
    public function testInsert() {
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...
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() {
73
        $this->expectException(OutOfBoundsException::class);
74
        $this->list->delete(10);
75
    }
76
    
77 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...
78
        $this->list->push(20);
79
        $this->list->push(true);
80
        $this->list->push(15);
81
        $this->list->push(3.14);
82
        $this->list->push("string");
83
        
84
        $this->assertEquals($this->list->delete(4), "string");
85
        $this->assertEquals(3.14, $this->list->delete(3));
86
        
87
        $this->assertEquals(3, $this->list->size());
88
        $this->assertEquals(true, $this->list->delete(1));
89
        $this->assertEquals(false, $this->list->empty());
90
        $this->assertEquals(20, $this->list->delete(0));
91
    }
92
    
93
    public function testShift() {
94
        $this->list->push(20);
95
        $this->list->push(true);
96
        $this->assertEquals(20, $this->list->shift());
97
        $this->assertEquals(1, $this->list->size());
98
        $this->assertEquals(true, $this->list->shift());
99
        $this->assertEquals(true, $this->list->empty());
100
    }
101
    
102
    public function testPop() {
103
        $this->list->push(20);
104
        $this->list->push(true);
105
        $this->list->push(15);
106
        $this->list->push(3.14);
107
        $this->list->push("string");
108
        
109
        $this->assertEquals($this->list->pop(), "string");
110
        $this->assertEquals(3.14, $this->list->pop());
111
        $this->list->insert(1, ['hello']);
112
        $this->assertEquals(15, $this->list->pop());
113
        $this->assertTrue($this->list->pop());
114
        $this->assertSame($this->list->pop(), 'hello');
115
        $this->assertSame($this->list->pop(), 20);
116
        $this->assertTrue($this->list->empty());
117
    }
118
    
119
    public function testPopException() {
120
        $this->expectException(OutOfBoundsException::class);
121
        $this->list->pop();
122
    }
123
    
124 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...
125
        $this->list->unshift(999);
126
        $this->assertEquals(1, $this->list->size());
127
        $this->assertEquals(999, $this->list->get(0));
128
        $this->assertEquals($this->list->getLast(), 999);
129
        $this->list->unshift(888);
130
        $this->assertEquals(2, $this->list->size());
131
        $this->assertEquals(888, $this->list->get(0));
132
        $this->assertEquals(999, $this->list->get(1));
133
        $this->assertEquals($this->list->getLast(), 999);
134
        $this->list->unshift(777);
135
        $this->assertEquals(3, $this->list->size());
136
        $this->assertEquals(777, $this->list->get(0));
137
        $this->assertEquals(888, $this->list->get(1));
138
        $this->assertEquals(999, $this->list->get(2));
139
        $this->assertEquals($this->list->getLast(), 999);
140
    }
141
    
142 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...
143
        $this->list->push(20);
144
        $this->list->push(true);
145
        $this->list->push(15);
146
        $this->list->push(3.14);
147
        $this->list->push("string");
148
        $this->assertEquals($this->list->get(0), 20);
149
        $this->assertTrue($this->list->get(1));
150
        $this->assertEquals($this->list->get(2), 15);
151
        $this->assertEquals($this->list->get(3), 3.14);
152
        $this->assertEquals($this->list->get(4), "string");
153
    }
154
    
155
    public function testGetWithException() {
156
        $this->expectException(OutOfBoundsException::class);
157
        $this->list->get(5);
158
    }
159
160
    public function testGetOutOfBound() {
161
        $this->expectException(OutOfBoundsException::class);
162
        $this->list->get(-1);
163
    }
164
    
165 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...
166
        foreach($this->list->getAll() as $node) {
167
            $this->assertNull($node);
168
        }
169
        
170
        $this->list->push(20);
171
        $this->list->push(true);
172
        $this->list->push(15);
173
        $this->list->push(3.14);
174
        $this->list->push("string");
175
        $data = [];
176
        foreach($this->list->getAll() as $node) {
177
            $data[] = $node;
178
        }
179
        $this->assertCount(5, $data);
180
        $this->assertSame([20, true, 15, 3.14, "string"], $data);
181
    }
182
    
183 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...
184
        $this->list->push(20);
185
        $this->list->push(true);
186
        $this->list->push(15);
187
        $this->list->push(3.14);
188
        $this->list->push("string");
189
        $this->list->pop();
190
        $nodes = $this->list->toArray();
191
        $this->assertSame([20, true, 15, 3.14], $nodes);
192
    }
193
    
194
    public function testClear() {
195
        $this->list->push(20);
196
        $this->list->push(true);
197
        $this->list->push(15);
198
        $this->list->push(3.14);
199
        $this->list->push("string");
200
        $this->list->clear();
201
        $this->assertEmpty($this->list->toArray());
202
        $this->assertEquals($this->list->size(), 0);
203
    }
204
205
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
206
    public function testIterator() {
207
        $this->list->push(20);
208
        $this->list->push(true);
209
        $this->list->push(15);
210
        $this->list->push(3.14);
211
        $this->list->push("string");
212
213
        $this->list->shift();
214
        $this->assertEquals(4, count($this->list));
215
        $expectedResult = [];
216
        $result = [];
217
        foreach($this->list->getAll() as $node) {
218
            $expectedResult[] = $node;
219
        }
220
        
221
        foreach($this->list as $index => $val) {
222
            echo $index ." ----- ". $val . PHP_EOL;
223
            $result[] = $val;
224
        }
225
226
        $this->assertSame($expectedResult, $result);
227
    }
228
    */
229
}