Issues (111)

src/Botan.php (2 issues)

1
<?php
2
3
namespace TelegramBot\Api;
4
5
use TelegramBot\Api\Types\Message;
6
7
/**
8
 * @deprecated
9
 * @psalm-suppress all
10
 */
11
class Botan
12
{
13
    /**
14
     * @var string Tracker url
15
     */
16
    const BASE_URL = 'https://api.botan.io/track';
17
18
    /**
19
     * CURL object
20
     *
21
     * @var resource
22
     */
23
    protected $curl;
24
25
    /**
26
     * Yandex AppMetrica application api_key
27
     *
28
     * @var string
29
     */
30
    protected $token;
31
32
    /**
33
     * Botan constructor
34
     *
35
     * @param string $token
36
     *
37 2
     * @throws InvalidArgumentException
38
     */
39 2
    public function __construct($token)
40
    {
41
        if (empty($token)) {
42
            throw new InvalidArgumentException('Token should not be empty');
43 2
        }
44
45
        $this->token = $token;
46
        $this->curl = curl_init();
0 ignored issues
show
Documentation Bug introduced by
It seems like curl_init() can also be of type CurlHandle. However, the property $curl 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...
47 2
    }
48 2
49 2
    /**
50
     * Event tracking
51
     *
52
     * @param \TelegramBot\Api\Types\Message $message
53
     * @param string $eventName
54
     *
55
     * @throws \TelegramBot\Api\Exception
56
     * @throws \TelegramBot\Api\HttpException
57
     *
58
     * @return void
59
     */
60
    public function track(Message $message, $eventName = 'Message')
61
    {
62
        $uid = $message->getFrom()->getId();
63
64
        $options = [
65
            CURLOPT_URL => self::BASE_URL . "?token={$this->token}&uid={$uid}&name={$eventName}",
66
            CURLOPT_RETURNTRANSFER => true,
67
            CURLOPT_POST => true,
68
            CURLOPT_HTTPHEADER => [
69
                'Content-Type: application/json'
70
            ],
71
            CURLOPT_POSTFIELDS => $message->toJson(),
72
            CURLOPT_TIMEOUT => 5,
73
        ];
74
75
        curl_setopt_array($this->curl, $options);
76
        /** @var string $response */
77
        $response = curl_exec($this->curl);
78
        /** @var array $result */
79
        $result = BotApi::jsonValidate($response, true);
80
81
        BotApi::curlValidate($this->curl);
0 ignored issues
show
Deprecated Code introduced by
The function TelegramBot\Api\BotApi::curlValidate() has been deprecated. ( Ignorable by Annotation )

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

81
        /** @scrutinizer ignore-deprecated */ BotApi::curlValidate($this->curl);
Loading history...
82
83
        if ($result['status'] !== 'accepted') {
84
            throw new Exception('Error Processing Request');
85
        }
86
    }
87
88 2
    /**
89
     * Destructor. Close curl
90 2
     */
91 2
    public function __destruct()
92
    {
93
        $this->curl && curl_close($this->curl);
94
    }
95
}
96