Conditions | 13 |
Paths | 116 |
Total Lines | 78 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
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 |
||
84 | public function run() |
||
85 | { |
||
86 | $explode = explode(" ", $this->message->content); |
||
87 | $prefix = $this->channelConfig->prefix; |
||
88 | $system = isset($explode[0]) ? $explode[0] == "{$prefix}pc" ? "global" : str_replace($prefix, "", $explode[0]) : "global"; |
||
89 | unset($explode[0]); |
||
90 | $item = implode(" ", $explode); |
||
91 | |||
92 | // Stuff that doesn't need a db lookup |
||
93 | $quickLookUps = [ |
||
94 | "plex" => array("typeName" => "30 Day Pilot's License Extension (PLEX)", "typeID" => 29668), |
||
95 | "injector" => array("typeName" => "Skill Injector", "typeID" => 40520), |
||
96 | "extractor" => array("typeName" => "Skill Extractor", "typeID" => 40519) |
||
97 | ]; |
||
98 | |||
99 | if ($system && $item) { |
||
100 | if (isset($quickLookUps[$item])) { |
||
101 | $single = $quickLookUps[$item]; |
||
102 | $multiple = null; |
||
103 | } else { |
||
104 | $single = $this->db->queryRow("SELECT typeID, typeName FROM invTypes WHERE typeName = :item", array(":item" => $item)); |
||
105 | $multiple = $this->db->query("SELECT typeID, typeName FROM invTypes WHERE typeName LIKE :item LIMIT 5", array(":item" => $item)); |
||
106 | } |
||
107 | |||
108 | if (count($multiple) == 1) { |
||
109 | $single = $multiple[0]; |
||
110 | } |
||
111 | |||
112 | if (empty($single) && !empty($multiple)) { |
||
113 | $items = array(); |
||
114 | foreach ($multiple as $item) { |
||
115 | $items[] = $item["typeName"]; |
||
116 | } |
||
117 | $items = implode(", ", $items); |
||
118 | return $this->message->reply("**Multiple results found:** {$items}"); |
||
119 | } |
||
120 | |||
121 | // If there is a single result, we'll get data now! |
||
122 | if ($single) { |
||
123 | $typeID = $single["typeID"]; |
||
124 | $typeName = $single["typeName"]; |
||
125 | |||
126 | if ($system == "global") { |
||
127 | $system = "global"; |
||
128 | $data = new SimpleXMLElement($this->curl->get("https://api.eve-central.com/api/marketstat?typeid={$typeID}")); |
||
129 | } else { |
||
130 | $solarSystemID = $this->db->queryField("SELECT solarSystemID FROM mapSolarSystems WHERE solarSystemName = :system", "solarSystemID", array(":system" => $system)); |
||
131 | $data = new SimpleXMLElement($this->curl->get("https://api.eve-central.com/api/marketstat?usesystem={$solarSystemID}&typeid={$typeID}")); |
||
132 | } |
||
133 | $lowBuy = number_format((float)$data->marketstat->type->buy->min, 2); |
||
134 | $avgBuy = number_format((float)$data->marketstat->type->buy->avg, 2); |
||
135 | $highBuy = number_format((float)$data->marketstat->type->buy->max, 2); |
||
136 | $lowSell = number_format((float)$data->marketstat->type->sell->min, 2); |
||
137 | $avgSell = number_format((float)$data->marketstat->type->sell->avg, 2); |
||
138 | $highSell = number_format((float)$data->marketstat->type->sell->max, 2); |
||
139 | $solarSystemName = $system == "pc" ? "Global" : ucfirst($system); |
||
140 | $messageData = "``` |
||
141 | typeName: {$typeName} |
||
142 | solarSystemName: {$solarSystemName} |
||
143 | Buy: |
||
144 | Low: {$lowBuy} |
||
145 | Avg: {$avgBuy} |
||
146 | High: {$highBuy} |
||
147 | Sell: |
||
148 | Low: {$lowSell} |
||
149 | Avg: {$avgSell} |
||
150 | High: {$highSell}```"; |
||
151 | $this->message->reply($messageData); |
||
152 | } else { |
||
153 | $this->message->reply("**Error:** ***{$item}*** not found"); |
||
154 | } |
||
155 | } else { |
||
156 | $this->message->reply("**Error:** No itemName set.."); |
||
157 | } |
||
158 | |||
159 | // Mark this as garbage |
||
160 | $this->isGarbage(); |
||
161 | } |
||
162 | } |
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: