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 |
||
14 | class CacheTest extends \PHPUnit\Framework\TestCase |
||
15 | { |
||
16 | /** |
||
17 | * @var Cache |
||
18 | */ |
||
19 | public $cache; |
||
20 | |||
21 | /** |
||
22 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
23 | */ |
||
24 | public $storageMock; |
||
25 | |||
26 | /** |
||
27 | * |
||
28 | */ |
||
29 | public function setUp() |
||
34 | |||
35 | /** |
||
36 | * Test that CacheStorageInterface::set() invalid key exception |
||
37 | */ |
||
38 | public function testSetInvalidKey() |
||
44 | |||
45 | /** |
||
46 | * Test that CacheStorageInterface::set() is called |
||
47 | */ |
||
48 | View Code Duplication | public function testSet() |
|
63 | |||
64 | /** |
||
65 | * Test that CacheStorageInterface::set() is called |
||
66 | */ |
||
67 | public function testSetDateInterval() |
||
84 | |||
85 | /** |
||
86 | * Test that CacheStorageInterface::has() invalid key exception |
||
87 | */ |
||
88 | public function testHasInvalidKey() |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Test that CacheStorageInterface::has() is called and returns true on finding key |
||
98 | */ |
||
99 | public function testHasFoundWithKey() |
||
100 | { |
||
101 | $key = 'key'; |
||
102 | $expected = true; |
||
103 | |||
104 | $this->storageMock->expects($this->once()) |
||
105 | ->method('has') |
||
106 | ->with( |
||
107 | $this->equalTo($key) |
||
108 | ) |
||
109 | ->willReturn($expected); |
||
110 | |||
111 | $actual = $this->cache->has($key); |
||
112 | $this->assertEquals($expected, $actual); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Test that CacheStorageInterface::has() is called and returns false on failing to find key |
||
117 | */ |
||
118 | public function testHasFoundNoKey() |
||
119 | { |
||
120 | $key = 'key'; |
||
121 | $expected = false; |
||
122 | |||
123 | $this->storageMock->expects($this->once()) |
||
124 | ->method('has') |
||
125 | ->with( |
||
126 | $this->equalTo($key) |
||
127 | ) |
||
128 | ->willReturn($expected); |
||
129 | |||
130 | $actual = $this->cache->has($key); |
||
131 | $this->assertEquals($expected, $actual); |
||
132 | } |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Test that CacheStorageInterface::has() is called and returns false on failing to find key |
||
137 | */ |
||
138 | public function testGetInvalidKey() |
||
144 | |||
145 | /** |
||
146 | * Test that CacheStorageInterface::get() returns values when key is present. |
||
147 | */ |
||
148 | public function testGetFoundValue() |
||
171 | |||
172 | /** |
||
173 | * Test that CacheStorageInterface::get() doesn't return values and default is returned. |
||
174 | */ |
||
175 | View Code Duplication | public function testGetDefaultValue() |
|
176 | { |
||
177 | $key = 'key'; |
||
178 | $default = 'default value'; |
||
179 | |||
180 | $this->storageMock->expects($this->once()) |
||
181 | ->method('has') |
||
182 | ->with( |
||
183 | $this->equalTo($key) |
||
184 | ) |
||
185 | ->willReturn(false); |
||
186 | |||
187 | $actual = $this->cache->get($key, $default); |
||
188 | $this->assertEquals($default, $actual); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Test that CacheStorageInterface::delete() is called |
||
193 | */ |
||
194 | public function testDelete() |
||
204 | |||
205 | /** |
||
206 | * |
||
207 | */ |
||
208 | public function testGetMultiple(){ |
||
234 | |||
235 | /** |
||
236 | * |
||
237 | */ |
||
238 | public function testSetMultiple(){ |
||
267 | |||
268 | /** |
||
269 | * |
||
270 | */ |
||
271 | public function testSetMultipleWithFailure(){ |
||
307 | |||
308 | /** |
||
309 | * |
||
310 | */ |
||
311 | public function testDeleteMultiple(){ |
||
329 | |||
330 | /** |
||
331 | * |
||
332 | */ |
||
333 | public function testDeleteMultipleFailure(){ |
||
351 | |||
352 | /** |
||
353 | * Test that storage clear is called. |
||
354 | */ |
||
355 | public function testclear(){ |
||
362 | } |
||
363 |
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.