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 |
||
25 | final class Client |
||
26 | { |
||
27 | /** |
||
28 | * @var GuzzleClient |
||
29 | */ |
||
30 | private $guzzle; |
||
31 | |||
32 | /** |
||
33 | * @var HandlerStack |
||
34 | */ |
||
35 | private $handlerStack; |
||
36 | |||
37 | public function __construct(Environment $environment, Keypair $keypair, PublicKey $serverPublicKey = null, string $sessionToken = '', $proxy = null) |
||
38 | { |
||
39 | Assertion::true(is_null($proxy) || is_string($proxy) || is_array($proxy), 'In case a proxy parameter is provided, it should be either a string or an array.'); |
||
40 | |||
41 | $this->handlerStack = HandlerStack::create(); |
||
42 | |||
43 | $this->addRequestIdMiddleware($sessionToken); |
||
44 | $this->addRequestSignatureMiddleware($keypair); |
||
45 | $this->addServerResponseMiddleware($serverPublicKey); |
||
46 | $this->addDebugMiddleware($environment); |
||
47 | |||
48 | $configuration = [ |
||
49 | 'base_uri' => $environment->endpoint(), |
||
50 | 'handler' => $this->handlerStack, |
||
51 | 'headers' => [ |
||
52 | 'User-Agent' => 'Link0 Bunq API Client' |
||
53 | ] |
||
54 | ]; |
||
55 | |||
56 | if(is_string($proxy) || is_array($proxy)){ |
||
57 | $configuration['proxy'] = $proxy; |
||
58 | } |
||
59 | |||
60 | $this->guzzle = new GuzzleClient($configuration); |
||
61 | } |
||
62 | |||
63 | public function get(string $endpoint, array $headers = []): array |
||
64 | { |
||
65 | return $this->processResponse( |
||
66 | $this->guzzle->request('GET', $endpoint, [ |
||
67 | 'headers' => $headers, |
||
68 | ]) |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | View Code Duplication | public function post(string $endpoint, array $body, array $headers = []): array |
|
|
|||
73 | { |
||
74 | return $this->processResponse( |
||
75 | $this->guzzle->request('POST', $endpoint, [ |
||
76 | 'json' => $body, |
||
77 | 'headers' => $headers, |
||
78 | ]) |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | View Code Duplication | public function put(string $endpoint, array $body, array $headers = []): array |
|
83 | { |
||
84 | return $this->processResponse( |
||
85 | $this->guzzle->request('PUT', $endpoint, [ |
||
86 | 'json' => $body, |
||
87 | 'headers' => $headers, |
||
88 | ]) |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $endpoint |
||
94 | * @param array $headers |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | public function delete(string $endpoint, array $headers = []) |
||
99 | { |
||
100 | $this->guzzle->request('DELETE', $endpoint, [ |
||
101 | 'headers' => $headers, |
||
102 | ]); |
||
103 | } |
||
104 | |||
105 | private function processResponse(ResponseInterface $response): array |
||
106 | { |
||
107 | $contents = (string)$response->getBody(); |
||
108 | $json = json_decode($contents, true)['Response']; |
||
109 | |||
110 | // Return empty responses |
||
111 | if (count($json) === 0) { |
||
112 | return []; |
||
113 | } |
||
114 | |||
115 | foreach ($json as $key => $value) { |
||
116 | if (is_numeric($key)) { |
||
117 | // Often only a single associative entry here |
||
118 | foreach ($value as $type => $struct) { |
||
119 | $json[$key] = $this->mapResponse($type, $struct); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | return $json; |
||
124 | } |
||
125 | |||
126 | private function mapResponse(string $key, array $value) |
||
151 | |||
152 | /** |
||
153 | * @param string $sessionToken |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | private function addRequestIdMiddleware(string $sessionToken) |
||
164 | |||
165 | /** |
||
166 | * @param Keypair $keypair |
||
167 | * |
||
168 | * @return void |
||
169 | */ |
||
170 | private function addRequestSignatureMiddleware(Keypair $keypair) |
||
178 | |||
179 | /** |
||
180 | * @param PublicKey|null $serverPublicKey |
||
181 | * |
||
182 | * @return void |
||
183 | */ |
||
184 | private function addServerResponseMiddleware(PublicKey $serverPublicKey = null) |
||
193 | |||
194 | /** |
||
195 | * @param Environment $environment |
||
196 | * |
||
197 | * @return void |
||
198 | */ |
||
199 | private function addDebugMiddleware(Environment $environment) |
||
205 | } |
||
206 |
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.