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 |
||
30 | class HttpClientPoolItem implements HttpClient, HttpAsyncClient |
||
31 | { |
||
32 | /** |
||
33 | * @var int Number of request this client is currently sending |
||
34 | */ |
||
35 | private $sendingRequestCount = 0; |
||
36 | |||
37 | /** |
||
38 | * @var \DateTime|null Time when this client has been disabled or null if enable |
||
39 | */ |
||
40 | private $disabledAt; |
||
41 | |||
42 | /** |
||
43 | * Number of seconds until this client is enabled again after an error. |
||
44 | * |
||
45 | * null: never reenable this client. |
||
46 | * |
||
47 | * @var int|null |
||
48 | */ |
||
49 | private $reenableAfter; |
||
50 | |||
51 | /** |
||
52 | * @var FlexibleHttpClient A http client responding to async and sync request |
||
53 | */ |
||
54 | private $client; |
||
55 | |||
56 | /** |
||
57 | * @param ClientInterface|HttpAsyncClient $client |
||
58 | * @param int|null $reenableAfter Number of seconds until this client is enabled again after an error |
||
59 | */ |
||
60 | 23 | View Code Duplication | public function __construct($client, int $reenableAfter = null) |
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 13 | public function sendRequest(RequestInterface $request): ResponseInterface |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 8 | public function sendAsyncRequest(RequestInterface $request) |
|
117 | |||
118 | /** |
||
119 | * Whether this client is disabled or not. |
||
120 | * |
||
121 | * If the client was disabled, calling this method checks if the client can |
||
122 | * be reenabled and if so enables it. |
||
123 | */ |
||
124 | 21 | public function isDisabled(): bool |
|
141 | |||
142 | /** |
||
143 | * Get current number of request that are currently being sent by the underlying HTTP client. |
||
144 | */ |
||
145 | 2 | public function getSendingRequestCount(): int |
|
149 | |||
150 | /** |
||
151 | * Increment the request count. |
||
152 | */ |
||
153 | 21 | private function incrementRequestCount(): void |
|
157 | |||
158 | /** |
||
159 | * Decrement the request count. |
||
160 | */ |
||
161 | 16 | private function decrementRequestCount(): void |
|
165 | |||
166 | /** |
||
167 | * Enable the current client. |
||
168 | */ |
||
169 | 5 | private function enable(): void |
|
173 | |||
174 | /** |
||
175 | * Disable the current client. |
||
176 | */ |
||
177 | 10 | private function disable(): void |
|
181 | } |
||
182 |
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.