| Conditions | 6 |
| Paths | 6 |
| Total Lines | 51 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 55 | public function onMessage($msgData, $message) |
||
| 56 | { |
||
| 57 | $this->message = $message; |
||
| 58 | |||
| 59 | $message = $msgData['message']['message']; |
||
| 60 | |||
| 61 | $data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']); |
||
| 62 | if (isset($data['trigger'])) { |
||
| 63 | |||
| 64 | //Admin Check |
||
| 65 | $userID = $msgData['message']['fromID']; |
||
| 66 | $adminRoles = $this->config['bot']['adminRoles']; |
||
| 67 | $adminRoles = array_map('strtolower', $adminRoles); |
||
| 68 | $id = $this->config['bot']['guild']; |
||
| 69 | $guild = $this->discord->guilds->get('id', $id); |
||
| 70 | $member = $guild->members->get('id', $userID); |
||
| 71 | $roles = $member->roles; |
||
| 72 | foreach ($roles as $role) { |
||
| 73 | if (in_array(strtolower($role->name), $adminRoles, true)) { |
||
| 74 | $avatarURL = strtolower((string)$data['messageString']); |
||
| 75 | $ch = curl_init($avatarURL); |
||
| 76 | if (substr($avatarURL, -4) === '.jpg') { |
||
| 77 | $fp = fopen('/tmp/avatar.jpg', 'wb'); |
||
| 78 | $dir = '/tmp/avatar.jpg'; |
||
| 79 | } elseif (substr($avatarURL, -4) === '.png') { |
||
| 80 | $fp = fopen('/tmp/avatar.png', 'wb'); |
||
| 81 | $dir = '/tmp/avatar.png'; |
||
| 82 | } else { |
||
| 83 | return $this->message->reply('Invalid URL. Make sure the URL links directly to a JPG or PNG.'); |
||
| 84 | } |
||
| 85 | curl_setopt($ch, CURLOPT_FILE, $fp); |
||
| 86 | curl_setopt($ch, CURLOPT_HEADER, 0); |
||
| 87 | curl_exec($ch); |
||
| 88 | curl_close($ch); |
||
| 89 | fclose($fp); |
||
| 90 | $this->discord->setAvatar($dir); |
||
| 91 | $this->discord->save(); |
||
| 92 | |||
| 93 | $msg = 'New avatar set'; |
||
| 94 | $this->logger->addInfo("setGame: Bot avatar changed to {$avatarURL} by {$msgData['message']['from']}"); |
||
| 95 | $this->message->reply($msg); |
||
| 96 | return null; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $this->logger->addInfo("setGame: {$msgData['message']['from']} attempted to change the bot's avatar."); |
||
| 100 | $msg = ':bangbang: You do not have the necessary roles to issue this command :bangbang:'; |
||
| 101 | $this->message->reply($msg); |
||
| 102 | return null; |
||
| 103 | } |
||
| 104 | return null; |
||
| 105 | } |
||
| 106 | |||
| 120 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.