Issues (5)

src/CafeApi.php (3 issues)

1
<?php
2
3
/** Namespace */
4
namespace MatheusBastos\CafeApi;
5
6
/**
7
 * Api abstract class
8
 * @package MatheusBastos\CafeApi
9
 */
10
abstract class CafeApi
11
{
12
    /** @var string */
13
    private $api_url;
14
15
    /** @var array */
16
    private $headers;
17
18
    /** @var array */
19
    private $fields;
20
21
    /** @var string */
22
    private $endpoint;
23
24
    /** @var string */
25
    private $method;
26
27
    /** @var object */
28
    protected $response;
29
30
    /**
31
     * Api constructor
32
     * @param string $apiUrl
33
     * @param string $email
34
     * @param string $password
35
     */
36
    public function __construct(string $api_url, string $email, string $password)
37
    {
38
        $this->api_url = $api_url;
39
        $this->headers([
40
            'email' => $email,
41
            'password' => $password,
42
        ]);
43
    }
44
45
    /**
46
     * Api request
47
     * @param string $method
48
     * @param string $endpoint
49
     * @param array|null $fields
50
     * @param array|null $headers
51
     * @return void
52
     */
53
    protected function request(string $method, string $endpoint, array $fields = null, array $headers = null): void
54
    {
55
        $this->method = $method;
56
        $this->endpoint = $endpoint;
57
        $this->fields = $fields;
58
        $this->headers($headers);
59
60
        $this->dispatch();
61
    }
62
63
    /**
64
     * Api response
65
     * @return object|null
66
     */
67
    public function response()
68
    {
69
        return $this->response;
70
    }
71
72
    /**
73
     * Api error
74
     * @return object|null
75
     */
76
    public function error()
77
    {
78
        if (!empty($this->response->errors)) {
79
            return $this->response->errors;
80
        }
81
82
        return null;
83
    }
84
85
    /**
86
     * Api headers
87
     * @param array|null $headers
88
     * @return void
89
     */
90
    private function headers(?array $headers): void
91
    {
92
        if (!$headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
            return;
94
        }
95
96
        foreach ($headers as $key => $header) {
97
            $this->headers[] = "{$key}: {$header}";
98
        }
99
    }
100
101
    /**
102
     * Api dispatch
103
     * @return void
104
     */
105
    private function dispatch(): void
106
    {
107
        $curl = curl_init();
108
109
        if (empty($this->fields['files'])) {
110
            $this->fields = !empty($this->fields) ? http_build_query($this->fields) : null;
0 ignored issues
show
Documentation Bug introduced by
It seems like ! empty($this->fields) ?...y($this->fields) : null can also be of type string. However, the property $fields is declared as type array. 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...
111
        }
112
113
        curl_setopt_array($curl, [
114
            CURLOPT_URL => "{$this->api_url}/{$this->endpoint}",
115
            CURLOPT_RETURNTRANSFER => true,
116
            CURLOPT_MAXREDIRS => 10,
117
            CURLOPT_TIMEOUT => 30,
118
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
119
            CURLOPT_CUSTOMREQUEST => $this->method,
120
            CURLOPT_POSTFIELDS => $this->fields,
121
            CURLOPT_HTTPHEADER => $this->headers,
122
        ]);
123
124
        $this->response = json_decode(curl_exec($curl));
0 ignored issues
show
It seems like curl_exec($curl) 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

124
        $this->response = json_decode(/** @scrutinizer ignore-type */ curl_exec($curl));
Loading history...
125
        curl_close($curl);
126
    }
127
}
128