GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DebugCommand::execute()   F
last analyzed

Complexity

Conditions 14
Paths 449

Size

Total Lines 74
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
cc 14
eloc 47
nc 449
nop 0
dl 0
loc 74
ccs 0
cts 54
cp 0
crap 210
rs 2.8652
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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));
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
introduced by
$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