| Conditions | 33 |
| Total Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 156 |