help.php$0 ➔ renderHelpMessage()   D
last analyzed

Complexity

Conditions 33

Size

Total Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 33
dl 0
loc 96
rs 4.1666
c 3
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
 * Livia
4
 * Copyright 2017-2019 Charlotte Dunois, All Rights Reserved
5
 *
6
 * Website: https://charuru.moe
7
 * License: https://github.com/CharlotteDunois/Livia/blob/master/LICENSE
8
*/
9
10
return function ($client) {
11
    return (new class($client) extends \CharlotteDunois\Livia\Commands\Command {
12
        function __construct(\CharlotteDunois\Livia\Client $client) {
13
            parent::__construct($client, array(
14
                'name' => 'help',
15
                'aliases' => array('commands'),
16
                'group' => 'utils',
17
                'description' => 'Displays a list of available commands, or detailed information for a specified command.',
18
                'details' => "The command may be part of a command name or a whole command name.\nIf it isn't specified, all available commands will be listed.",
19
                'examples' => array('help', 'help prefix'),
20
                'guildOnly' => false,
21
                'throttling' => array(
22
                    'usages' => 2,
23
                    'duration' => 3
24
                ),
25
                'args' => array(
26
                    array(
27
                        'key' => 'command',
28
                        'prompt' => 'Which command would you like to view the help for?',
29
                        'type' => 'string',
30
                        'default' => ''
31
                    )
32
                ),
33
                'guarded' => true
34
            ));
35
        }
36
        
37
        function run(\CharlotteDunois\Livia\Commands\Context $context, \ArrayObject $args, bool $fromPattern) {
38
            return $context->direct($this->renderHelpMessage($context, $args), array('split' => true))->then(function ($msg) use ($context) {
39
                if(!($context->message->channel instanceof \CharlotteDunois\Yasmin\Interfaces\DMChannelInterface)) {
40
                    return $context->reply('Sent you a DM with information.');
41
                }
42
                
43
                return $msg;
44
            }, function () use ($context) {
45
                if(!($context->message->channel instanceof \CharlotteDunois\Yasmin\Interfaces\DMChannelInterface)) {
46
                    return $context->reply('Unable to send you the help DM. You probably have DMs disabled.');
47
                }
48
            });
49
        }
50
        
51
        /**
52
         * @param \CharlotteDunois\Livia\Commands\Context  $context
53
         * @param \ArrayObject                             $args
54
         * @return string
55
         */
56
        function renderHelpMessage(\CharlotteDunois\Livia\Commands\Context $context, \ArrayObject $args) {
57
            $groups = $this->client->registry->groups;
58
            $commands = (!empty($args['command']) ? $this->client->registry->findCommands($args['command'], false, $context->message) : $this->client->registry->commands->all());
59
            
60
            $isDM = ($context->message->channel instanceof \CharlotteDunois\Yasmin\Interfaces\DMChannelInterface);
61
            $showAll = (!empty($args['command']) && \mb_strtolower($args['command']) === 'all');
62
            
63
            if(!empty($args['command']) && !$showAll) {
64
                $countCommands = \count($commands);
65
                
66
                if($countCommands === 0) {
67
                    return 'Unable to identify command. Use '.$this->usage('', ($isDM ? null : $this->client->getGuildPrefix($context->message->guild)), ($isDM ? null : $this->client->user)).' to view the list of all commands.';
68
                }
69
                
70
                /** @var \CharlotteDunois\Livia\Commands\Command  $cmd */
71
                foreach($commands as $key => $cmd) {
72
                    if($cmd->ownerOnly && $cmd->hasPermission($context) !== true) {
73
                        unset($commands[$key]);
74
                    }
75
                }
76
                
77
                $countCommands = \count($commands);
78
                
79
                if($countCommands === 1) {
80
                    $command = $commands[0];
81
                    
82
                    $help = "__Command **{$command->name}**:__ {$command->description} ".($command->guildOnly ? '(Usable only in servers)' : '').\PHP_EOL.\PHP_EOL.
83
                            '**Format:** '.\CharlotteDunois\Livia\Commands\Command::anyUsage($command->name.(!empty($command->format) ? ' '.$command->format : '')).\PHP_EOL;
84
                            
85
                    if(!empty($command->aliases)) {
86
                        $help .= \PHP_EOL.'**Aliases:** '.\implode(', ', $command->aliases);
87
                    }
88
                    
89
                    $help .= \PHP_EOL."**Group:** {$command->group->name} (`{$command->groupID}:{$command->name}`)";
90
                    
91
                    if(!empty($command->details)) {
92
                        $help .= \PHP_EOL.'**Details:** '.$command->details;
93
                    }
94
                    
95
                    if(!empty($command->examples)) {
96
                        $help .= \PHP_EOL.'**Examples:**'.\PHP_EOL.\implode(\PHP_EOL, $command->examples);
97
                    }
98
                    
99
                    return $help;
100
                } elseif($countCommands > 15) {
101
                    return 'Multiple commands found. Please be more specific.';
102
                } elseif($countCommands > 1) {
103
                    return \CharlotteDunois\Livia\Utils\DataHelpers::disambiguation($commands, 'commands', 'name');
104
                }
105
            } else {
106
                $help = 'To run a command in '.($context->message->guild !== null ? $context->message->guild->name : 'any server').', use '.
107
                        \CharlotteDunois\Livia\Commands\Command::anyUsage('command', $this->client->getGuildPrefix($context->message->guild), $this->client->user).
108
                        '. For example, '.
109
                        \CharlotteDunois\Livia\Commands\Command::anyUsage('prefix', $this->client->getGuildPrefix($context->message->guild), $this->client->user).'.'.\PHP_EOL.
110
                        'To run a command in this DM, simply use '.\CharlotteDunois\Livia\Commands\Command::anyUsage('command').' with no prefix.'.\PHP_EOL.\PHP_EOL.
111
                        'Use '.$this->usage('<command>', null, null).' to view detailed information about a specific command.'.\PHP_EOL.
112
                        'Use '.$this->usage('all', null, null).' to view a list of *all* commands, not just available ones.'.\PHP_EOL.\PHP_EOL.
113
                        '__**'.($showAll ? 'All commands' : 'Available commands in '.($context->message->guild !== null ? $context->message->guild->name : 'this DM')).'**__'.\PHP_EOL.\PHP_EOL.
114
                        \implode(\PHP_EOL.\PHP_EOL, \array_map(function (\CharlotteDunois\Livia\Commands\CommandGroup $group) use ($context, $showAll) {
115
                            $cmds = ($showAll ? $group->commands->filter(function (\CharlotteDunois\Livia\Commands\Command $cmd) use ($context) {
116
                                return (!$cmd->hidden && (!$cmd->ownerOnly || $this->client->isOwner($context->message->author)));
117
                            }) : $group->commands->filter(function (\CharlotteDunois\Livia\Commands\Command $cmd) use ($context) {
118
                                return (!$cmd->hidden && $cmd->isUsable($context));
119
                            }));
120
                            
121
                            return "__{$group->name}__".\PHP_EOL.
122
                                \implode(\PHP_EOL, $cmds->sortCustom(function (\CharlotteDunois\Livia\Commands\Command $a, \CharlotteDunois\Livia\Commands\Command $b) {
123
                                    return $a->name <=> $b->name;
124
                                })->map(function (\CharlotteDunois\Livia\Commands\Command $cmd) {
125
                                    return "**{$cmd->name}:** {$cmd->description}";
126
                                })->all());
127
                        }, ($showAll ? $groups->filter(function (\CharlotteDunois\Livia\Commands\CommandGroup $group) use ($context) {
128
                            /** @var \CharlotteDunois\Livia\Commands\Command  $cmd */
129
                            foreach($group->commands as $cmd) {
130
                                if(!$cmd->hidden && (!$cmd->ownerOnly || $this->client->isOwner($context->message->author))) {
131
                                    return true;
132
                                }
133
                            }
134
                            
135
                            return false;
136
                        })->sortCustom(function ($a, $b) {
137
                            return $a->name <=> $b->name;
138
                        })->all() : $groups->filter(function (\CharlotteDunois\Livia\Commands\CommandGroup $group) use ($context) {
139
                            /** @var \CharlotteDunois\Livia\Commands\Command  $cmd */
140
                            foreach($group->commands as $cmd) {
141
                                if($cmd->isUsable($context)) {
142
                                    return true;
143
                                }
144
                            }
145
                            
146
                            return false;
147
                        })->sortCustom(function ($a, $b) {
148
                            return $a->name <=> $b->name;
149
                        })->all())));
150
                
151
                return $help;
152
            }
153
        }
154
    });
155
};
156