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 |
||
18 | class JsonTest extends PHPUnit_Framework_TestCase |
||
19 | { |
||
20 | /** @var Json */ |
||
21 | protected $client; |
||
22 | |||
23 | /** @var HttpClient | \Prophecy\Prophecy\ObjectProphecy */ |
||
24 | protected $mockClient; |
||
25 | |||
26 | /** |
||
27 | * @before |
||
28 | */ |
||
29 | public function initialize() |
||
33 | |||
34 | /** |
||
35 | * @test |
||
36 | * @expectedException \InvalidArgumentException |
||
37 | * @expectedExceptionMessage Endpoint not set |
||
38 | */ |
||
39 | public function throwsExceptionWhenNoEndpointIsGiven() |
||
43 | |||
44 | /** |
||
45 | * @test |
||
46 | */ |
||
47 | public function implementsTheCorrectInterface() |
||
51 | |||
52 | /** |
||
53 | * @test |
||
54 | */ |
||
55 | View Code Duplication | public function canResolvePathWithNonLeadingSlash() |
|
71 | |||
72 | |||
73 | /** |
||
74 | * @test |
||
75 | */ |
||
76 | public function canMutateEndpoint() |
||
81 | |||
82 | /** |
||
83 | * @test |
||
84 | */ |
||
85 | View Code Duplication | public function canMakeGetRequests() |
|
101 | |||
102 | /** |
||
103 | * @test |
||
104 | */ |
||
105 | View Code Duplication | public function canMakeGetRequestsWithAuthentication() |
|
124 | |||
125 | /** |
||
126 | * @test |
||
127 | */ |
||
128 | View Code Duplication | public function canMakeGetRequestsWithBasicAuthentication() |
|
147 | |||
148 | /** |
||
149 | * @test |
||
150 | */ |
||
151 | View Code Duplication | public function canMakePostRequests() |
|
167 | |||
168 | /** |
||
169 | * @test |
||
170 | */ |
||
171 | View Code Duplication | public function canMakePutRequest() |
|
187 | |||
188 | /** |
||
189 | * @test |
||
190 | * @expectedException \BadMethodCallException |
||
191 | */ |
||
192 | public function deleteIsNotImplemented() |
||
196 | |||
197 | /** |
||
198 | * @test |
||
199 | * @expectedException \BadMethodCallException |
||
200 | */ |
||
201 | public function patchIsNotImplemented() |
||
205 | |||
206 | /** |
||
207 | * @test |
||
208 | */ |
||
209 | public function convertsGuzzleResponseExceptionsIntoFriendlyClientExceptions() |
||
234 | |||
235 | /** |
||
236 | * @test |
||
237 | * @expectedException \Brofist\ApiClient\InvalidJsonResponseBodyException |
||
238 | */ |
||
239 | public function throwsInvalidJsonResponse() |
||
246 | |||
247 | /** |
||
248 | * @test |
||
249 | */ |
||
250 | public function passesOnHeaders() |
||
259 | |||
260 | /** |
||
261 | * @return \Prophecy\Prophecy\ObjectProphecy |
||
262 | */ |
||
263 | private function mockClient() |
||
271 | |||
272 | /** |
||
273 | * @param string $path |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | private function url($path = '/') |
||
281 | |||
282 | private function fooBarResponse() |
||
286 | |||
287 | private function mockResponseBody($responseBody) |
||
294 | |||
295 | private function getMockResponse(array $data = []) |
||
299 | |||
300 | /** |
||
301 | * @param array $data |
||
302 | */ |
||
303 | private function assertFooBarResponse($data) |
||
307 | |||
308 | private function setClient(array $params = []) |
||
316 | } |
||
317 |
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.