Curl   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 42
c 7
b 0
f 0
dl 0
loc 121
rs 10
ccs 46
cts 46
cp 1
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurlOpts() 0 3 1
A execute() 0 26 5
A getRequest() 0 3 1
A getResponse() 0 7 2
A getUrl() 0 3 1
A getContents() 0 5 1
A __construct() 0 7 1
A post() 0 5 1
A __toString() 0 6 2
A postJson() 0 10 2
1
<?php
2
3
namespace Palmtree\Curl;
4
5
use Palmtree\Curl\Exception\BadMethodCallException;
6
use Palmtree\Curl\Exception\CurlErrorException;
7
8
class Curl
9
{
10
    /** @var string */
11
    private $url;
12
    /** @var resource */
13
    private $handle;
14
    /** @var Request */
15
    private $request;
16
    /** @var Response */
17
    private $response;
18
    /** @var CurlOpts */
19
    private $curlOpts = [];
20
21 14
    public function __construct(string $url, array $curlOpts = [])
22
    {
23 14
        $this->url     = $url;
24 14
        $this->handle  = \curl_init($url);
0 ignored issues
show
Documentation Bug introduced by
It seems like curl_init($url) can also be of type false. However, the property $handle is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
25 14
        $this->request = new Request();
26
27 14
        $this->curlOpts = new CurlOpts($curlOpts);
28 14
    }
29
30
    /** @throws CurlErrorException */
31 1
    public static function getContents(string $url, array $curlOpts = []): string
32
    {
33 1
        $curl = new self($url, $curlOpts);
34
35 1
        return $curl->getResponse()->getBody();
36
    }
37
38
    /**
39
     * @param string|array $data
40
     *
41
     * @throws CurlErrorException
42
     */
43 3
    public function post($data): Response
44
    {
45 3
        $this->getRequest()->setBody($data);
46
47 3
        return $this->execute();
48
    }
49
50
    /**
51
     * @param string|mixed $json
52
     *
53
     * @throws CurlErrorException
54
     */
55 2
    public function postJson($json): Response
56
    {
57 2
        if (!\is_string($json)) {
58 1
            $json = \json_encode($json);
59
        }
60
61 2
        $this->getRequest()->addHeader('Content-Type', 'application/json');
62 2
        $this->getRequest()->addHeader('Content-Length', \strlen($json));
63
64 2
        return $this->post($json);
65
    }
66
67
    /** @throws CurlErrorException */
68 9
    public function execute(): Response
69
    {
70 9
        if ($this->response !== null) {
71 1
            throw new BadMethodCallException('Request already executed');
72
        }
73
74 9
        \curl_setopt_array($this->handle, $this->curlOpts->toArray());
75
76 9
        if ($headers = $this->getRequest()->getHeaderStrings()) {
77 2
            \curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
78
        }
79
80 9
        if ($body = $this->getRequest()->getBody()) {
81 3
            \curl_setopt($this->handle, CURLOPT_POST, true);
82 3
            \curl_setopt($this->handle, CURLOPT_POSTFIELDS, $body);
83
        }
84
85 9
        $response = \curl_exec($this->handle);
86
87 9
        if ($errorNumber = \curl_errno($this->handle)) {
88 2
            throw new CurlErrorException(\curl_error($this->handle), $errorNumber);
89
        }
90
91 7
        $this->response = new Response($response, \curl_getinfo($this->handle, CURLINFO_HTTP_CODE));
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type boolean; however, parameter $response of Palmtree\Curl\Response::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

91
        $this->response = new Response(/** @scrutinizer ignore-type */ $response, \curl_getinfo($this->handle, CURLINFO_HTTP_CODE));
Loading history...
92
93 7
        return $this->response;
94
    }
95
96 9
    public function getRequest(): Request
97
    {
98 9
        return $this->request;
99
    }
100
101
    /**
102
     * @throws CurlErrorException
103
     */
104 4
    public function getResponse(): Response
105
    {
106 4
        if ($this->response === null) {
107 4
            $this->execute();
108
        }
109
110 3
        return $this->response;
111
    }
112
113 1
    public function getUrl(): string
114
    {
115 1
        return $this->url;
116
    }
117
118 4
    public function getCurlOpts(): CurlOpts
119
    {
120 4
        return $this->curlOpts;
121
    }
122
123 2
    public function __toString(): string
124
    {
125
        try {
126 2
            return $this->getResponse()->getBody();
127 1
        } catch (CurlErrorException $e) {
128 1
            return '';
129
        }
130
    }
131
}
132