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 |
||
| 5 | class ArrayStoreTest extends TestCase |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var ArrayStore |
||
| 9 | */ |
||
| 10 | private $store; |
||
| 11 | |||
| 12 | public function setUp() |
||
| 13 | { |
||
| 14 | $this->store = new ArrayStore(); |
||
| 15 | } |
||
| 16 | |||
| 17 | public function testSet() |
||
| 18 | { |
||
| 19 | $this->store->set('example.com', 60, ['foo' => 'bar']); |
||
| 20 | |||
| 21 | $this->assertEquals(['foo' => 'bar'], $this->store->get('example.com')); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function testOverwrite() |
||
| 25 | { |
||
| 26 | $this->store->set('example.com', 60, ['foo' => 'bar']); |
||
| 27 | |||
| 28 | $this->assertEquals(['foo' => 'bar'], $this->store->get('example.com')); |
||
| 29 | |||
| 30 | $this->store->set('example.com', 60, ['foo' => 'asd']); |
||
| 31 | |||
| 32 | $this->assertEquals(['foo' => 'asd'], $this->store->get('example.com')); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function testExpire() |
||
| 36 | { |
||
| 37 | $this->store->set('example.com', 3, ['foo' => 'bar']); |
||
| 38 | |||
| 39 | $this->assertNotFalse($this->store->get('example.com')); |
||
| 40 | |||
| 41 | sleep(4); |
||
| 42 | |||
| 43 | $this->assertFalse($this->store->get('example.com')); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function testDelete() |
||
| 47 | { |
||
| 48 | $this->store->set('example.com', 60, ['foo' => 'bar']); |
||
| 49 | |||
| 50 | $this->assertNotFalse($this->store->get('example.com')); |
||
| 51 | |||
| 52 | $this->store->delete('example.com'); |
||
| 53 | |||
| 54 | $this->assertFalse($this->store->get('example.com')); |
||
| 55 | } |
||
| 56 | } |
||
| 57 |