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 |
||
23 | class Client implements HttpClient, HttpAsyncClient |
||
24 | { |
||
25 | use HttpAsyncClientEmulator; |
||
26 | use VersionBridgeClient; |
||
27 | |||
28 | /** |
||
29 | * @var ResponseFactory |
||
30 | */ |
||
31 | private $responseFactory; |
||
32 | |||
33 | /** |
||
34 | * @var RequestInterface[] |
||
35 | */ |
||
36 | private $requests = []; |
||
37 | |||
38 | /** |
||
39 | * @var ResponseInterface[] |
||
40 | */ |
||
41 | private $responses = []; |
||
42 | |||
43 | /** |
||
44 | * @var ResponseInterface|null |
||
45 | */ |
||
46 | private $defaultResponse; |
||
47 | |||
48 | /** |
||
49 | * @var Exception[] |
||
50 | */ |
||
51 | private $exceptions = []; |
||
52 | |||
53 | /** |
||
54 | * @var Exception|null |
||
55 | */ |
||
56 | private $defaultException; |
||
57 | |||
58 | 11 | public function __construct(ResponseFactory $responseFactory = null) |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 7 | public function doSendRequest(RequestInterface $request) |
|
89 | |||
90 | /** |
||
91 | * Adds an exception that will be thrown. |
||
92 | */ |
||
93 | 2 | View Code Duplication | public function addException(\Exception $exception) |
100 | |||
101 | /** |
||
102 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
103 | * |
||
104 | * If both a default exception and a default response are set, the exception will be thrown. |
||
105 | */ |
||
106 | View Code Duplication | public function setDefaultException(\Exception $defaultException = null) |
|
113 | |||
114 | /** |
||
115 | * Adds a response that will be returned in first in first out order. |
||
116 | */ |
||
117 | public function addResponse(ResponseInterface $response) |
||
121 | |||
122 | /** |
||
123 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
124 | */ |
||
125 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
||
129 | |||
130 | /** |
||
131 | * Returns requests that were sent. |
||
132 | * |
||
133 | * @return RequestInterface[] |
||
134 | */ |
||
135 | public function getRequests() |
||
139 | |||
140 | public function getLastRequest() |
||
144 | |||
145 | public function reset() |
||
153 | } |
||
154 |
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.