GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Httpie::query()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
nc 1
nop 1
dl 0
loc 5
c 1
b 0
f 0
cc 1
rs 10
ccs 0
cts 5
cp 0
crap 2
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Utility;
9
10
use Deployer\Exception\HttpieException;
11
12
class Httpie
13
{
14
    private $method = 'GET';
15
    private $url = '';
16
    private $headers = [];
17
    private $body = '';
18
    private $curlopts = [];
19
20
    public function __construct()
21
    {
22
        if (!extension_loaded('curl')) {
23
            throw new \Exception(
24
                "Please, install curl extension.\n" .
25
                "https://goo.gl/yTAeZh"
26
            );
27
        }
28
    }
29
30
    public static function get(string $url): Httpie
31
    {
32
        $http = new self;
33
        $http->method = 'GET';
34
        $http->url = $url;
35
        return $http;
36
    }
37
38
    public static function post(string $url): Httpie
39
    {
40
        $http = new self;
41
        $http->method = 'POST';
42
        $http->url = $url;
43
        return $http;
44
    }
45
46
    public function query(array $params): Httpie
47
    {
48
        $http = clone $this;
49
        $http->url .= '?' . http_build_query($params);
50
        return $http;
51
    }
52
53
    public function header(string $header): Httpie
54
    {
55
        $http = clone $this;
56
        $http->headers[] = $header;
57
        return $http;
58
    }
59
60
    public function body(array $data): Httpie
61
    {
62
        $http = clone $this;
63
        $http->body = json_encode($data, JSON_PRETTY_PRINT);
64
        $http->headers = array_merge($http->headers, [
65
            'Content-Type: application/json',
66
            'Content-Length: ' . strlen($http->body)
67
        ]);
68
        return $http;
69
    }
70
71
    public function form(array $data): Httpie
72
    {
73
        $http = clone $this;
74
        $http->body = http_build_query($data);
75
        $http->headers = array_merge($this->headers, [
76
            'Content-type: application/x-www-form-urlencoded',
77
            'Content-Length: ' . strlen($http->body)
78
        ]);
79
        return $http;
80
    }
81
82
    public function setopt($key, $value)
83
    {
84
        $http = clone $this;
85
        $http->curlopts[$key] = $value;
86
        return $http;
87
    }
88
89
    public function send()
90
    {
91
        $ch = curl_init($this->url);
92
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, 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

92
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CUSTOMREQUEST, $this->method);
Loading history...
93
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
94
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
95
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
96
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
97
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
98
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
99
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
100
        foreach ($this->curlopts as $key => $value) {
101
            curl_setopt($ch, $key, $value);
102
        }
103
        $result = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, 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

103
        $result = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
104
        if ($result === false) {
105
            $error = curl_error($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, 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

105
            $error = curl_error(/** @scrutinizer ignore-type */ $ch);
Loading history...
106
            $errno = curl_errno($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_errno() does only seem to accept resource, 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

106
            $errno = curl_errno(/** @scrutinizer ignore-type */ $ch);
Loading history...
107
            curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, 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

107
            curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
108
            throw new HttpieException($error, $errno);
109
        }
110
        curl_close($ch);
111
        return $result;
112
    }
113
114
    public function getJson()
115
    {
116
        $result = $this->send();
117
        $response = json_decode($result, true);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $json of json_decode() 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

117
        $response = json_decode(/** @scrutinizer ignore-type */ $result, true);
Loading history...
118
        if (json_last_error() !== JSON_ERROR_NONE) {
119
            throw new \Exception('JSON Error: ' . json_last_error_msg());
120
        }
121
        return $response;
122
    }
123
}
124