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 ArrayDebuggerTest extends \PHPUnit_Framework_TestCase |
||
| 6 | { |
||
| 7 | |||
| 8 | public $base = array( |
||
| 9 | 'hum' => 1, |
||
| 10 | 'dois' => 2, |
||
| 11 | 'tres' => 3 |
||
| 12 | ); |
||
| 13 | |||
| 14 | public function testGetter() { |
||
| 15 | |||
| 16 | $array = new ArrayDebugger($this->base); |
||
| 17 | $t = $this; |
||
| 18 | |||
| 19 | $array->setLogger(function($retorno) use($t) { |
||
| 20 | $t->assertEquals($retorno['type'], ArrayDebugger::TYPE_GET); |
||
| 21 | }); |
||
| 22 | |||
| 23 | $teste = $array['hum']; |
||
| 24 | |||
| 25 | $array->setLogger(function($retorno) use($t) { |
||
| 26 | $t->assertNotEquals($retorno['type'], ArrayDebugger::TYPE_GET); |
||
| 27 | }); |
||
| 28 | |||
| 29 | $array['quatro'] = 4; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * offsetExists |
||
| 33 | */ |
||
| 34 | isset($array['hum']); |
||
| 35 | /** |
||
| 36 | * offsetUnset |
||
| 37 | */ |
||
| 38 | unset($array['quatro']); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testSetter() { |
||
| 42 | |||
| 43 | $array = new ArrayDebugger($this->base); |
||
| 44 | |||
| 45 | $t = $this; |
||
| 46 | $array->setLogger(function($retorno) use($t) { |
||
| 47 | $t->assertEquals($retorno['type'], ArrayDebugger::TYPE_SET); |
||
| 48 | }); |
||
| 49 | |||
| 50 | $array['quatro'] = 4; |
||
| 51 | |||
| 52 | $array->setLogger(function($retorno) use($t) { |
||
| 53 | $t->assertNotEquals($retorno['type'], ArrayDebugger::TYPE_SET); |
||
| 54 | }); |
||
| 55 | |||
| 56 | $teste = $array['hum']; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * offsetExists |
||
| 60 | */ |
||
| 61 | isset($array['hum']); |
||
| 62 | /** |
||
| 63 | * offsetUnset |
||
| 64 | */ |
||
| 65 | unset($array['quatro']); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testIsset() { |
||
| 69 | |||
| 70 | $array = new ArrayDebugger($this->base); |
||
| 71 | |||
| 72 | $t = $this; |
||
| 73 | $array->setLogger(function($retorno) use($t) { |
||
| 74 | $t->assertEquals($retorno['type'], ArrayDebugger::TYPE_EXISTS); |
||
| 75 | }); |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * offsetExists |
||
| 80 | */ |
||
| 81 | isset($array['hum']); |
||
| 82 | |||
| 83 | $array->setLogger(function($retorno) use($t) { |
||
| 84 | $t->assertNotEquals($retorno['type'], ArrayDebugger::TYPE_EXISTS); |
||
| 85 | }); |
||
| 86 | |||
| 87 | $array['quatro'] = 4; |
||
| 88 | |||
| 89 | $teste = $array['hum']; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * offsetUnset |
||
| 93 | */ |
||
| 94 | unset($array['quatro']); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function testUnset() { |
||
| 98 | |||
| 99 | $array = new ArrayDebugger($this->base); |
||
| 100 | $t = $this; |
||
| 101 | |||
| 102 | $array->setLogger(function($retorno) use($t) { |
||
| 103 | $t->assertEquals($retorno['type'], ArrayDebugger::TYPE_UNSET); |
||
| 104 | }); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * offsetUnset |
||
| 108 | */ |
||
| 109 | unset($array['dois']); |
||
| 110 | |||
| 111 | $array->setLogger(function($retorno) use($t) { |
||
| 112 | $t->assertNotEquals($retorno['type'], ArrayDebugger::TYPE_UNSET); |
||
| 113 | }); |
||
| 114 | /** |
||
| 115 | * offsetExists |
||
| 116 | */ |
||
| 117 | isset($array['hum']); |
||
| 118 | |||
| 119 | $array['quatro'] = 4; |
||
| 120 | |||
| 121 | $teste = $array['hum']; |
||
| 122 | |||
| 123 | } |
||
| 124 | } |
||
| 125 |