Issues (36)

src/Commands/AdminCommands/DebugCommand.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of the TelegramBot package.
5
 *
6
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Longman\TelegramBot\Commands\AdminCommands;
13
14
use Exception;
15
use Longman\TelegramBot\Commands\AdminCommand;
16
use Longman\TelegramBot\DB;
17
use Longman\TelegramBot\Entities\ServerResponse;
18
use Longman\TelegramBot\Exception\TelegramException;
19
use Longman\TelegramBot\Request;
20
21
/**
22
 * Admin "/debug" command
23
 */
24
class DebugCommand extends AdminCommand
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $name = 'debug';
30
31
    /**
32
     * @var string
33
     */
34
    protected $description = 'Debug command to help find issues';
35
36
    /**
37
     * @var string
38
     */
39
    protected $usage = '/debug';
40
41
    /**
42
     * @var string
43
     */
44
    protected $version = '1.1.0';
45
46
    /**
47
     * Command execute method
48
     *
49
     * @return mixed
50
     * @throws TelegramException
51
     */
52
    public function execute(): ServerResponse
53
    {
54
        $pdo     = DB::getPdo();
55
        $message = $this->getMessage();
56
        $chat    = $message->getChat();
57
        $text    = strtolower($message->getText(true));
0 ignored issues
show
It seems like $message->getText(true) can also be of type null; however, parameter $string of strtolower() 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

57
        $text    = strtolower(/** @scrutinizer ignore-type */ $message->getText(true));
Loading history...
58
59
        $data = ['chat_id' => $chat->getId()];
60
61
        if ($text !== 'glasnost' && !$chat->isPrivateChat()) {
62
            $data['text'] = 'Only available in a private chat.';
63
64
            return Request::sendMessage($data);
65
        }
66
67
        $debug_info = [];
68
69
        $debug_info[] = sprintf('*TelegramBot version:* `%s`', $this->telegram->getVersion());
70
        $debug_info[] = sprintf('*Download path:* `%s`', $this->telegram->getDownloadPath() ?: '`_Not set_`');
71
        $debug_info[] = sprintf('*Upload path:* `%s`', $this->telegram->getUploadPath() ?: '`_Not set_`');
72
73
        // Commands paths.
74
        $debug_info[] = '*Commands paths:*';
75
        $debug_info[] = sprintf(
76
            '```' . PHP_EOL . '%s```',
77
            json_encode($this->telegram->getCommandsPaths(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
78
        );
79
80
        $php_bit = '';
81
        PHP_INT_SIZE === 4 && $php_bit = ' (32bit)';
82
        PHP_INT_SIZE === 8 && $php_bit = ' (64bit)';
83
        $debug_info[] = sprintf('*PHP version:* `%1$s%2$s; %3$s; %4$s`', PHP_VERSION, $php_bit, PHP_SAPI, PHP_OS);
84
        $debug_info[] = sprintf('*Maximum PHP script execution time:* `%d seconds`', ini_get('max_execution_time'));
85
86
        $mysql_version = $pdo ? $pdo->query('SELECT VERSION() AS version')->fetchColumn() : null;
0 ignored issues
show
$pdo is of type PDO, thus it always evaluated to true.
Loading history...
87
        $debug_info[]  = sprintf('*MySQL version:* `%s`', $mysql_version ?: 'disabled');
88
89
        $debug_info[] = sprintf('*Operating System:* `%s`', php_uname());
90
91
        if (isset($_SERVER['SERVER_SOFTWARE'])) {
92
            $debug_info[] = sprintf('*Web Server:* `%s`', $_SERVER['SERVER_SOFTWARE']);
93
        }
94
        if (function_exists('curl_init')) {
95
            $curlversion  = curl_version();
96
            $debug_info[] = sprintf('*curl version:* `%1$s; %2$s`', $curlversion['version'], $curlversion['ssl_version']);
97
        }
98
99
        $webhook_info_title = '*Webhook Info:*';
100
        try {
101
            // Check if we're actually using the Webhook method.
102
            if (Request::getInput() === '') {
103
                $debug_info[] = $webhook_info_title . ' `Using getUpdates method, not Webhook.`';
104
            } else {
105
                $webhook_info_result = json_decode(Request::getWebhookInfo(), true)['result'];
106
                // Add a human-readable error date string if necessary.
107
                if (isset($webhook_info_result['last_error_date'])) {
108
                    $webhook_info_result['last_error_date_string'] = date('Y-m-d H:i:s', $webhook_info_result['last_error_date']);
109
                }
110
111
                $webhook_info_result_str = json_encode($webhook_info_result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
112
                $debug_info[]            = $webhook_info_title;
113
                $debug_info[]            = sprintf(
114
                    '```' . PHP_EOL . '%s```',
115
                    $webhook_info_result_str
116
                );
117
            }
118
        } catch (Exception $e) {
119
            $debug_info[] = $webhook_info_title . sprintf(' `Failed to get webhook info! (%s)`', $e->getMessage());
120
        }
121
122
        $data['parse_mode'] = 'Markdown';
123
        $data['text']       = implode(PHP_EOL, $debug_info);
124
125
        return Request::sendMessage($data);
126
    }
127
}
128