Complex classes like Detail often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Detail, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Detail { |
||
20 | |||
21 | public static function involvedships($array) |
||
22 | { |
||
23 | $involved = array(); |
||
24 | foreach($array as $inv) |
||
25 | { |
||
26 | if(isset($involved[$inv["shipTypeID"]]) && isset($inv["shipName"])) |
||
27 | $involved[$inv["shipTypeID"]] = array("shipName" => $inv["shipName"], "shipTypeID" => $inv["shipTypeID"], "count" => $involved[$inv["shipTypeID"]]["count"] + 1); |
||
28 | elseif(isset($inv["shipTypeID"]) && isset($inv["shipName"])) |
||
29 | { |
||
30 | $involved[$inv["shipTypeID"]] = array("shipName" => $inv["shipName"], "shipTypeID" => $inv["shipTypeID"], "count" => 1); |
||
31 | } |
||
32 | else |
||
33 | continue; |
||
34 | } |
||
35 | |||
36 | usort($involved, "sortByOrder"); |
||
37 | return $involved; |
||
38 | } |
||
39 | |||
40 | public static function sortByOrder($a, $b) |
||
44 | |||
45 | public static function usdeurgbp($totalprice) |
||
46 | { |
||
47 | $usd = 17; |
||
48 | $eur = 13; |
||
49 | $gbp = 10; |
||
50 | $plex = Price::getItemPrice("29668", date("Ymd")); |
||
51 | $usdval = $plex / $usd; |
||
52 | $eurval = $plex / $eur; |
||
53 | $gbpval = $plex / $gbp; |
||
54 | |||
55 | return array("usd" => $totalprice / $usdval, "eur" => $totalprice / $eurval, "gbp" => $totalprice / $gbpval); |
||
56 | } |
||
57 | |||
58 | public static function eftarray($md5, $items, $victimID = 0) |
||
193 | |||
194 | public static function combineditems($md5, $items) |
||
195 | { |
||
196 | $Cache = Cache::get($md5."combineditems"); |
||
197 | if($Cache) return $Cache; |
||
198 | |||
199 | // Create the new item array with combined items and whatnot |
||
200 | $itemList = array(); |
||
201 | foreach($items as $itm) |
||
202 | { |
||
203 | if (!isset($itm["inContainer"])) $itm["inContainer"] = 0; |
||
204 | if ($itm["inContainer"] == 1) $itm["flag"] = 0; |
||
205 | if (!isset($itm["flagName"])) $itm["flagName"] = Info::getFlagName($itm["flag"]); |
||
206 | for ($i = 0; $i <= 1; $i++) { |
||
207 | $mItem = $itm; |
||
208 | if ($i == 0) $mItem["qtyDropped"] = 0; |
||
209 | if ($i == 1) $mItem["qtyDestroyed"] = 0; |
||
210 | if ($mItem["qtyDropped"] == 0 && $mItem["qtyDestroyed"] == 0) continue; |
||
211 | $key = static::buildItemKey($mItem); |
||
212 | |||
213 | if(!isset($itemList[$key])) { |
||
214 | $itemList[$key] = $mItem; |
||
215 | $itemList[$key]["price"] = $mItem["price"] * ($mItem["qtyDropped"] + $mItem["qtyDestroyed"]); |
||
216 | } |
||
217 | else { |
||
218 | $itemList[$key]["qtyDropped"] += $mItem["qtyDropped"]; |
||
219 | $itemList[$key]["qtyDestroyed"] += $mItem["qtyDestroyed"]; |
||
220 | $itemList[$key]["price"] += $mItem["price"] * ($mItem["qtyDropped"] + $mItem["qtyDestroyed"]); |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | Cache::set($md5."combineditems", $itemList); |
||
225 | return $itemList; |
||
226 | } |
||
227 | |||
228 | public static function fullCombinedItems($md5, $items) |
||
229 | { |
||
230 | // Create the new item array with combined items and whatnot |
||
231 | $itemList = array(); |
||
232 | foreach($items as $itm) |
||
233 | { |
||
234 | if ($itm["fittable"] != 1) continue; |
||
235 | if (!isset($itm["inContainer"])) $itm["inContainer"] = 0; |
||
236 | if ($itm["inContainer"] == 1) $itm["flag"] = 0; |
||
237 | if (!isset($itm["flagName"])) $itm["flagName"] = Info::getFlagName($itm["flag"]); |
||
238 | |||
239 | $mItem = $itm; |
||
240 | if ($mItem["qtyDropped"] == 0 && $mItem["qtyDestroyed"] == 0) continue; |
||
241 | $key = $itm["typeID"]; |
||
242 | |||
243 | if(!isset($itemList[$key])) { |
||
244 | $itemList[$key] = $mItem; |
||
245 | $itemList[$key]["price"] = $mItem["price"] * ($mItem["qtyDropped"] + $mItem["qtyDestroyed"]); |
||
246 | } |
||
247 | else $itemList[$key]["qtyDropped"] += $mItem["qtyDropped"]; |
||
248 | $itemList[$key]["qtyDropped"] += $mItem["qtyDestroyed"]; |
||
249 | $mItem["qtyDestroyed"] = 0; |
||
250 | $itemList[$key]["price"] += $mItem["price"] * ($mItem["qtyDropped"] + $mItem["qtyDestroyed"]); |
||
251 | } |
||
252 | return $itemList; |
||
253 | } |
||
254 | |||
255 | public static function buildItemKey($itm) |
||
256 | { |
||
257 | $key = $itm["typeName"] . ($itm["singleton"] == 2 ? " (Copy)" : ""); |
||
258 | $key .= "|" . ($itm["qtyDropped"] > 0 ? "dropped" : "destroyed"); |
||
259 | if (!isset($itm["flagName"])) $itm["flagName"] = Info::getFlagName($itm["flag"]); |
||
260 | $key .= "|" . $itm["flagName"]; |
||
261 | if (in_array($itm["groupID"], array(340, 649)) && isset($itm["items"])) $key .= microtime() . rand(0, 10000); |
||
262 | return $key; |
||
263 | } |
||
264 | |||
265 | public static function involvedCorpsAndAllis($md5, $involved) |
||
266 | { |
||
267 | $Cache = Cache::get($md5."involvedCorpsAndAllis"); |
||
268 | if($Cache) return $Cache; |
||
269 | |||
270 | $involvedAlliCount = 0; |
||
271 | $involvedCorpCount = 0; |
||
272 | // Create the involved corps / alliances list |
||
273 | $invAll = array(); |
||
274 | foreach($involved as $inv) { |
||
275 | $allianceID = $inv["allianceID"]; |
||
276 | $corporationID = $inv["corporationID"]; |
||
277 | if (!isset($invAll["$allianceID"])) { |
||
278 | $involvedAlliCount++; |
||
279 | $invAll["$allianceID"] = array(); |
||
280 | if ($allianceID != 0) $invAll["$allianceID"]["allianceName"] = $inv["allianceName"]; |
||
281 | if ($allianceID != 0) $invAll["$allianceID"]["name"] = $inv["allianceName"]; |
||
282 | if ($allianceID != 0) $invAll["$allianceID"]["allianceID"] = $allianceID; |
||
283 | $invAll["$allianceID"]["corporations"] = array(); |
||
284 | $invAll["$allianceID"]["involved"] = 0; |
||
285 | } |
||
286 | $involvedCount = $invAll["$allianceID"]["involved"]; |
||
287 | $invAll["$allianceID"]["involved"] = $involvedCount + 1; |
||
288 | |||
289 | if (!isset($invAll["$allianceID"]["corporations"]["$corporationID"])) { |
||
290 | $involvedCorpCount++; |
||
291 | $invAll["$allianceID"]["corporations"]["$corporationID"] = array(); |
||
292 | $invAll["$allianceID"]["corporations"]["$corporationID"]["corporationName"] = isset($inv["corporationName"]) ? $inv["corporationName"] : ""; |
||
293 | $invAll["$allianceID"]["corporations"]["$corporationID"]["name"] = isset($inv["corporationName"]) ? $inv["corporationName"] : ""; |
||
294 | $invAll["$allianceID"]["corporations"]["$corporationID"]["corporationID"] = $corporationID; |
||
295 | $invAll["$allianceID"]["corporations"]["$corporationID"]["involved"] = 0; |
||
296 | } |
||
297 | $involvedCount = $invAll["$allianceID"]["corporations"]["$corporationID"]["involved"]; |
||
298 | $invAll["$allianceID"]["corporations"]["$corporationID"]["involved"] = $involvedCount + 1; |
||
299 | } |
||
300 | uasort($invAll, "involvedSort"); |
||
301 | foreach($invAll as $id=>$alliance) { |
||
302 | $corps = $alliance["corporations"]; |
||
303 | uasort($corps, "involvedSort"); |
||
304 | $invAll["$id"]["corporations"] = $corps; |
||
305 | } |
||
306 | if ($involvedCorpCount <= 1 && $involvedAlliCount <= 1) $invAll = array(); |
||
307 | Cache::set($md5."involvedCorpsAndAllis", $invAll); |
||
308 | return $invAll; |
||
309 | } |
||
310 | |||
311 | public static function involvedSort($field1, $field2) |
||
316 | |||
317 | public static function droppedIsk($md5, $items) |
||
330 | |||
331 | public static function fittedIsk($md5, $items) |
||
332 | { |
||
333 | $key = $md5 . "fittedIsk"; |
||
334 | $cache = Cache::get($key); |
||
335 | if($cache) |
||
336 | return $cache; |
||
351 | } |
||
352 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.