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 |
||
9 | final class ResponseTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | /** |
||
12 | * Verify basic functionality of the response object. |
||
13 | * |
||
14 | * @test |
||
15 | * @covers ::__construct |
||
16 | * @covers ::getHttpCode |
||
17 | * @covers ::getHeaders |
||
18 | * @covers ::getBody |
||
19 | * |
||
20 | * @return void |
||
21 | */ |
||
22 | public function construct() |
||
32 | |||
33 | /** |
||
34 | * Verify basic behavior of getDataWrapper() |
||
35 | * |
||
36 | * @test |
||
37 | * @covers ::getDataWrapper |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | public function getDataWrapper() |
||
61 | |||
62 | /** |
||
63 | * Verify invalid constructor parameters cause exceptions. |
||
64 | * |
||
65 | * @param integer $httpCode The http response code. |
||
66 | * @param array $headers The response headers. |
||
67 | * @param array $body The response body. |
||
68 | * |
||
69 | * @test |
||
70 | * @covers ::__construct |
||
71 | * @dataProvider constructorBadData |
||
72 | * @expectedException \InvalidArgumentException |
||
73 | * |
||
74 | * @return void |
||
75 | */ |
||
76 | public function constructWithInvalidParameters($httpCode, array $headers, array $body) |
||
80 | |||
81 | /** |
||
82 | * Data provider for constructWithInvalidParameters. |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | View Code Duplication | public function constructorBadData() |
|
94 | } |
||
95 |
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.