Issues (236)

src/telegram/request/curl.php (3 issues)

1
<?php
2
3
namespace BPT\telegram\request;
4
5
use BPT\constants\loggerTypes;
6
use BPT\logger;
7
use BPT\settings;
8
use BPT\types\types;
0 ignored issues
show
The type BPT\types\types was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use CurlHandle;
10
use JetBrains\PhpStorm\ArrayShape;
11
12
/**
13
 * curl class , part of request class for handling request based on curl
14
 */
15
class curl {
16
    private static CurlHandle $curl_handler;
17
18
    /**
19
     * @internal Only for BPT self usage , Don't use it in your source!
20
     */
21
    public static function init(string $method,array $data) {
22
        $info = self::getInfo($data);
23
        $data = $info['data'];
24
        $handler = $info['handler'];
25
        self::setTimeout($data,$handler,$method);
0 ignored issues
show
It seems like $handler can also be of type resource; however, parameter $curl_handler of BPT\telegram\request\curl::setTimeout() does only seem to accept CurlHandle, 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

25
        self::setTimeout($data,/** @scrutinizer ignore-type */ $handler,$method);
Loading history...
26
        self::setData($data);
27
        $data['method'] = $method;
28
        curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
29
        $result = curl_exec($handler);
30
        if (curl_errno($handler)) {
31
            logger::write(curl_error($handler),loggerTypes::WARNING);
32
        }
33
        if ($info['token'] != settings::$token) {
34
            curl_close($handler);
35
        }
36
        return json_decode($result);
0 ignored issues
show
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

36
        return json_decode(/** @scrutinizer ignore-type */ $result);
Loading history...
37
    }
38
39
    #[ArrayShape(['data' => 'array', 'token' => 'mixed|string', 'handler' => '\CurlHandle|false|CurlHandle'])]
40
    private static function getInfo(array $data): array {
41
        if (isset($data['token']) && $data['token'] !== settings::$token) {
42
            $token = $data['token'];
43
            unset($data['token']);
44
            $curl_handler = curl_init(settings::$base_url."/bot$token/");
45
            curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, true);
46
            curl_setopt($curl_handler, CURLOPT_SSL_VERIFYPEER, false);
47
        }
48
        else{
49
            $token = settings::$token;
50
            if (!isset(self::$curl_handler)){
51
                self::$curl_handler = curl_init(settings::$base_url."/bot$token/");
52
                curl_setopt(self::$curl_handler, CURLOPT_RETURNTRANSFER, true);
53
                curl_setopt(self::$curl_handler, CURLOPT_SSL_VERIFYPEER, false);
54
                curl_setopt(self::$curl_handler, CURLOPT_TCP_KEEPALIVE, 1);
55
            }
56
            $curl_handler = self::$curl_handler;
57
        }
58
59
        return [
60
            'data'    => $data,
61
            'token'   => $token,
62
            'handler' => $curl_handler
63
        ];
64
    }
65
66
    private static function setTimeout(array &$data , CurlHandle $curl_handler,string $method): void {
67
        if (isset($data['forgot'])) {
68
            curl_setopt($curl_handler, CURLOPT_TIMEOUT_MS, settings::$forgot_time);
69
            unset($data['forgot']);
70
        }
71
        elseif ($method === 'getUpdates' || $method === 'setWebhook'){
72
            curl_setopt($curl_handler, CURLOPT_TIMEOUT_MS, 5000);
73
        }
74
        elseif (settings::$base_timeout > 0) {
75
            curl_setopt($curl_handler, CURLOPT_TIMEOUT_MS, settings::$base_timeout);
76
        }
77
    }
78
79
    private static function setData(array &$data): void {
80
        foreach ($data as &$value){
81
            if (is_array($value) || (is_object($value) && !is_a($value,'CURLFile') && !($value instanceof types))){
82
                $value = json_encode($value);
83
            }
84
        }
85
    }
86
}