Conditions | 30 |
Paths | 54 |
Total Lines | 126 |
Code Lines | 92 |
Lines | 31 |
Ratio | 24.6 % |
Changes | 6 | ||
Bugs | 3 | Features | 1 |
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 |
||
82 | public function run() |
||
83 | { |
||
84 | $guildID = $this->message->full_channel->guild->id; |
||
85 | $input = explode(" ", $this->message->content); |
||
86 | unset($input[0]); |
||
87 | $type = isset($input[1]) ? $input[1] : ""; |
||
88 | unset($input[1]); |
||
89 | |||
90 | // Defaults |
||
91 | $channelID = $this->message->channel_id; |
||
92 | $msg = ""; |
||
93 | |||
94 | // Config options |
||
95 | switch (trim($type)) { |
||
96 | case "setTrigger": |
||
97 | $trigger = $input[2]; |
||
98 | $orgTrigger = $this->serverConfig->get($guildID, "prefix") ? $this->serverConfig->get($guildID, "prefix") : $this->channelConfig->prefix; |
||
99 | $this->serverConfig->set($guildID, "prefix", $trigger); |
||
100 | $msg = "Trigger has been changed from {$orgTrigger} to {$trigger}"; |
||
101 | break; |
||
102 | case "enablePorn": |
||
103 | $pornArray = $this->serverConfig->getAll($guildID)->porn->allowedChannels; |
||
104 | if (!in_array($channelID, $pornArray)) { |
||
105 | $pornArray[] = $channelID; |
||
106 | } |
||
107 | |||
108 | $this->serverConfig->set($guildID, "porn", array("allowedChannels" => $pornArray)); |
||
109 | $msg = "Porn has now been enabled on this channel, enjoy, you perv ;)"; |
||
110 | break; |
||
111 | case "disablePorn": |
||
112 | $pornArray = $this->serverConfig->getAll($guildID)->porn->allowedChannels; |
||
113 | foreach ($pornArray as $key => $value) { |
||
114 | if ($value == $channelID) { |
||
115 | unset($pornArray[$key]); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | $this->serverConfig->set($guildID, "porn", array("allowedChannels" => $pornArray)); |
||
120 | $msg = "Porn has now been disabled on this channel. :("; |
||
121 | break; |
||
122 | case "addKillmails": |
||
123 | // %config addKillmails character characterID |
||
124 | $typeName = trim($input[2]); |
||
125 | $typeID = trim($input[3]); |
||
126 | |||
127 | switch ($typeName) { |
||
128 | View Code Duplication | case "character": |
|
129 | // Check said char exists on the killboard.. |
||
130 | $exists = json_decode($this->curl->get("https://evedata.xyz/api/character/information/{$typeID}/")); |
||
131 | if (isset($exists->characterID)) { |
||
132 | $this->db->execute("INSERT IGNORE INTO killmailPosting (channelID, typeName, typeID) VALUES (:channelID, :typeName, :typeID)", array(":channelID" => $channelID, ":typeName" => $typeName, ":typeID" => $typeID)); |
||
133 | $msg = "**Success** killmails should start getting posted for {$exists->characterName} to this channel"; |
||
134 | } else { |
||
135 | $msg = "**Error** characterID is not valid"; |
||
136 | } |
||
137 | break; |
||
138 | |||
139 | View Code Duplication | case "corporation": |
|
140 | // Check said char exists on the killboard.. |
||
141 | $exists = json_decode($this->curl->get("https://evedata.xyz/api/corporation/information/{$typeID}/")); |
||
142 | if (isset($exists->corporationID)) { |
||
143 | $this->db->execute("INSERT IGNORE INTO killmailPosting (channelID, typeName, typeID) VALUES (:channelID, :typeName, :typeID)", array(":channelID" => $channelID, ":typeName" => $typeName, ":typeID" => $typeID)); |
||
144 | $msg = "**Success** killmails should start getting posted for {$exists->corporationName} to this channel"; |
||
145 | } else { |
||
146 | $msg = "**Error** corporationID is not valid"; |
||
147 | } |
||
148 | |||
149 | break; |
||
150 | |||
151 | View Code Duplication | case "alliance": |
|
152 | // Check said char exists on the killboard.. |
||
153 | $exists = json_decode($this->curl->get("https://evedata.xyz/api/alliance/information/{$typeID}/")); |
||
154 | if (isset($exists->allianceID)) { |
||
155 | $this->db->execute("INSERT IGNORE INTO killmailPosting (channelID, typeName, typeID) VALUES (:channelID, :typeName, :typeID)", array(":channelID" => $channelID, ":typeName" => $typeName, ":typeID" => $typeID)); |
||
156 | $msg = "**Success** killmails should start getting posted for {$exists->allianceName} to this channel"; |
||
157 | } else { |
||
158 | $msg = "**Error** allianceID is not valid"; |
||
159 | } |
||
160 | break; |
||
161 | } |
||
162 | break; |
||
163 | case "removeKillmails": |
||
164 | break; |
||
165 | case "listKillmails": |
||
166 | break; |
||
167 | case "addTwitterOauth": |
||
168 | // Add oauth settings for twitter, and send twitter messages to the channel it was enabled in, unless channelID was passed along |
||
169 | break; |
||
170 | case "removeTwitterOauth": |
||
171 | // Disable twitter, and remove the oauth keys |
||
172 | break; |
||
173 | case "addSiphonKey": |
||
174 | // Add an apikey used for checking for siphons, output to the channel it was enabled in, unless a channelID was passed along |
||
175 | break; |
||
176 | case "removeSiphonKey": |
||
177 | break; |
||
178 | case "addMailKey": |
||
179 | // same as add siphon |
||
180 | break; |
||
181 | case "removeMailKey": |
||
182 | break; |
||
183 | case "addNotificationKey": |
||
184 | // same as add siphon |
||
185 | break; |
||
186 | case "removeNotificationKey": |
||
187 | break; |
||
188 | case "addAuth": |
||
189 | // Enable authentication for a characterID, corporationID or allianceID - have multiple, and let them map 1:1 to groups on Discord (if group doesn't exist, create it) |
||
190 | break; |
||
191 | case "removeAuth": |
||
192 | break; |
||
193 | case "addJabberReader": |
||
194 | // Setup a socket to listen for messages, make them prepend a key for the channel it was enabled in (unless a channelID was specified) |
||
195 | break; |
||
196 | case "removeJabberReader": |
||
197 | break; |
||
198 | default: |
||
199 | $msg = "Error, no configuration option picked. Available configuration options are: setTrigger, enablePorn, disablePorn, addTwitterOauth, removeTwitterOauth, addSiphonKey, removeSiphonKey, addMailKey, removeMailKey, addNotificationKey, removeNotificationKey, addJabberReader, removeJabberReader, addAuth, removeAuth"; |
||
200 | break; |
||
201 | } |
||
202 | |||
203 | $this->message->reply($msg); |
||
204 | |||
205 | // Mark this as garbage |
||
206 | $this->isGarbage(); |
||
207 | } |
||
208 | } |
||
209 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.