Total Complexity | 22 |
Total Lines | 189 |
Duplicated Lines | 23.81 % |
Changes | 0 |
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 |
||
16 | class Remote |
||
17 | { |
||
18 | private $config = []; |
||
19 | |||
20 | public function __construct(array $config = []) |
||
21 | { |
||
22 | $this->config = $config + [ |
||
23 | // defaults |
||
24 | ]; |
||
25 | } |
||
26 | |||
27 | View Code Duplication | public function get(string $url, array $headers = []): Message |
|
1 ignored issue
–
show
|
|||
28 | { |
||
29 | $request = (new Message) |
||
30 | ->withType(Message::REQUEST) |
||
31 | ->withUrl($url) |
||
32 | ->withMethod(Request::GET) |
||
33 | ->withHeaders($headers); |
||
34 | return $this->http($request); |
||
35 | } |
||
36 | |||
37 | View Code Duplication | public function postJson(string $url, array $data, array $headers = [], string $method = Request::POST): Message |
|
1 ignored issue
–
show
|
|||
38 | { |
||
39 | $headers['Content-Type'] = 'application/json; charset=utf-8'; |
||
40 | $headers['Accept'] = 'application/json'; |
||
41 | $data_string = json_encode($data); |
||
42 | $headers['Content-Length'] = strlen($data_string); |
||
43 | $request = (new Message) |
||
44 | ->withType(Message::REQUEST) |
||
45 | ->withUrl($url) |
||
46 | ->withMethod($method) |
||
47 | ->withBody($data_string) |
||
48 | ->withHeaders($headers); |
||
49 | return $this->http($request); |
||
50 | } |
||
51 | |||
52 | View Code Duplication | public function postForm(string $url, array $data, array $headers = [], string $method = Request::POST): Message |
|
1 ignored issue
–
show
|
|||
53 | { |
||
54 | $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; |
||
55 | $data_string = http_build_query($data); |
||
56 | $headers['Content-Length'] = strlen($data_string); |
||
57 | $request = (new Message) |
||
58 | ->withType(Message::REQUEST) |
||
59 | ->withUrl($url) |
||
60 | ->withMethod($method) |
||
61 | ->withBody($data_string) |
||
62 | ->withHeaders($headers); |
||
63 | return $this->http($request); |
||
64 | } |
||
65 | |||
66 | View Code Duplication | public function delete(string $url, array $headers = []): Message |
|
1 ignored issue
–
show
|
|||
67 | { |
||
68 | $request = (new Message) |
||
69 | ->withType(Message::REQUEST) |
||
70 | ->withUrl($url) |
||
71 | ->withMethod(Request::DELETE) |
||
72 | ->withHeaders($headers); |
||
73 | return $this->http($request); |
||
74 | } |
||
75 | |||
76 | public function http(Message $request): Message |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * return resource a hurl handler |
||
132 | */ |
||
133 | private function createCurlHandlerFromRequest(Message $request) |
||
134 | { |
||
135 | $curl_handler = curl_init(); |
||
136 | |||
137 | $options = $this->config['curl'] ?? []; |
||
138 | $options += [ |
||
139 | CURLOPT_FOLLOWLOCATION => true, |
||
140 | CURLOPT_MAXREDIRS => 10, |
||
141 | CURLOPT_CONNECTTIMEOUT => 5, |
||
142 | CURLOPT_TIMEOUT => 5, |
||
143 | CURLOPT_URL => $request->url(), |
||
144 | CURLOPT_USERAGENT => 'alkemann\h2l\Remote', |
||
145 | CURLOPT_RETURNTRANSFER => true, |
||
146 | CURLOPT_HEADER => true, |
||
147 | CURLOPT_CUSTOMREQUEST => $request->method(), |
||
148 | ]; |
||
149 | |||
150 | if (!isset($options[CURLOPT_HTTPHEADER])) { |
||
151 | $options[CURLOPT_HTTPHEADER] = []; |
||
152 | } |
||
153 | $body = $request->body(); |
||
154 | if ($body) { |
||
155 | $options[CURLOPT_POSTFIELDS] = $body; |
||
156 | } |
||
157 | |||
158 | foreach ($request->headers() as $header_name => $header_value) { |
||
159 | $options[CURLOPT_HTTPHEADER][] = "{$header_name}: {$header_value}"; |
||
160 | } |
||
161 | |||
162 | $headers_to_set_to_blank_if_not_set = [ |
||
163 | 'Content-Type', |
||
164 | 'Expect', |
||
165 | 'Accept', |
||
166 | 'Accept-Encoding' |
||
167 | ]; |
||
168 | foreach ($headers_to_set_to_blank_if_not_set as $name) { |
||
169 | if ($request->header($name) === null) { |
||
170 | $conf[CURLOPT_HTTPHEADER][] = "{$name}:"; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | curl_setopt_array($curl_handler, $options); |
||
175 | |||
176 | return $curl_handler; |
||
177 | } |
||
178 | |||
179 | private function extractHeaders(string $header): array |
||
205 | } |
||
206 | } |
||
207 |
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.