Conditions | 11 |
Paths | 48 |
Total Lines | 87 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 4 | 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 |
||
83 | public function run() |
||
84 | { |
||
85 | // Most EVE players on Discord use their ingame name, so lets support @highlights |
||
86 | $explode = explode(" ", $this->message->content); |
||
87 | unset($explode[0]); |
||
88 | $name = implode(" ", $explode); |
||
89 | $name = stristr($name, "@") ? str_replace("<@", "", str_replace(">", "", $name)) : $name; |
||
90 | |||
91 | if (is_numeric($name)) { |
||
92 | // The person used @highlighting, so now we got a discord id, lets map that to a name |
||
93 | $name = $this->db->queryField("SELECT nickName FROM users WHERE discordID = :id", "nickName", array(":id" => $name)); |
||
94 | } |
||
95 | |||
96 | $url = "https://evedata.xyz/api/search/character/" . urlencode($name) . "/"; |
||
97 | $data = @json_decode($this->curl->get($url), true)["character"]; |
||
98 | if (empty($data)) { |
||
99 | return $this->message->reply("**Error:** no results was returned."); |
||
100 | } |
||
101 | |||
102 | $exists = false; |
||
103 | if (count($data) > 1) { |
||
104 | $results = array(); |
||
105 | foreach ($data as $char) { |
||
106 | if (strtolower($char["characterName"]) == strtolower($name)) { |
||
107 | $data[0]["characterID"] = $char["characterID"]; |
||
108 | $exists = true; |
||
109 | } |
||
110 | $results[] = $char["characterName"]; |
||
111 | } |
||
112 | if ($exists == false) { |
||
113 | return $this->message->reply("**Error:** more than one result was returned: " . implode(", ", $results)); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | // Get stats |
||
118 | $characterID = $data[0]["characterID"]; |
||
119 | $statsURL = "https://beta.eve-kill.net/api/charInfo/characterID/" . urlencode($characterID) . "/"; |
||
120 | $stats = json_decode($this->curl->get($statsURL), true); |
||
121 | if (empty($stats)) { |
||
122 | return $this->message->reply("**Error:** no data available"); |
||
123 | } |
||
124 | |||
125 | $characterName = @$stats["characterName"]; |
||
126 | $corporationName = @$stats["corporationName"]; |
||
127 | $allianceName = isset($stats["allianceName"]) ? $stats["allianceName"] : "None"; |
||
128 | $factionName = isset($stats["factionName"]) ? $stats["factionName"] : "None"; |
||
129 | $securityStatus = @$stats["securityStatus"]; |
||
130 | $lastSeenSystem = @$stats["lastSeenSystem"]; |
||
131 | $lastSeenRegion = @$stats["lastSeenRegion"]; |
||
132 | $lastSeenShip = @$stats["lastSeenShip"]; |
||
133 | $lastSeenDate = @$stats["lastSeenDate"]; |
||
134 | $corporationActiveArea = @$stats["corporationActiveArea"]; |
||
135 | $allianceActiveArea = @$stats["allianceActiveArea"]; |
||
136 | $soloKills = @$stats["soloKills"]; |
||
137 | $blobKills = @$stats["blobKills"]; |
||
138 | $lifeTimeKills = @$stats["lifeTimeKills"]; |
||
139 | $lifeTimeLosses = @$stats["lifeTimeLosses"]; |
||
140 | $amountOfSoloPVPer = @$stats["percentageSoloPVPer"]; |
||
141 | $ePeenSize = @$stats["ePeenSize"]; |
||
142 | $facepalms = @$stats["facepalms"]; |
||
143 | $lastUpdated = @$stats["lastUpdatedOnBackend"]; |
||
144 | $url = "https://beta.eve-kill.net/character/" . $stats["characterID"] . "/"; |
||
145 | $msg = "```characterName: {$characterName} |
||
146 | corporationName: {$corporationName} |
||
147 | allianceName: {$allianceName} |
||
148 | factionName: {$factionName} |
||
149 | securityStatus: {$securityStatus} |
||
150 | lastSeenSystem: {$lastSeenSystem} |
||
151 | lastSeenRegion: {$lastSeenRegion} |
||
152 | lastSeenShip: {$lastSeenShip} |
||
153 | lastSeenDate: {$lastSeenDate} |
||
154 | corporationActiveArea: {$corporationActiveArea} |
||
155 | allianceActiveArea: {$allianceActiveArea} |
||
156 | soloKills: {$soloKills} |
||
157 | blobKills: {$blobKills} |
||
158 | lifeTimeKills: {$lifeTimeKills} |
||
159 | lifeTimeLosses: {$lifeTimeLosses} |
||
160 | percentageSoloPVPer: {$amountOfSoloPVPer} |
||
161 | ePeenSize: {$ePeenSize} |
||
162 | facepalms: {$facepalms} |
||
163 | lastUpdated: $lastUpdated``` |
||
164 | For more info, visit: $url"; |
||
165 | $this->message->reply($msg); |
||
166 | |||
167 | // Mark this as garbage |
||
168 | $this->isGarbage(); |
||
169 | } |
||
170 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: