Passed
Pull Request — develop (#1492)
by Rabie
07:07
created

DebugCommand::execute()   D

Complexity

Conditions 15
Paths 225

Size

Total Lines 72
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
cc 15
eloc 45
c 0
b 0
f 0
nc 225
nop 0
dl 0
loc 72
rs 4.7708
ccs 0
cts 46
cp 0
crap 240

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\Entities\ServerResponse;
17
use Longman\TelegramBot\Exception\TelegramException;
18
use Longman\TelegramBot\Request;
19
20
/**
21
 * Admin "/debug" command
22
 */
23
class DebugCommand extends AdminCommand
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $name = 'debug';
29
30
    /**
31
     * @var string
32
     */
33
    protected $description = 'Debug command to help find issues';
34
35
    /**
36
     * @var string
37
     */
38
    protected $usage = '/debug';
39
40
    /**
41
     * @var string
42
     */
43
    protected $version = '1.1.0';
44
45
    /**
46
     * Command execute method
47
     *
48
     * @return mixed
49
     * @throws TelegramException
50
     */
51
    public function execute(): ServerResponse
52
    {
53
        $message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost();
54
        $chat    = $message->getChat();
55
        $text    = strtolower($message->getText(true));
0 ignored issues
show
Bug introduced by
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

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