Test Failed
Branch v0.2 (3f77aa)
by Freddie
07:42 queued 01:11
created

HttpRequestCapabilities   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 40.63 %

Importance

Changes 0
Metric Value
dl 39
loc 96
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A sendPost() 10 10 1
A createRequest() 0 5 1
A streamFor() 0 11 2
A sendGet() 8 8 1
A getRawBody() 0 3 1
A sendDelete() 7 7 1
A send() 0 5 1
A getBody() 0 3 1
A sendPut() 10 10 1
A addHeaders() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

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);
2
3
namespace Simplex\Quickstart\Shared\Testing;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Simplex\HttpApplication;
8
use Zend\Diactoros\Response;
9
use Zend\Diactoros\ServerRequestFactory;
10
use Zend\Diactoros\Stream;
11
use Zend\Diactoros\Uri;
12
use Psr\Http;
13
14
trait HttpRequestCapabilities
15
{
16 View Code Duplication
    public function sendGet(string $uri, array $queryParams = [], array $headers = []): ResponseInterface
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $application = new HttpApplication($this->/** @scrutinizer ignore-call */ getContainer());
Loading history...
49
50
        return $application->handleRequest($request, new Response());
51
    }
52
53 View Code Duplication
    public function sendPost(string $uri,  array $body, array $headers = []): ResponseInterface
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
54
    {
55
        $stream = $this->streamFor($body);
56
57
        $request = $this->createRequest('POST', $uri)
58
            ->withBody($stream);
59
60
        $request = $this->addHeaders($headers, $request);
61
62
        return $this->send($request);
63
    }
64
65
    /**
66
     * @see https://github.com/guzzle/psr7/blob/master/src/functions.php
67
     */
68
    private function streamFor(array $body): Stream
69
    {
70
        $resource = json_encode($body);
71
72
        $stream = fopen('php://temp', 'r+');
73
        if ($resource !== '') {
74
            fwrite($stream, $resource);
75
            fseek($stream, 0);
76
        }
77
78
        return new Stream($stream);
79
    }
80
81 View Code Duplication
    public function sendPut(string $uri,  array $body, array $headers = []): ResponseInterface
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
108
    {
109
        return (string) $response->getBody();
110
    }
111
}
112