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 |
||
13 | class CacheTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | /** |
||
16 | * @var Cache |
||
17 | */ |
||
18 | public $cache; |
||
19 | |||
20 | /** |
||
21 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
22 | */ |
||
23 | public $storageMock; |
||
24 | |||
25 | /** |
||
26 | * |
||
27 | */ |
||
28 | public function setUp() |
||
33 | |||
34 | /** |
||
35 | * Test that NullStorage Adaptor is called by default. |
||
36 | */ |
||
37 | public function testConstructDefaultNullStorage(){ |
||
42 | |||
43 | /** |
||
44 | * Test that getCacheStorage returns storageMock. |
||
45 | */ |
||
46 | public function testGetCacheStorage(){ |
||
50 | |||
51 | /** |
||
52 | * Test CacheStorage is updated from construct. |
||
53 | */ |
||
54 | public function testWithCacheStorage(){ |
||
61 | |||
62 | /** |
||
63 | * Test that CacheStorageInterface::has() is called |
||
64 | */ |
||
65 | View Code Duplication | public function testHas() |
|
75 | |||
76 | /** |
||
77 | * Test that CacheStorageInterface::set() is called |
||
78 | */ |
||
79 | public function testSet() |
||
94 | |||
95 | /** |
||
96 | * Test that CacheStorageInterface::get() is called |
||
97 | */ |
||
98 | View Code Duplication | public function testGet() |
|
108 | |||
109 | /** |
||
110 | * Test that CacheStorageInterface::delete() is called |
||
111 | */ |
||
112 | View Code Duplication | public function testDelete() |
|
122 | } |
||
123 |
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.