Issues (4)

src/DeOlhoNoImposto.php (4 issues)

1
<?php
2
3
namespace WagnerMontanini\DeOlhoNoImposto;
4
5
/**
6
 * Class DeOlhoNoImposto
7
 * @package WagnerMontanini\DeOlhoNoImposto
8
 */
9
abstract class DeOlhoNoImposto
10
{
11
    /** @var string */
12
    private $apiUrl;
13
14
    /** @var string */
15
    private $token;
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
     * DeOlhoNoImposto constructor.
31
     * @param string $versao
32
     * @param string $token
33
     */
34
    public function __construct(string $versao, string $token)
35
    {
36
        $this->apiUrl = "https://apidoni.ibpt.org.br/api/{$versao}";
37
        $this->token = $token;
38
    }
39
40
    /**
41
     * @param string $method
42
     * @param string $endpoint
43
     * @param array|null $fields
44
     * @param array|null $headers
45
     */
46
    protected function request(string $method, string $endpoint, array $fields): void
47
    {
48
        $this->method = $method;
49
        $this->endpoint = $endpoint;
50
        $this->fields = $fields;
51
52
        $this->dispatch();
53
    }
54
55
    /**
56
     * @return object|null
57
     */
58
    public function response()
59
    {
60
        return $this->response;
61
    }
62
63
    /**
64
     * @return object|null
65
     */
66
    public function error()
67
    {
68
        if (!empty($this->response->errors)) {
69
            return $this->response->errors;
70
        }
71
72
        return null;
73
    }
74
75
    /**
76
     *
77
     */
78
    private function dispatch(): void
79
    {
80
        $curl = curl_init();
81
82
        $this->fields['token'] = $this->token;
83
84
        $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...
85
        
86
        curl_setopt_array($curl, array(
0 ignored issues
show
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt_array() 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

86
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, array(
Loading history...
87
            CURLOPT_URL => "{$this->apiUrl}{$this->endpoint}?{$this->fields}",
88
            CURLOPT_RETURNTRANSFER => true,
89
            CURLOPT_MAXREDIRS => 10,
90
            CURLOPT_TIMEOUT => 30,
91
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
92
            CURLOPT_CUSTOMREQUEST => $this->method,
93
            CURLOPT_HTTPHEADER => [],
94
        ));
95
96
        $this->response = json_decode(curl_exec($curl));
0 ignored issues
show
It seems like $curl 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

96
        $this->response = json_decode(curl_exec(/** @scrutinizer ignore-type */ $curl));
Loading history...
97
        curl_close($curl);
0 ignored issues
show
It seems like $curl 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

97
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
98
    }
99
}