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.

ChatsCommand::execute()   D
last analyzed

Complexity

Conditions 18
Paths 18

Size

Total Lines 88
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
cc 18
eloc 56
nc 18
nop 0
dl 0
loc 88
ccs 0
cts 69
cp 0
crap 342
rs 4.8666
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 Longman\TelegramBot\Commands\AdminCommand;
15
use Longman\TelegramBot\DB;
16
use Longman\TelegramBot\Entities\Chat;
17
use Longman\TelegramBot\Entities\ServerResponse;
18
use Longman\TelegramBot\Exception\TelegramException;
19
use Longman\TelegramBot\Request;
20
21
class ChatsCommand extends AdminCommand
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $name = 'chats';
27
28
    /**
29
     * @var string
30
     */
31
    protected $description = 'List or search all chats stored by the bot';
32
33
    /**
34
     * @var string
35
     */
36
    protected $usage = '/chats, /chats * or /chats <search string>';
37
38
    /**
39
     * @var string
40
     */
41
    protected $version = '1.2.0';
42
43
    /**
44
     * @var bool
45
     */
46
    protected $need_mysql = true;
47
48
    /**
49
     * Command execute method
50
     *
51
     * @return ServerResponse
52
     * @throws TelegramException
53
     */
54
    public function execute(): ServerResponse
55
    {
56
        $message = $this->getMessage();
57
58
        $chat_id = $message->getChat()->getId();
59
        $text    = trim($message->getText(true));
60
61
        $results = DB::selectChats([
62
            'groups'      => true,
63
            'supergroups' => true,
64
            'channels'    => true,
65
            'users'       => true,
66
            'text'        => ($text === '' || $text === '*') ? null : $text //Text to search in user/group name
67
        ]);
68
69
        $user_chats       = 0;
70
        $group_chats      = 0;
71
        $supergroup_chats = 0;
72
        $channel_chats    = 0;
73
74
        if ($text === '') {
75
            $text_back = '';
76
        } elseif ($text === '*') {
77
            $text_back = 'List of all bot chats:' . PHP_EOL;
78
        } else {
79
            $text_back = 'Chat search results:' . PHP_EOL;
80
        }
81
82
        if (is_array($results)) {
83
            foreach ($results as $result) {
84
                //Initialize a chat object
85
                $result['id'] = $result['chat_id'];
86
                $chat         = new Chat($result);
87
88
                $whois = $chat->getId();
89
                if ($this->telegram->getCommandObject('whois')) {
90
                    // We can't use '-' in command because part of it will become unclickable
91
                    $whois = '/whois' . str_replace('-', 'g', $chat->getId());
92
                }
93
94
                if ($chat->isPrivateChat()) {
95
                    if ($text !== '') {
96
                        $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . PHP_EOL;
97
                    }
98
99
                    ++$user_chats;
100
                } elseif ($chat->isSuperGroup()) {
101
                    if ($text !== '') {
102
                        $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
103
                    }
104
105
                    ++$supergroup_chats;
106
                } elseif ($chat->isGroupChat()) {
107
                    if ($text !== '') {
108
                        $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
109
                    }
110
111
                    ++$group_chats;
112
                } elseif ($chat->isChannel()) {
113
                    if ($text !== '') {
114
                        $text_back .= '- C ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
115
                    }
116
117
                    ++$channel_chats;
118
                }
119
            }
120
        }
121
122
        if (($user_chats + $group_chats + $supergroup_chats) === 0) {
123
            $text_back = 'No chats found..';
124
        } else {
125
            $text_back .= PHP_EOL . 'Private Chats: ' . $user_chats;
126
            $text_back .= PHP_EOL . 'Groups: ' . $group_chats;
127
            $text_back .= PHP_EOL . 'Super Groups: ' . $supergroup_chats;
128
            $text_back .= PHP_EOL . 'Channels: ' . $channel_chats;
129
            $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $supergroup_chats);
130
131
            if ($text === '') {
132
                $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>';
133
            }
134
        }
135
136
        $data = [
137
            'chat_id' => $chat_id,
138
            'text'    => $text_back,
139
        ];
140
141
        return Request::sendMessage($data);
142
    }
143
}
144