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 IterableTraitTest extends \PHPUnit_Framework_TestCase |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var BaseCollection |
||
| 21 | */ |
||
| 22 | private $collection; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | */ |
||
| 27 | public function setUp() |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Verify collection implements ArrayAccess interface |
||
| 36 | * |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | public function testImplementsInterface() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Test collection key position |
||
| 46 | * |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | View Code Duplication | public function testKey() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Test collection current position |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function testCurrent() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Test collection first position |
||
| 74 | * |
||
| 75 | * @depends testKey |
||
| 76 | * @depends testCurrent |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function testFirst() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Test collection last position |
||
| 89 | * |
||
| 90 | * @depends testKey |
||
| 91 | * @depends testCurrent |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function testLast() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Test collection next position |
||
| 103 | * |
||
| 104 | * @depends testKey |
||
| 105 | * @depends testCurrent |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function testNext() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Test collection previous position |
||
| 122 | * |
||
| 123 | * @depends testKey |
||
| 124 | * @depends testCurrent |
||
| 125 | * @depends testNext |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function testPrev() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Test collection valid |
||
| 142 | * |
||
| 143 | * @depends testNext |
||
| 144 | * @depends testLast |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | public function testValid() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Test collection rewind |
||
| 160 | * |
||
| 161 | * @depends testKey |
||
| 162 | * @depends testCurrent |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function testRewind() |
||
| 175 | } |
||
| 176 |
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.