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 |
||
| 8 | class HeaderTest extends TestCase |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @dataProvider validQrTypeProvider |
||
| 12 | */ |
||
| 13 | public function testQrTypeIsValid($value) |
||
| 14 | { |
||
| 15 | $header = new Header(); |
||
| 16 | $header->setQrType($value); |
||
| 17 | $header->setVersion('0100'); |
||
| 18 | $header->setCoding(1); |
||
| 19 | |||
| 20 | $this->assertSame(0, $header->getViolations()->count()); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function validQrTypeProvider() |
||
| 24 | { |
||
| 25 | return [ |
||
| 26 | ['SPC'], |
||
| 27 | ['foo'], |
||
| 28 | ['123'], |
||
| 29 | ['000'], |
||
| 30 | ['A1B'], |
||
| 31 | ['1AB'], |
||
| 32 | ['AB1'], |
||
| 33 | ]; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @dataProvider invalidQrTypeProvider |
||
| 38 | */ |
||
| 39 | public function testQrTypeIsInvalid($value) |
||
| 40 | { |
||
| 41 | $header = new Header(); |
||
| 42 | $header->setQrType($value); |
||
| 43 | $header->setVersion('0100'); |
||
| 44 | $header->setCoding(1); |
||
| 45 | |||
| 46 | $this->assertSame(1, $header->getViolations()->count()); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function invalidQrTypeProvider() |
||
| 50 | { |
||
| 51 | return [ |
||
| 52 | ['SP'], |
||
| 53 | ['SPCC'], |
||
| 54 | ['fo'], |
||
| 55 | ['fooo'], |
||
| 56 | ['12'], |
||
| 57 | ['00'], |
||
| 58 | ['SP*'], |
||
| 59 | ['*SP'], |
||
| 60 | ]; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function testQrTypeIsRequired() |
||
| 64 | { |
||
| 65 | $header = new Header(); |
||
| 66 | $header->setVersion('0100'); |
||
| 67 | $header->setCoding(1); |
||
| 68 | |||
| 69 | $this->assertSame(1, $header->getViolations()->count()); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @dataProvider validVersionProvider |
||
| 74 | */ |
||
| 75 | public function testVersionIsValid($value) |
||
| 76 | { |
||
| 77 | $header = new Header(); |
||
| 78 | $header->setQrType('SPC'); |
||
| 79 | $header->setVersion($value); |
||
| 80 | $header->setCoding(1); |
||
| 81 | |||
| 82 | $this->assertSame(0, $header->getViolations()->count()); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function validVersionProvider() |
||
| 86 | { |
||
| 87 | return [ |
||
| 88 | ['0100'], |
||
| 89 | ['1234'], |
||
| 90 | ['0000'], |
||
| 91 | ['9999'], |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @dataProvider invalidVersionProvider |
||
| 97 | */ |
||
| 98 | public function testVersionIsInvalid($value) |
||
| 99 | { |
||
| 100 | $header = new Header(); |
||
| 101 | $header->setQrType('SPC'); |
||
| 102 | $header->setVersion($value); |
||
| 103 | $header->setCoding(1); |
||
| 104 | |||
| 105 | $this->assertSame(1, $header->getViolations()->count()); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function invalidVersionProvider() |
||
| 109 | { |
||
| 110 | return [ |
||
| 111 | ['010'], |
||
| 112 | ['234'], |
||
| 113 | ['ABCD'], |
||
| 114 | ['abcd'], |
||
| 115 | ['a1b2'], |
||
| 116 | ['1a2b'], |
||
| 117 | ['010*'], |
||
| 118 | ['*010'] |
||
| 119 | ]; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testVersionIsRequired() |
||
| 123 | { |
||
| 124 | $header = new Header(); |
||
| 125 | $header->setQrType('SPC'); |
||
| 126 | $header->setCoding(1); |
||
| 127 | |||
| 128 | $this->assertSame(1, $header->getViolations()->count()); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @dataProvider validCodingProvider |
||
| 133 | */ |
||
| 134 | public function testCodingIsValid($value) |
||
| 135 | { |
||
| 136 | $header = new Header(); |
||
| 137 | $header->setQrType('SPC'); |
||
| 138 | $header->setVersion('0100'); |
||
| 139 | $header->setCoding($value); |
||
| 140 | |||
| 141 | $this->assertSame(0, $header->getViolations()->count()); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function validCodingProvider() |
||
| 145 | { |
||
| 146 | return [ |
||
| 147 | [0], |
||
| 148 | [1], |
||
| 149 | [2], |
||
| 150 | [3], |
||
| 151 | [4], |
||
| 152 | [5], |
||
| 153 | [6], |
||
| 154 | [7], |
||
| 155 | [8], |
||
| 156 | [9], |
||
| 157 | ]; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @dataProvider invalidCodingProvider |
||
| 162 | */ |
||
| 163 | public function testCodingIsInvalid($value) |
||
| 164 | { |
||
| 165 | $header = new Header(); |
||
| 166 | $header->setQrType('SPC'); |
||
| 167 | $header->setVersion('0100'); |
||
| 168 | $header->setCoding($value); |
||
| 169 | |||
| 170 | $this->assertSame(1, $header->getViolations()->count()); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function invalidCodingProvider() |
||
| 174 | { |
||
| 175 | return [ |
||
| 176 | [11], |
||
| 177 | [222], |
||
| 178 | ]; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function testCodingisRequired() |
||
| 182 | { |
||
| 183 | $header = new Header(); |
||
| 184 | $header->setQrType('SPC'); |
||
| 185 | $header->setVersion('0100'); |
||
| 186 | |||
| 187 | $this->assertSame(1, $header->getViolations()->count()); |
||
| 188 | } |
||
| 189 | } |