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 SeekableTraitTest extends \PHPUnit_Framework_TestCase |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var BaseCollection |
||
| 21 | */ |
||
| 22 | private $collection; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | */ |
||
| 27 | public function setUp() |
||
| 28 | { |
||
| 29 | parent::setUp(); |
||
| 30 | |||
| 31 | $this->collection = new BaseCollection([1, 2, 3, 'a' => 4, 'b' => 5]); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Verify collection implements ArrayAccess interface |
||
| 36 | * |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | public function testImplementsInterface() |
||
| 40 | { |
||
| 41 | $this->assertInstanceOf(SeekableIterator::class, $this->collection); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Test collection seek method |
||
| 46 | * |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | public function testSeek() |
||
| 50 | { |
||
| 51 | $this->collection->seek('a'); |
||
| 52 | $this->assertEquals(4, $this->collection->current()); |
||
| 53 | |||
| 54 | $this->collection->seek(2); |
||
| 55 | $this->assertEquals(3, $this->collection->current()); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Test invalid offset seek exception |
||
| 60 | * |
||
| 61 | * @expectedException \InvalidArgumentException |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | public function testSeekException() |
||
| 65 | { |
||
| 66 | $this->collection->seek('noop'); |
||
| 67 | } |
||
| 68 | } |
||
| 69 |