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 |
||
17 | class RpcMiddlewareMiddlewareTest extends UnitTestCase |
||
18 | { |
||
19 | /** @var mixed */ |
||
20 | private $request; |
||
21 | /** @var mixed */ |
||
22 | private $response; |
||
23 | /** @var RpcErrorMiddleware */ |
||
24 | private $middleware; |
||
25 | |||
26 | public function setUp() |
||
33 | |||
34 | public function testApplyRequest() |
||
38 | |||
39 | /** |
||
40 | * @expectedException \Graze\GuzzleHttp\JsonRpc\Exception\ClientException |
||
41 | */ |
||
42 | View Code Duplication | public function testApplyResponseThrowsClientException() |
|
|
|||
43 | { |
||
44 | $this->response->shouldReceive('getRpcErrorCode')->times(2)->withNoArgs()->andReturn(-32600); |
||
45 | $this->request->shouldReceive('getRequestTarget')->once()->withNoArgs()->andReturn('http://foo'); |
||
46 | $this->request->shouldReceive('getRpcMethod')->once()->withNoArgs()->andReturn('foo'); |
||
47 | $this->response->shouldReceive('getRpcErrorMessage')->once()->withNoArgs()->andReturn('bar'); |
||
48 | $this->response->shouldReceive('getStatusCode')->once()->withNoArgs()->andReturn(200); |
||
49 | |||
50 | $this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @expectedException \Graze\GuzzleHttp\JsonRpc\Exception\ServerException |
||
55 | */ |
||
56 | View Code Duplication | public function testApplyResponseThrowsServerException() |
|
57 | { |
||
58 | $this->response->shouldReceive('getRpcErrorCode')->times(2)->withNoArgs()->andReturn(-32000); |
||
59 | $this->request->shouldReceive('getRequestTarget')->once()->withNoArgs()->andReturn('http://foo'); |
||
60 | $this->request->shouldReceive('getRpcMethod')->once()->withNoArgs()->andReturn('foo'); |
||
61 | $this->response->shouldReceive('getRpcErrorMessage')->once()->withNoArgs()->andReturn('bar'); |
||
62 | $this->response->shouldReceive('getStatusCode')->once()->withNoArgs()->andReturn(200); |
||
63 | |||
64 | $this->middleware->applyResponse($this->request, $this->response, ['rpc_error' => true]); |
||
65 | } |
||
66 | |||
67 | public function testApplyResponseNoError() |
||
73 | |||
74 | public function testApplyResponseNoOption() |
||
78 | } |
||
79 |
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.