Total Complexity | 13 |
Total Lines | 96 |
Duplicated Lines | 40.63 % |
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 declare(strict_types=1); |
||
14 | trait HttpRequestCapabilities |
||
15 | { |
||
16 | View Code Duplication | public function sendGet(string $uri, array $queryParams = [], array $headers = []): ResponseInterface |
|
1 ignored issue
–
show
|
|||
17 | { |
||
18 | $request = $this->createRequest('GET', $uri) |
||
19 | ->withQueryParams($queryParams); |
||
20 | |||
21 | $request = $this->addHeaders($headers, $request); |
||
22 | |||
23 | return $this->send($request); |
||
24 | } |
||
25 | |||
26 | private function createRequest(string $method, string $uri): ServerRequestInterface |
||
27 | { |
||
28 | return (ServerRequestFactory::fromGlobals([])) |
||
29 | ->withMethod($method) |
||
30 | ->withUri(new Uri($uri)); |
||
31 | } |
||
32 | |||
33 | private function addHeaders(array $headers, ServerRequestInterface $request): ServerRequestInterface |
||
34 | { |
||
35 | if (empty($headers)) { |
||
36 | return $request; |
||
37 | } |
||
38 | |||
39 | foreach ($headers as $headerName => $headerValue) { |
||
40 | $request = $request->withHeader($headerName, $headerValue); |
||
41 | } |
||
42 | |||
43 | return $request; |
||
44 | } |
||
45 | |||
46 | private function send(ServerRequestInterface $request): ResponseInterface |
||
47 | { |
||
48 | $application = new HttpApplication($this->getContainer()); |
||
49 | |||
50 | return $application->handleRequest($request, new Response()); |
||
51 | } |
||
52 | |||
53 | View Code Duplication | public function sendPost(string $uri, array $body, array $headers = []): ResponseInterface |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @see https://github.com/guzzle/psr7/blob/master/src/functions.php |
||
67 | */ |
||
68 | private function streamFor(array $body): Stream |
||
79 | } |
||
80 | |||
81 | View Code Duplication | public function sendPut(string $uri, array $body, array $headers = []): ResponseInterface |
|
1 ignored issue
–
show
|
|||
82 | { |
||
83 | $stream = $this->streamFor($body); |
||
84 | |||
85 | $request = $this->createRequest('PUT', $uri) |
||
86 | ->withBody($stream); |
||
87 | |||
88 | $request = $this->addHeaders($headers, $request); |
||
89 | |||
90 | return $this->send($request); |
||
91 | } |
||
92 | |||
93 | View Code Duplication | public function sendDelete(string $uri, array $headers = []): ResponseInterface |
|
1 ignored issue
–
show
|
|||
94 | { |
||
95 | $request = $this->createRequest('DELETE', $uri); |
||
96 | |||
97 | $request = $this->addHeaders($headers, $request); |
||
98 | |||
99 | return $this->send($request); |
||
100 | } |
||
101 | |||
102 | public function getBody(ResponseInterface $response): array |
||
103 | { |
||
104 | return json_decode((string) $response->getBody(), true); |
||
105 | } |
||
106 | |||
107 | public function getRawBody(ResponseInterface $response): string |
||
110 | } |
||
111 | } |
||
112 |
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.