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 |
||
| 12 | class RRuleTest extends \PHPUnit_Framework_TestCase { |
||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | function dataForTestRRule() { |
||
| 18 | return array( |
||
| 19 | array('IcalParserRRule1.ics',array("FREQ"=>"WEEKLY","BYDAY"=>"WE")), |
||
| 20 | array('IcalParserRRule2.ics',array("FREQ"=>"WEEKLY","BYDAY"=>"TH","COUNT"=>5)), |
||
| 21 | ); |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @dataProvider dataForTestRRule |
||
| 26 | */ |
||
| 27 | function testGetByPosition ($filename, $rrule) { |
||
| 28 | $parser = new ICalParser(); |
||
| 29 | $this->assertTrue($parser->parseFromFile(dirname(__FILE__)."/data/".$filename)); |
||
| 30 | $events = $parser->getEvents(); |
||
| 31 | $this->assertEquals(1, count($events)); |
||
| 32 | $event = $events[0]; |
||
| 33 | |||
| 34 | $eventRRule = $event->getRrule(); |
||
| 35 | $this->assertEquals(count(array_keys($rrule)), count(array_keys($eventRRule))); |
||
| 36 | foreach($rrule as $k=>$v) { |
||
| 37 | $this->assertEquals($v, $eventRRule[$k]); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @dataProvider dataForTestRRule |
||
| 43 | */ |
||
| 44 | function testGetByArray ($filename, $rrule) { |
||
| 45 | $parser = new ICalParser(); |
||
| 46 | $this->assertTrue($parser->parseFromFile(dirname(__FILE__)."/data/".$filename)); |
||
| 47 | $events = $parser->getEvents(); |
||
| 48 | $this->assertEquals(1, count($events)); |
||
| 49 | $event = $events[0]; |
||
| 50 | |||
| 51 | $eventRRule = $event->getRrule(); |
||
| 52 | $this->assertEquals(count(array_keys($rrule)), count(array_keys($eventRRule))); |
||
| 53 | foreach($rrule as $k=>$v) { |
||
| 54 | $this->assertEquals($v, $eventRRule[$k]); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | } |
||
| 61 | |||
| 62 |