Issues (9)

src/ApiNfeFasa.php (3 issues)

1
<?php
2
3
namespace WagnerMontanini\ApiNfeFasa;
4
5
/**
6
 * Class ApiNfeFasa
7
 * @package WagnerMontanini\ApiNfeFasa
8
 */
9
abstract class ApiNfeFasa
10
{
11
    /** @var string */
12
    private $apiUrl;
13
14
    /** @var array */
15
    private $headers;
16
17
    /** @var array */
18
    private $fields;
19
20
    /** @var string */
21
    private $endpoint;
22
23
    /** @var string */
24
    private $method;
25
26
    /** @var object */
27
    protected $response;
28
29
    /**
30
     * ApiNfeFasa constructor.
31
     * @param string $apiUrl
32
     */
33
    public function __construct(string $apiUrl,?string $token = null)
34
    {
35
        $this->apiUrl = $apiUrl;
36
        $headers = empty($token)?null:array("Authorization"=>"Bearer {$token}");
37
        $this->headers($headers);
38
39
    }
40
41
    /**
42
     * @param string $method
43
     * @param string $endpoint
44
     * @param array|null $fields
45
     * @param array|null $headers
46
     */
47
    protected function request(string $method, string $endpoint, array $fields = null, array $headers = null): void
48
    {
49
        $this->method = $method;
50
        $this->endpoint = $endpoint;
51
        $this->fields = $fields;
52
        $this->headers($headers);
53
54
        $this->dispatch();
55
    }
56
57
    /**
58
     * @return object|null
59
     */
60
    public function response()
61
    {
62
        return $this->response;
63
    }
64
65
    /**
66
     * @return object|null
67
     */
68
    public function error()
69
    {
70
        if (!empty($this->response->errors)) {
71
            return $this->response->errors;
72
        }
73
74
        return null;
75
    }
76
77
    /**
78
     * @param array|null $headers
79
     */
80
    private function headers(?array $headers): void
81
    {
82
        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...
83
            return;
84
        }
85
86
        foreach ($headers as $key => $header) {
87
            $this->headers[] = "{$key}: {$header}";
88
        }
89
    }
90
91
    /**
92
     *
93
     */
94
    private function dispatch(): void
95
    {
96
        $curl = curl_init();
97
        
98
        if (empty($this->fields["files"])) {
99
            $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...
100
        }
101
102
        curl_setopt_array($curl, array(
103
            CURLOPT_URL => "{$this->apiUrl}{$this->endpoint}",
104
            CURLOPT_RETURNTRANSFER => true,
105
            CURLOPT_ENCODING => '',
106
            CURLOPT_MAXREDIRS => 10,
107
            CURLOPT_TIMEOUT => 30,
108
            CURLOPT_FOLLOWLOCATION => true,
109
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
110
            CURLOPT_CUSTOMREQUEST => $this->method,
111
            CURLOPT_POSTFIELDS => $this->fields,
112
            CURLOPT_HTTPHEADER => $this->headers,
113
        ));
114
115
        $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

115
        $this->response = json_decode(/** @scrutinizer ignore-type */ curl_exec($curl));
Loading history...
116
        curl_close($curl);
117
    }
118
}