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 |
||
| 3 | class FactsFactoryTest extends PHPUnit_Framework_TestCase |
||
| 4 | { |
||
| 5 | |||
| 6 | public function testMakeLocalBusiness() |
||
| 7 | { |
||
| 8 | $profile = array( |
||
| 9 | 'model' => 'LocalBusiness', |
||
| 10 | 'options' => array( |
||
| 11 | 'base' => array( 'default'=> 'urn:test') |
||
| 12 | ), |
||
| 13 | 'datamapper' => function(array $rawdata){ |
||
| 14 | $data = array(); |
||
| 15 | $data['id'] = $rawdata[0]; |
||
| 16 | $data['businessName'][] = $rawdata[2] . ' ' . $rawdata[1]; |
||
| 17 | $data['businessName'][] = $rawdata[2]; |
||
| 18 | $data['vatID'] = $rawdata[3]; |
||
| 19 | $data['email'] = $rawdata[4]; |
||
| 20 | $data['addressLocality'] = $rawdata[5]; |
||
| 21 | $data['postalCode'] = $rawdata[7]; |
||
| 22 | $data['addressRegion'] = $rawdata[8]; |
||
| 23 | $data['streetAddress'] = $rawdata[9] . ' ' . $rawdata[10] . ', ' . $rawdata[11]; |
||
| 24 | $data['long'] = $rawdata[14]; |
||
| 25 | $data['lat'] = $rawdata[15]; |
||
| 26 | |||
| 27 | return $data; |
||
| 28 | }, |
||
| 29 | ); |
||
| 30 | $rawdata = array( |
||
| 31 | '10042650', |
||
| 32 | 'ERBORISTERIA I PRATI DI GIOVANNA MONAMI', |
||
| 33 | '', |
||
| 34 | '01209991007', |
||
| 35 | '', |
||
| 36 | 'ROMA', |
||
| 37 | 'ROMA', |
||
| 38 | '00195', |
||
| 39 | 'RM', |
||
| 40 | 'VIA', |
||
| 41 | 'ANTONIO MORDINI', |
||
| 42 | '3', |
||
| 43 | '058091', |
||
| 44 | '0580912017145', |
||
| 45 | '12.464163', |
||
| 46 | '41.914001' |
||
| 47 | ); |
||
| 48 | |||
| 49 | $factsFactory = new \BOTK\FactsFactory($profile); |
||
| 50 | $facts = $factsFactory->factualize($rawdata); |
||
| 51 | $structuredData = $facts->asArray(); |
||
| 52 | |||
| 53 | $this->assertInstanceOf('\BOTK\Model\LocalBusiness', $facts); |
||
| 54 | $this->assertEquals(1, count($structuredData['businessName'])); |
||
| 55 | $this->assertEquals($structuredData['vatID'], '01209991007'); |
||
| 56 | $this->assertEquals($structuredData['id'], '10042650'); |
||
| 57 | $this->assertEquals($structuredData['long'], '12.464163'); |
||
| 58 | $this->assertEquals($structuredData['streetAddress'], 'VIA ANTONIO MORDINI, 3'); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testRemoveEmpty() |
||
| 62 | { |
||
| 63 | $profile = array( |
||
| 64 | 'model' => 'LocalBusiness', |
||
| 65 | 'options' => array(), |
||
| 66 | 'datamapper' => function(array $rawdata){return array();}, |
||
| 67 | ); |
||
| 68 | $data = array( |
||
| 69 | 'one' => 'notempty', |
||
| 70 | 'two' => null, |
||
| 71 | 'three' => false, |
||
| 72 | 'four' => 0, |
||
| 73 | 'five' => array(), |
||
| 74 | 'seven' => array(''), |
||
| 75 | 'eight' => array('notempy'), |
||
| 76 | 'nine' => array('notempy','',''), |
||
| 77 | ); |
||
| 78 | $expectedData = array( |
||
| 79 | 'one' => 'notempty', |
||
| 80 | 'eight' => array('notempy'), |
||
| 81 | 'nine' => array('notempy'), |
||
| 82 | ); |
||
| 83 | $factsFactory = new \BOTK\FactsFactory($profile); |
||
| 84 | $this->assertEquals($expectedData, $factsFactory->removeEmpty($data)); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 |