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 |
||
17 | class RedisTest extends TestCase |
||
18 | { |
||
19 | /** |
||
20 | * @var Cache\Redis |
||
21 | * @since 1.0 |
||
22 | */ |
||
23 | private $instance; |
||
24 | |||
25 | /** |
||
26 | * Tests for the correct Psr\Cache return values. |
||
27 | * |
||
28 | * @return void |
||
29 | * |
||
30 | * @coversNothing |
||
31 | * @since 1.0 |
||
32 | */ |
||
33 | public function testPsrCache() |
||
43 | |||
44 | /** |
||
45 | * Tests the Joomla\Cache\Redis::get and Joomla\Cache\Redis::set methods. |
||
46 | * |
||
47 | * @return void |
||
48 | * |
||
49 | * @covers Joomla\Cache\Redis::get |
||
50 | * @covers Joomla\Cache\Redis::set |
||
51 | * @covers Joomla\Cache\Redis::connect |
||
52 | * @since 1.0 |
||
53 | */ |
||
54 | public function testGetAndSet() |
||
67 | |||
68 | /** |
||
69 | * Tests the Joomla\Cache\Redis::get and Joomla\Cache\Redis::set methods with timeout |
||
70 | * |
||
71 | * @return void |
||
72 | * |
||
73 | * @covers Joomla\Cache\Redis::get |
||
74 | * @covers Joomla\Cache\Redis::set |
||
75 | * @covers Joomla\Cache\Redis::connect |
||
76 | * @since 1.0 |
||
77 | */ |
||
78 | public function testGetAndSetWithTimeout() |
||
92 | |||
93 | /** |
||
94 | * Tests the Joomla\Cache\Redis::clear method. |
||
95 | * |
||
96 | * @return void |
||
97 | * |
||
98 | * @covers Joomla\Cache\Redis::clear |
||
99 | * @covers Joomla\Cache\Redis::connect |
||
100 | * @since 1.0 |
||
101 | */ |
||
102 | View Code Duplication | public function testClear() |
|
119 | |||
120 | /** |
||
121 | * Tests the Joomla\Cache\Redis::exists method. |
||
122 | * |
||
123 | * @return void |
||
124 | * |
||
125 | * @covers Joomla\Cache\Redis::connect |
||
126 | * @covers Joomla\Cache\Redis::exists |
||
127 | * @since 1.0 |
||
128 | */ |
||
129 | public function testExists() |
||
143 | |||
144 | /** |
||
145 | * Tests the Joomla\Cache\Redis::remove method. |
||
146 | * |
||
147 | * @return void |
||
148 | * |
||
149 | * @covers Joomla\Cache\Redis::connect |
||
150 | * @covers Joomla\Cache\Redis::remove |
||
151 | * @since 1.0 |
||
152 | */ |
||
153 | |||
154 | public function testRemove() |
||
169 | |||
170 | /** |
||
171 | * Tests the Joomla\Cache\Redis::getMultiple method. |
||
172 | * |
||
173 | * @return void |
||
174 | * |
||
175 | * @covers Joomla\Cache\Redis::getMultiple |
||
176 | * @since 1.0 |
||
177 | */ |
||
178 | public function testGetMultiple() |
||
208 | |||
209 | /** |
||
210 | * Tests the Joomla\Cache\Redis::setMultiple method. |
||
211 | * |
||
212 | * @return void |
||
213 | * |
||
214 | * @covers Joomla\Cache\Redis::setMultiple |
||
215 | * @since 1.0 |
||
216 | */ |
||
217 | public function testSetMultiple() |
||
234 | |||
235 | /** |
||
236 | * Tests the Joomla\Cache\Redis::removeMultiple method. |
||
237 | * |
||
238 | * @return void |
||
239 | * |
||
240 | * @covers Joomla\Cache\Redis::removeMultiple |
||
241 | * @since 1.0 |
||
242 | */ |
||
243 | View Code Duplication | public function removeMultiple() |
|
259 | |||
260 | /** |
||
261 | * Setup the tests. |
||
262 | * |
||
263 | * @return void |
||
264 | * |
||
265 | * @covers Joomla\Cache\Redis::__construct |
||
266 | * @since 1.0 |
||
267 | */ |
||
268 | protected function setUp() |
||
322 | |||
323 | /** |
||
324 | * Flush all data before each test |
||
325 | * |
||
326 | * @return void |
||
327 | * |
||
328 | * @since 1.0 |
||
329 | */ |
||
330 | protected function assertPreConditions() |
||
337 | |||
338 | /** |
||
339 | * Teardown the test. |
||
340 | * |
||
341 | * @return void |
||
342 | * |
||
343 | * @since 1.0 |
||
344 | */ |
||
345 | protected function tearDown() |
||
352 | } |
||
353 |
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.