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 | final class CollectionTest extends \PHPUnit_Framework_TestCase |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Set up for all tests. |
||
| 18 | * |
||
| 19 | * @return void |
||
| 20 | */ |
||
| 21 | public function setUp() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Tear down for all tests. |
||
| 35 | * |
||
| 36 | * @return void |
||
| 37 | */ |
||
| 38 | public function tearDown() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Verifies basic usage of the collection. |
||
| 45 | * |
||
| 46 | * @test |
||
| 47 | * @covers ::__construct |
||
| 48 | * @covers ::rewind |
||
| 49 | * @covers ::valid |
||
| 50 | * @covers ::key |
||
| 51 | * @covers ::current |
||
| 52 | * @covers ::next |
||
| 53 | * @covers ::count |
||
| 54 | * |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public function directUsage() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Verifies code does not explode when rewind() consectutively. |
||
| 76 | * |
||
| 77 | * @test |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function consecutiveRewind() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Verifies code does not explode when current() consectutively. |
||
| 99 | * |
||
| 100 | * @test |
||
| 101 | * @covers ::current |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function consecutiveCurrent() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Verifies code does not explode when next() consectutively. |
||
| 115 | * |
||
| 116 | * @test |
||
| 117 | * @covers ::next |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function consecutiveNext() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Verifies count() lazy loads the next result. |
||
| 133 | * |
||
| 134 | * @test |
||
| 135 | * @covers ::count |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function countBasic() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Verifies key() lazy loads the next result. |
||
| 148 | * |
||
| 149 | * @test |
||
| 150 | * @covers ::key |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function key() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Verifies current() lazy loads the next result. |
||
| 163 | * |
||
| 164 | * @test |
||
| 165 | * @covers ::current |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function current() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Verfies current() throws when collection is empty. |
||
| 179 | * |
||
| 180 | * @test |
||
| 181 | * @covers ::current |
||
| 182 | * @expectedException \OutOfBoundsException |
||
| 183 | * |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | public function currentWithEmpty() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Verfies key() throws when collection is empty. |
||
| 195 | * |
||
| 196 | * @test |
||
| 197 | * @covers ::key |
||
| 198 | * @expectedException \OutOfBoundsException |
||
| 199 | * |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | public function keyWithEmpty() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Verify Collection can iterated multiple times. |
||
| 211 | * |
||
| 212 | * @test |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function multiIteration() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Verify Collection can handle an empty response. |
||
| 242 | * |
||
| 243 | * @test |
||
| 244 | * |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | public function emptyResult() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Verify Collection can handle a response with a single item. |
||
| 257 | * |
||
| 258 | * @test |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function oneItemCollection() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Verify current() returns result from the loader given in the constructor. |
||
| 274 | * |
||
| 275 | * @test |
||
| 276 | * @covers ::current |
||
| 277 | * |
||
| 278 | * @return void |
||
| 279 | */ |
||
| 280 | public function currentCustomLoader() |
||
| 302 | } |
||
| 303 |
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.