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 |
||
24 | final class Client |
||
25 | { |
||
26 | /** |
||
27 | * @var GuzzleClient |
||
28 | */ |
||
29 | private $guzzle; |
||
30 | |||
31 | /** |
||
32 | * @var HandlerStack |
||
33 | */ |
||
34 | private $handlerStack; |
||
35 | |||
36 | /** |
||
37 | * @param Environment $environment |
||
38 | */ |
||
39 | public function __construct(Environment $environment, Keypair $keypair, PublicKey $serverPublicKey = null, string $sessionToken = '') |
||
56 | |||
57 | /** |
||
58 | * @param string $endpoint |
||
59 | * @return array |
||
60 | */ |
||
61 | public function get(string $endpoint, array $headers = []): array |
||
69 | |||
70 | /** |
||
71 | * @param string $endpoint |
||
72 | * @param array $body |
||
73 | * @param array $headers |
||
74 | * @return array |
||
75 | */ |
||
76 | View Code Duplication | public function post(string $endpoint, array $body, array $headers = []): array |
|
85 | |||
86 | /** |
||
87 | * @param string $endpoint |
||
88 | * @param array $body |
||
89 | * @param array $headers |
||
90 | * @return array |
||
91 | */ |
||
92 | View Code Duplication | public function put(string $endpoint, array $body, array $headers = []): array |
|
101 | |||
102 | /** |
||
103 | * @param string $endpoint |
||
104 | * @param array $headers |
||
105 | * @return void |
||
106 | */ |
||
107 | public function delete(string $endpoint, array $headers = []) |
||
113 | |||
114 | /** |
||
115 | * @param ResponseInterface $response |
||
116 | * @return array |
||
117 | */ |
||
118 | private function processResponse(ResponseInterface $response): array |
||
138 | |||
139 | private function mapResponse(string $key, array $value) |
||
164 | |||
165 | /** |
||
166 | * @param string $sessionToken |
||
167 | * @return void |
||
168 | */ |
||
169 | private function addRequestIdMiddleware(string $sessionToken) |
||
176 | |||
177 | /** |
||
178 | * @param Keypair $keypair |
||
179 | * @return void |
||
180 | */ |
||
181 | private function addRequestSignatureMiddleware(Keypair $keypair) |
||
189 | |||
190 | /** |
||
191 | * @param PublicKey|null $serverPublicKey |
||
192 | * @return void |
||
193 | */ |
||
194 | private function addServerResponseMiddleware(PublicKey $serverPublicKey = null) |
||
203 | |||
204 | /** |
||
205 | * @param Environment $environment |
||
206 | * @return void |
||
207 | */ |
||
208 | private function addDebugMiddleware(Environment $environment) |
||
214 | } |
||
215 |
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.