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.

WhoisCommand::execute()   F
last analyzed

Complexity

Conditions 20
Paths 534

Size

Total Lines 128
Code Lines 81

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 0
Metric Value
cc 20
eloc 81
nc 534
nop 0
dl 0
loc 128
ccs 0
cts 99
cp 0
crap 420
rs 0.6472
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
 * Written by Jack'lul <[email protected]>
12
 */
13
14
namespace Longman\TelegramBot\Commands\AdminCommands;
15
16
use Longman\TelegramBot\Commands\AdminCommand;
17
use Longman\TelegramBot\DB;
18
use Longman\TelegramBot\Entities\Chat;
19
use Longman\TelegramBot\Entities\PhotoSize;
20
use Longman\TelegramBot\Entities\ServerResponse;
21
use Longman\TelegramBot\Entities\UserProfilePhotos;
22
use Longman\TelegramBot\Exception\TelegramException;
23
use Longman\TelegramBot\Request;
24
25
/**
26
 * Admin "/whois" command
27
 */
28
class WhoisCommand extends AdminCommand
29
{
30
    /**
31
     * @var string
32
     */
33
    protected $name = 'whois';
34
35
    /**
36
     * @var string
37
     */
38
    protected $description = 'Lookup user or group info';
39
40
    /**
41
     * @var string
42
     */
43
    protected $usage = '/whois <id> or /whois <search string>';
44
45
    /**
46
     * @var string
47
     */
48
    protected $version = '1.3.0';
49
50
    /**
51
     * @var bool
52
     */
53
    protected $need_mysql = true;
54
55
    /**
56
     * Command execute method
57
     *
58
     * @return ServerResponse
59
     * @throws TelegramException
60
     */
61
    public function execute(): ServerResponse
62
    {
63
        $message = $this->getMessage();
64
65
        $chat_id = $message->getChat()->getId();
66
        $command = $message->getCommand();
67
        $text    = trim($message->getText(true));
68
69
        $data = ['chat_id' => $chat_id];
70
71
        //No point in replying to messages in private chats
72
        if (!$message->getChat()->isPrivateChat()) {
73
            $data['reply_to_message_id'] = $message->getMessageId();
74
        }
75
76
        if ($command !== 'whois') {
77
            $text = substr($command, 5);
0 ignored issues
show
Bug introduced by
It seems like $command can also be of type null; however, parameter $string of substr() 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

77
            $text = substr(/** @scrutinizer ignore-type */ $command, 5);
Loading history...
78
79
            //We need that '-' now, bring it back
80
            if (strpos($text, 'g') === 0) {
81
                $text = str_replace('g', '-', $text);
82
            }
83
        }
84
85
        if ($text === '') {
86
            $text = 'Provide the id to lookup: /whois <id>';
87
        } else {
88
            $user_id    = $text;
89
            $chat       = null;
90
            $created_at = null;
91
            $updated_at = null;
92
            $result     = null;
93
94
            if (is_numeric($text)) {
95
                $results = DB::selectChats([
96
                    'groups'      => true,
97
                    'supergroups' => true,
98
                    'channels'    => true,
99
                    'users'       => true,
100
                    'chat_id'     => $user_id, //Specific chat_id to select
101
                ]);
102
103
                if (!empty($results)) {
104
                    $result = reset($results);
105
                }
106
            } else {
107
                $results = DB::selectChats([
108
                    'groups'      => true,
109
                    'supergroups' => true,
110
                    'channels'    => true,
111
                    'users'       => true,
112
                    'text'        => $text //Text to search in user/group name
113
                ]);
114
115
                if (is_array($results) && count($results) === 1) {
116
                    $result = reset($results);
117
                }
118
            }
119
120
            if (is_array($result)) {
121
                $result['id']       = $result['chat_id'];
122
                $result['username'] = $result['chat_username'];
123
                $chat               = new Chat($result);
124
125
                $user_id    = $result['id'];
126
                $created_at = $result['chat_created_at'];
127
                $updated_at = $result['chat_updated_at'];
128
                $old_id     = $result['old_id'];
129
            }
130
131
            if ($chat !== null) {
132
                if ($chat->isPrivateChat()) {
133
                    $text = 'User ID: ' . $user_id . PHP_EOL;
134
                    $text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . PHP_EOL;
135
136
                    $username = $chat->getUsername();
137
                    if ($username !== null && $username !== '') {
138
                        $text .= 'Username: @' . $username . PHP_EOL;
139
                    }
140
141
                    $text .= 'First time seen: ' . $created_at . PHP_EOL;
142
                    $text .= 'Last activity: ' . $updated_at . PHP_EOL;
143
144
                    //Code from Whoami command
145
                    $limit    = 10;
146
                    $offset   = null;
147
                    $response = Request::getUserProfilePhotos(
148
                        [
149
                            'user_id' => $user_id,
150
                            'limit'   => $limit,
151
                            'offset'  => $offset,
152
                        ]
153
                    );
154
155
                    if ($response->isOk()) {
156
                        /** @var UserProfilePhotos $user_profile_photos */
157
                        $user_profile_photos = $response->getResult();
158
159
                        if ($user_profile_photos->getTotalCount() > 0) {
160
                            $photos = $user_profile_photos->getPhotos();
161
162
                            /** @var PhotoSize $photo */
163
                            $photo   = $photos[0][2];
164
                            $file_id = $photo->getFileId();
165
166
                            $data['photo']   = $file_id;
167
                            $data['caption'] = $text;
168
169
                            return Request::sendPhoto($data);
170
                        }
171
                    }
172
                } elseif ($chat->isGroupChat()) {
173
                    $text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . PHP_EOL;
174
                    $text .= 'Type: ' . ucfirst($chat->getType()) . PHP_EOL;
175
                    $text .= 'Title: ' . $chat->getTitle() . PHP_EOL;
176
                    $text .= 'First time added to group: ' . $created_at . PHP_EOL;
177
                    $text .= 'Last activity: ' . $updated_at . PHP_EOL;
178
                }
179
            } elseif (is_array($results) && count($results) > 1) {
180
                $text = 'Multiple chats matched!';
181
            } else {
182
                $text = 'Chat not found!';
183
            }
184
        }
185
186
        $data['text'] = $text;
187
188
        return Request::sendMessage($data);
189
    }
190
}
191