Complex classes like Related 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 Related, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Related |
||
| 24 | { |
||
| 25 | private static $killstorage = array(); |
||
| 26 | |||
| 27 | public static function buildSummary(&$kills, $parameters, $options) |
||
|
|
|||
| 28 | { |
||
| 29 | $involvedEntities = array(); |
||
| 30 | foreach($kills as $killID => $kill) |
||
| 31 | self::addAllInvolved($involvedEntities, $killID); |
||
| 32 | |||
| 33 | $blueTeam = array(); |
||
| 34 | $redTeam = self::findWinners($kills, "allianceID"); |
||
| 35 | foreach($involvedEntities as $entity=>$chars) |
||
| 36 | if (!in_array($entity, $redTeam)) |
||
| 37 | $blueTeam[] = $entity; |
||
| 38 | |||
| 39 | if (isset($options["A"])) |
||
| 40 | self::assignSides($options["A"], $redTeam, $blueTeam); |
||
| 41 | if (isset($options["B"])) |
||
| 42 | self::assignSides($options["B"], $blueTeam, $redTeam); |
||
| 43 | |||
| 44 | $redInvolved = self::getInvolved($kills, $redTeam); |
||
| 45 | $blueInvolved = self::getInvolved($kills, $blueTeam); |
||
| 46 | |||
| 47 | $redKills = self::getKills($kills, $redTeam); |
||
| 48 | $blueKills = self::getKills($kills, $blueTeam); |
||
| 49 | |||
| 50 | $redKillIDs = self::getKillIDs($kills, $redTeam); |
||
| 51 | $blueKillIDs = self::getKillIDs($kills, $blueTeam); |
||
| 52 | |||
| 53 | self::addMoreInvolved($redInvolved, $redKills); |
||
| 54 | self::addMoreInvolved($blueInvolved, $blueKills); |
||
| 55 | |||
| 56 | $redTotals = self::getStatsKillList(array_keys($redKills)); |
||
| 57 | $redTotals["pilotCount"] = count($redInvolved); |
||
| 58 | $blueTotals = self::getStatsKillList(array_keys($blueKills)); |
||
| 59 | $blueTotals["pilotCount"] = count($blueInvolved); |
||
| 60 | |||
| 61 | $red = self::addInfo($redTeam); |
||
| 62 | asort($red); |
||
| 63 | $blue = self::addInfo($blueTeam); |
||
| 64 | asort($blue); |
||
| 65 | |||
| 66 | usort($redInvolved, "Related::compareShips"); |
||
| 67 | usort($blueInvolved, "Related::compareShips"); |
||
| 68 | |||
| 69 | $retValue = array( |
||
| 70 | "teamA" => array( |
||
| 71 | "list" => $redInvolved, |
||
| 72 | "kills" => $redKills, |
||
| 73 | "totals" => $redTotals, |
||
| 74 | "entities" => $red, |
||
| 75 | "killIDs" => $redKillIDs |
||
| 76 | ), |
||
| 77 | "teamB" => array( |
||
| 78 | "list" => $blueInvolved, |
||
| 79 | "kills" => $blueKills, |
||
| 80 | "totals" => $blueTotals, |
||
| 81 | "entities" => $blue, |
||
| 82 | "killIDs" => $blueKillIDs |
||
| 83 | ), |
||
| 84 | ); |
||
| 85 | return $retValue; |
||
| 86 | } |
||
| 87 | |||
| 88 | private static function addAllInvolved(&$entities, $killID) |
||
| 89 | { |
||
| 90 | $killjson = Killmail::get($killID); |
||
| 91 | $kill = json_decode($killjson, true); |
||
| 92 | |||
| 93 | self::$killstorage[$killID] = $kill; |
||
| 94 | |||
| 95 | $victim = $kill["victim"]; |
||
| 96 | self::addInvolved($entities, $victim); |
||
| 97 | $involved = $kill["attackers"]; |
||
| 98 | foreach($involved as $entry) |
||
| 99 | self::addInvolved($entities, $entry); |
||
| 100 | } |
||
| 101 | |||
| 102 | private static function addInvolved(&$entities, &$entry) |
||
| 103 | { |
||
| 104 | $entity = isset($entry["allianceID"]) && $entry["allianceID"] != 0 ? $entry["allianceID"] : $entry["corporationID"]; |
||
| 105 | if (!isset($entities["$entity"])) |
||
| 106 | $entities["$entity"] = array(); |
||
| 107 | if (!in_array($entry["characterID"], $entities["$entity"])) |
||
| 108 | $entities["$entity"][] = $entry["characterID"]; |
||
| 109 | } |
||
| 110 | |||
| 111 | private static function getInvolved(&$kills, $team) |
||
| 112 | { |
||
| 113 | $involved = array(); |
||
| 114 | foreach($kills as $kill) |
||
| 115 | { |
||
| 116 | $kill = self::$killstorage[$kill["victim"]["killID"]]; |
||
| 117 | |||
| 118 | $attackers = $kill["attackers"]; |
||
| 119 | foreach($attackers as $entry) |
||
| 120 | { |
||
| 121 | $add = false; |
||
| 122 | if (in_array($entry["allianceID"], $team)) |
||
| 123 | $add = true; |
||
| 124 | if (in_array($entry["corporationID"], $team)) |
||
| 125 | $add = true; |
||
| 126 | |||
| 127 | if ($add) |
||
| 128 | { |
||
| 129 | $key = $entry["characterID"] . ":" . $entry["corporationID"] . ":" . $entry["allianceID"] . ":" . $entry["shipTypeID"]; |
||
| 130 | $entry["shipName"] = Info::getItemName($entry["shipTypeID"]); |
||
| 131 | if (!in_array($key, $involved)) |
||
| 132 | $involved[$key] = $entry; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | return $involved; |
||
| 137 | } |
||
| 138 | |||
| 139 | private static function addMoreInvolved(&$team, $kills) |
||
| 153 | |||
| 154 | private static function getKills(&$kills, $team) |
||
| 167 | |||
| 168 | private static function getKillIDs(&$kills, $team) |
||
| 181 | |||
| 182 | private static function getStatsKillList($killIDs) |
||
| 183 | { |
||
| 184 | $totalPrice = 0; |
||
| 185 | $totalPoints = 0; |
||
| 186 | $groupIDs = array(); |
||
| 187 | $totalShips = 0; |
||
| 188 | foreach ($killIDs as $killID) { |
||
| 189 | $kill = Kills::getKillDetails($killID); |
||
| 190 | $info = $kill["info"]; |
||
| 191 | $victim = $kill["victim"]; |
||
| 192 | $totalPrice += $info["total_price"]; |
||
| 193 | $totalPoints += $info["points"]; |
||
| 194 | $groupID = $victim["groupID"]; |
||
| 195 | if (!isset($groupIDs[$groupID])) |
||
| 196 | { |
||
| 197 | $groupIDs[$groupID] = array(); |
||
| 198 | $groupIDs[$groupID]["count"] = 0; |
||
| 199 | $groupIDs[$groupID]["isk"] = 0; |
||
| 200 | $groupIDs[$groupID]["points"] = 0; |
||
| 201 | } |
||
| 202 | $groupIDs[$groupID]["groupID"] = $groupID; |
||
| 203 | $groupIDs[$groupID]["count"]++; |
||
| 204 | $groupIDs[$groupID]["isk"] += $info["total_price"]; |
||
| 205 | $groupIDs[$groupID]["points"] += $info["points"]; |
||
| 206 | $totalShips++; |
||
| 207 | } |
||
| 208 | Info::addInfo($groupIDs); |
||
| 209 | return array( |
||
| 210 | "total_price" => $totalPrice, "groupIDs" => $groupIDs, "totalShips" => $totalShips, |
||
| 211 | "total_points" => $totalPoints |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | |||
| 215 | private static function addInfo(&$team) |
||
| 216 | { |
||
| 217 | $retValue = array(); |
||
| 218 | foreach($team as $entity) |
||
| 219 | { |
||
| 220 | $alliName = Info::getAlliName($entity); |
||
| 221 | if ($alliName) |
||
| 222 | $retValue[$entity] = $alliName; |
||
| 223 | else |
||
| 224 | $retValue[$entity] = Info::getCorpName($entity); |
||
| 225 | } |
||
| 226 | return $retValue; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $typeColumn |
||
| 231 | */ |
||
| 232 | private static function findWinners(&$kills, $typeColumn) |
||
| 245 | |||
| 246 | public static function compareShips($a, $b) |
||
| 247 | { |
||
| 248 | $aSize = Db::queryField("select mass from ccp_invTypes where typeID = :typeID", "mass", array(":typeID" => $a["shipTypeID"])); |
||
| 249 | $bSize = Db::queryField("select mass from ccp_invTypes where typeID = :typeID", "mass", array(":typeID" => $b["shipTypeID"])); |
||
| 250 | return $aSize < $bSize; |
||
| 251 | } |
||
| 252 | |||
| 253 | private static function addInvolvedEntity(&$involvedArray, &$killID, &$entity) |
||
| 266 | |||
| 267 | private static function assignSides($assignees, &$teamA, &$teamB) |
||
| 277 | } |
||
| 278 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.