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 |
||
23 | class RedPaymentSlipTest extends PaymentSlipTestCase |
||
24 | { |
||
25 | /** |
||
26 | * The object under test |
||
27 | * |
||
28 | * @var RedPaymentSlip |
||
29 | */ |
||
30 | protected $paymentSlip; |
||
31 | |||
32 | /** |
||
33 | * Setup a slip to test and some default and set attributes to test |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | protected function setUp() |
||
38 | { |
||
39 | $slipData = new RedPaymentSlipData(); |
||
40 | $this->paymentSlip = new RedPaymentSlip($slipData); |
||
41 | |||
42 | $attributes = array(); |
||
43 | $attributes['PosX'] = 0; |
||
44 | $attributes['PosY'] = 0; |
||
45 | $attributes['Width'] = 0; |
||
46 | $attributes['Height'] = 0; |
||
47 | $attributes['Background'] = 'transparent'; |
||
48 | $attributes['FontFamily'] = 'Helvetica'; |
||
49 | $attributes['FontSize'] = '10'; |
||
50 | $attributes['FontColor'] = '#000'; |
||
51 | $attributes['LineHeight'] = 4; |
||
52 | $attributes['TextAlign'] = 'L'; |
||
53 | |||
54 | $this->defaultAttributes = $attributes; |
||
55 | |||
56 | $attributes = array(); |
||
57 | $attributes['PosX'] = 123; |
||
58 | $attributes['PosY'] = 456; |
||
59 | $attributes['Width'] = 987; |
||
60 | $attributes['Height'] = 654; |
||
61 | $attributes['Background'] = '#123456'; |
||
62 | $attributes['FontFamily'] = 'Courier'; |
||
63 | $attributes['FontSize'] = '1'; |
||
64 | $attributes['FontColor'] = '#654321'; |
||
65 | $attributes['LineHeight'] = '15'; |
||
66 | $attributes['TextAlign'] = 'C'; |
||
67 | |||
68 | $this->setAttributes = $attributes; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Tests the getPaymentSlipData method |
||
73 | * |
||
74 | * @return void |
||
75 | * @covers ::getPaymentSlipData |
||
76 | */ |
||
77 | public function testGetPaymentSlipDataIsInstanceOf() |
||
84 | |||
85 | /** |
||
86 | * Tests the default background |
||
87 | * |
||
88 | * @return void |
||
89 | * @covers ::setDefaults |
||
90 | */ |
||
91 | public function testSlipBackgroundDefaultValues() |
||
95 | |||
96 | /** |
||
97 | * Tests the default attributes of the payment reason element |
||
98 | * |
||
99 | * @return void |
||
100 | * @covers ::setDefaults |
||
101 | * @todo Implement once the defaults are set properly in RedPaymentSlip::setDefaults() |
||
102 | */ |
||
103 | public function testPaymentReasonAttrDefaultValues() |
||
109 | |||
110 | /** |
||
111 | * Tests the default attributes of the left IBAN element |
||
112 | * |
||
113 | * @return void |
||
114 | * @covers ::setDefaults |
||
115 | * @todo Implement once the defaults are set properly in RedPaymentSlip::setDefaults() |
||
116 | */ |
||
117 | public function testIbanLeftAttrDefaultValues() |
||
123 | |||
124 | /** |
||
125 | * Tests the default attributes of the right IBAN element |
||
126 | * |
||
127 | * @return void |
||
128 | * @covers ::setDefaults |
||
129 | * @todo Implement once the defaults are set properly in RedPaymentSlip::setDefaults() |
||
130 | */ |
||
131 | public function testIbanRightAttrDefaultValues() |
||
137 | |||
138 | /** |
||
139 | * Tests the setPaymentReasonAttr method |
||
140 | * |
||
141 | * @return void |
||
142 | * @covers ::setPaymentReasonAttr |
||
143 | * @covers ::setAttributes |
||
144 | * @covers ::getPaymentReasonAttr |
||
145 | */ |
||
146 | public function testSetPaymentReasonAttr() |
||
152 | |||
153 | /** |
||
154 | * Tests the setIbanLeftAttr method |
||
155 | * |
||
156 | * @return void |
||
157 | * @covers ::setIbanLeftAttr |
||
158 | * @covers ::setAttributes |
||
159 | * @covers ::getIbanLeftAttr |
||
160 | */ |
||
161 | public function testSetIbanLeftAttr() |
||
167 | |||
168 | /** |
||
169 | * Tests the setIbanRightAttr method |
||
170 | * |
||
171 | * @return void |
||
172 | * @covers ::setIbanRightAttr |
||
173 | * @covers ::setAttributes |
||
174 | * @covers ::getIbanRightAttr |
||
175 | */ |
||
176 | public function testSetIbanRightAttr() |
||
182 | |||
183 | /** |
||
184 | * Tests the setDisplayIban method |
||
185 | * |
||
186 | * @return void |
||
187 | * @covers ::setDisplayIban |
||
188 | * @covers ::getDisplayIban |
||
189 | * @covers ::isBool |
||
190 | */ |
||
191 | View Code Duplication | public function testSetDisplayIban() |
|
209 | |||
210 | /** |
||
211 | * Tests the setDisplayIban method with an invalid parameter |
||
212 | * |
||
213 | * @return void |
||
214 | * @expectedException \InvalidArgumentException |
||
215 | * @expectedExceptionMessage $displayIban is not a boolean. |
||
216 | * @covers ::setDisplayIban |
||
217 | * @covers ::isBool |
||
218 | */ |
||
219 | public function testSetDisplayIbanInvalidParameter() |
||
223 | |||
224 | /** |
||
225 | * Tests the setDisplayPaymentReason method |
||
226 | * |
||
227 | * @return void |
||
228 | * @covers ::setDisplayPaymentReason |
||
229 | * @covers ::getDisplayPaymentReason |
||
230 | * @covers ::isBool |
||
231 | */ |
||
232 | View Code Duplication | public function testSetDisplayPaymentReason() |
|
250 | |||
251 | /** |
||
252 | * Tests the setDisplayPaymentReason method with an invalid parameter |
||
253 | * |
||
254 | * @return void |
||
255 | * @expectedException \InvalidArgumentException |
||
256 | * @expectedExceptionMessage $displayPaymentReason is not a boolean. |
||
257 | * @covers ::setDisplayPaymentReason |
||
258 | * @covers ::isBool |
||
259 | */ |
||
260 | public function testSetDisplayPaymentReasonInvalidParameter() |
||
264 | |||
265 | /** |
||
266 | * Tests the getAllElements method |
||
267 | * |
||
268 | * @return void |
||
269 | * @covers ::getAllElements |
||
270 | */ |
||
271 | View Code Duplication | public function testGetAllElements() |
|
295 | |||
296 | /** |
||
297 | * Tests the getAllElements method when no elements are shown |
||
298 | * |
||
299 | * @return void |
||
300 | * @covers ::getAllElements |
||
301 | */ |
||
302 | View Code Duplication | public function testGetAllElementsNoElementsShown() |
|
318 | |||
319 | /** |
||
320 | * Tests the getAllElements method when all data is disabled |
||
321 | * |
||
322 | * @return void |
||
323 | * @covers ::getAllElements |
||
324 | */ |
||
325 | View Code Duplication | public function testGetAllElementsDisabledData() |
|
342 | } |
||
343 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.