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 | final class MongoCacheTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | /** |
||
16 | * set up each test. |
||
17 | * |
||
18 | * @return void |
||
19 | */ |
||
20 | public function setUp() |
||
24 | |||
25 | /** |
||
26 | * Tear down each test. |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | public function tearDown() |
||
34 | |||
35 | /** |
||
36 | * Verify cache is removed when expired. |
||
37 | * |
||
38 | * @test |
||
39 | * @covers ::set |
||
40 | * @expectedException \InvalidArgumentException |
||
41 | * @expectedExceptionMessage TTL value must be an integer >= 1 and <= 86400 |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | View Code Duplication | public function setTtlIsLessThanOne() |
|
53 | |||
54 | /** |
||
55 | * Verify cache is removed when expired. |
||
56 | * |
||
57 | * @test |
||
58 | * @covers ::set |
||
59 | * @expectedException \InvalidArgumentException |
||
60 | * @expectedExceptionMessage TTL value must be an integer >= 1 and <= 86400 |
||
61 | * |
||
62 | * @return void |
||
63 | */ |
||
64 | View Code Duplication | public function setTtlIsGreaterThanMax() |
|
72 | |||
73 | /** |
||
74 | * Verify cache is removed when expired. |
||
75 | * |
||
76 | * @test |
||
77 | * @covers ::get |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public function getNotFound() |
||
87 | |||
88 | /** |
||
89 | * Verify cache is removed when expired. |
||
90 | * |
||
91 | * @test |
||
92 | * @covers ::__construct |
||
93 | * @covers ::set |
||
94 | * @covers ::get |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | public function getExpired() |
||
129 | |||
130 | /** |
||
131 | * Verify construct throws with invalid parameters. |
||
132 | * |
||
133 | * @param mixed $collection The collection containing the cached data. |
||
134 | * @param mixed $defaultTimeToLive The default time to live in seconds. |
||
135 | * |
||
136 | * @test |
||
137 | * @covers ::__construct |
||
138 | * @dataProvider badConstructorData |
||
139 | * @expectedException \InvalidArgumentException |
||
140 | * @expectedExceptionMessage TTL value must be an integer >= 1 and <= 86400 |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | public function constructWithBadData($collection, $defaultTimeToLive) |
||
148 | |||
149 | /** |
||
150 | * Data provider for constructWithBadData. |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | public function badConstructorData() |
||
166 | |||
167 | /** |
||
168 | * Helper method to get a mongo collection for testing. |
||
169 | * |
||
170 | * @return \MongoDB\Collection |
||
171 | */ |
||
172 | private static function getMongoCollection() |
||
179 | } |
||
180 |
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.