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 |
||
| 16 | class CollectionTest extends \PHPUnit_Framework_TestCase |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Collection |
||
| 20 | */ |
||
| 21 | private $collection; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritdoc} |
||
| 25 | */ |
||
| 26 | public function setup() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Test pushing element onto collection |
||
| 35 | * |
||
| 36 | * @return void |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function testPush() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Test popping element from collection |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function testPop() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Test shifting element from collection |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function testShift() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Test un-shifting element onto collection |
||
| 77 | * |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function testUnshift() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Test adding element to collection |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function testAdd() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Test removing element from array |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public function testRemove() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Test removing key not in collection |
||
| 119 | * |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | public function testRemoveUnsetElement() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Test setting value at key |
||
| 131 | * |
||
| 132 | * @return void |
||
| 133 | */ |
||
| 134 | public function testSet() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Test creating slice of collection |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function testSlice() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Test slice without args returns empty collection |
||
| 161 | * |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | View Code Duplication | public function testSliceWithoutArgs() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Test splicing elements from collection |
||
| 175 | * |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function testSplice() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Test splicing elements from collection |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function testSpliceWithoutArgs() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Test splicing single element into collection |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function testSpliceWithScalarReplacement() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Test splicing array of elements into collection |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function testSpliceWithArrayOfReplacements() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Test mapping callback to new collection |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | public function testMap() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Test collection reduction method |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | public function testReduce() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Test sorting collection by callback |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function testSort() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Test sorting collection by callback and retaining element keys/indicies |
||
| 278 | * |
||
| 279 | * @return void |
||
| 280 | */ |
||
| 281 | View Code Duplication | public function testAsort() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Test applying a callback to each element of the collection |
||
| 296 | * |
||
| 297 | * @reutrn void |
||
| 298 | */ |
||
| 299 | public function testEach() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Test each method breaks when receiving false from callback |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | public function testEachWithEarlyReturn() |
||
| 325 | } |
||
| 326 |
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.