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 |
||
24 | class PaymentSlipTest extends \PHPUnit_Framework_TestCase |
||
25 | { |
||
26 | /** |
||
27 | * Tests the constructor with an invalid PDF engine object |
||
28 | * |
||
29 | * @return void |
||
30 | * @expectedException \InvalidArgumentException |
||
31 | * @expectedExceptionMessage $pdfEngine is not an object! |
||
32 | * @covers ::__construct |
||
33 | */ |
||
34 | public function testConstructorInvalidPdfEngine() |
||
38 | |||
39 | /** |
||
40 | * Tests the constructor with valid parameters |
||
41 | * |
||
42 | * @return void |
||
43 | * @covers ::__construct |
||
44 | */ |
||
45 | public function testConstructor() |
||
49 | |||
50 | /** |
||
51 | * Tests the createPaymentSlip method with a valid payment slip |
||
52 | * |
||
53 | * @return void |
||
54 | * @expectedException \PHPUnit_Framework_Error |
||
55 | * @expectedExceptionMessage Argument 1 passed to SwissPaymentSlip\SwissPaymentSlipPdf\PaymentSlipPdf::createPaymentSlip() must be an instance of SwissPaymentSlip\SwissPaymentSlip\PaymentSlip, instance of stdClass given |
||
56 | * @covers ::createPaymentSlip |
||
57 | */ |
||
58 | public function testConstructorInvalidPaymentSlip() |
||
66 | |||
67 | /** |
||
68 | * Tests the createPaymentSlip method |
||
69 | * |
||
70 | * @return void |
||
71 | * @covers ::createPaymentSlip |
||
72 | */ |
||
73 | public function testCreatePaymentSlip() |
||
121 | |||
122 | /** |
||
123 | * Tests the createPaymentSlip method when no background image is displayed |
||
124 | * |
||
125 | * @return void |
||
126 | * @covers ::createPaymentSlip |
||
127 | */ |
||
128 | public function testCreatePaymentSlipNoBackground() |
||
150 | |||
151 | /** |
||
152 | * Tests the writePaymentSlipLines method |
||
153 | * |
||
154 | * @return void |
||
155 | * @covers ::writePaymentSlipLines |
||
156 | */ |
||
157 | public function testWritePaymentSlipLines() |
||
180 | |||
181 | /** |
||
182 | * Tests the writePaymentSlipLines method with an element that has a background set |
||
183 | * |
||
184 | * @return void |
||
185 | * @covers ::writePaymentSlipLines |
||
186 | */ |
||
187 | public function testWritePaymentSlipLinesElementWithBackground() |
||
211 | |||
212 | /** |
||
213 | * Tests the writePaymentSlipLines method with an invalid first parameter |
||
214 | * |
||
215 | * @return void |
||
216 | * @expectedException \InvalidArgumentException |
||
217 | * @expectedExceptionMessage $elementName is not a string! |
||
218 | * @covers ::writePaymentSlipLines |
||
219 | */ |
||
220 | public function testWritePaymentSlipLinesInvalidFirstParameter() |
||
232 | |||
233 | /** |
||
234 | * Tests the writePaymentSlipLines method with an invalid second parameter |
||
235 | * |
||
236 | * @return void |
||
237 | * @expectedException \PHPUnit_Framework_Error |
||
238 | * @expectedExceptionMessage Argument 2 passed to SwissPaymentSlip\SwissPaymentSlipPdf\PaymentSlipPdf::writePaymentSlipLines() must be of the type array, string given |
||
239 | * @covers ::writePaymentSlipLines |
||
240 | */ |
||
241 | public function testWritePaymentSlipLinesInvalidSecondParameter() |
||
259 | |||
260 | /** |
||
261 | * Tests the writePaymentSlipLines method with no 'lines' key |
||
262 | * |
||
263 | * @return void |
||
264 | * @expectedException \InvalidArgumentException |
||
265 | * @expectedExceptionMessage $element contains not "lines" key! |
||
266 | * @covers ::writePaymentSlipLines |
||
267 | */ |
||
268 | View Code Duplication | public function testWritePaymentSlipLinesNoLinesKey() |
|
280 | |||
281 | /** |
||
282 | * Tests the writePaymentSlipLines method with no 'attributes' key |
||
283 | * |
||
284 | * @return void |
||
285 | * @expectedException \InvalidArgumentException |
||
286 | * @expectedExceptionMessage $element contains not "attributes" key! |
||
287 | * @covers ::writePaymentSlipLines |
||
288 | */ |
||
289 | View Code Duplication | public function testWritePaymentSlipLinesNoAttributesKey() |
|
300 | |||
301 | /** |
||
302 | * Tests the writePaymentSlipLines method with 'lines' key being no array |
||
303 | * |
||
304 | * @return void |
||
305 | * @expectedException \InvalidArgumentException |
||
306 | * @expectedExceptionMessage $lines is not an array! |
||
307 | * @covers ::writePaymentSlipLines |
||
308 | */ |
||
309 | View Code Duplication | public function testWritePaymentSlipLinesLinesKeyNoArray() |
|
320 | |||
321 | /** |
||
322 | * Tests the writePaymentSlipLines method with 'attributes' key being no array |
||
323 | * |
||
324 | * @return void |
||
325 | * @expectedException \InvalidArgumentException |
||
326 | * @expectedExceptionMessage $attributes is not an array! |
||
327 | * @covers ::writePaymentSlipLines |
||
328 | */ |
||
329 | View Code Duplication | public function testWritePaymentSlipLinesAttributesKeyNoArray() |
|
341 | |||
342 | /** |
||
343 | * Make a protected method public using the Reflection API |
||
344 | * |
||
345 | * @param string $className The full name of the class incl. namespace |
||
346 | * @param string $methodName The name of the method to make accessible. |
||
347 | * @return \ReflectionMethod The now public method |
||
348 | */ |
||
349 | protected function makeMethodAccessible($className, $methodName) { |
||
354 | } |
||
355 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: