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 |
||
13 | class ClientTest extends PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | /** @var Client */ |
||
16 | protected $client; |
||
17 | |||
18 | /** @var HttpClient | \Prophecy\Prophecy\ObjectProphecy */ |
||
19 | protected $mockClient; |
||
20 | |||
21 | public function setUp() |
||
30 | |||
31 | /** |
||
32 | * @test |
||
33 | */ |
||
34 | public function hasDefaultEndpoint() |
||
38 | |||
39 | /** |
||
40 | * @test |
||
41 | */ |
||
42 | public function canMutateEndpoint() |
||
47 | |||
48 | /** |
||
49 | * @test |
||
50 | */ |
||
51 | View Code Duplication | public function canMakeGetRequests() |
|
68 | |||
69 | /** |
||
70 | * @test |
||
71 | */ |
||
72 | View Code Duplication | public function canMakePostRequests() |
|
89 | |||
90 | /** |
||
91 | * @test |
||
92 | */ |
||
93 | View Code Duplication | public function canMakePutRequest() |
|
110 | |||
111 | /** |
||
112 | * @return \Prophecy\Prophecy\ObjectProphecy |
||
113 | */ |
||
114 | private function mockClient() |
||
122 | |||
123 | /** |
||
124 | * @param string $path |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | private function url($path = '/') |
||
132 | |||
133 | private function fooBarResponse() |
||
140 | |||
141 | /** |
||
142 | * @param array $data |
||
143 | */ |
||
144 | private function assertFooBarResponse($data) |
||
148 | } |
||
149 |
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.