Complex classes like Summary 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 Summary, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Summary |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param array $data |
||
| 25 | * @param integer $id |
||
| 26 | * @param array $parameters |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public static function getPilotSummary(&$data, $id, $parameters = array()) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param string $type |
||
| 36 | * @param string $column |
||
| 37 | * @param array $data |
||
| 38 | * @param integer $id |
||
| 39 | * @param array $parameters |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | public static function getSummary($type, $column, &$data, $id, $parameters = array(), $overRide = false) |
||
| 43 | { |
||
| 44 | $key = "summary:$type:$column:$id:$overRide:" . json_encode($parameters); |
||
| 45 | $mc = Cache::get($key); |
||
| 46 | if ($mc) return $mc; |
||
|
|
|||
| 47 | |||
| 48 | $rank = Db::queryRow("select * from zz_ranks where type = :type and typeID = :id", array(":type" => $type, ":id" => $id), 300); |
||
| 49 | $recentRank = Db::queryField("select overallRank from zz_ranks_recent where type = :type and typeID = :id", "overallRank", array(":type" => $type, ":id" => $id), 300); |
||
| 50 | $progress = Db::queryRow("select * from zz_ranks_progress where type = :type and typeID = :id and dttm >= date(date_sub(now(), interval 7 day)) order by dttm limit 1", array(":type" => $type, ":id" => $id), 300); |
||
| 51 | $previousRank = $nextRank = null; |
||
| 52 | if ($progress) { |
||
| 53 | $r = $progress["overallRank"]; |
||
| 54 | if ($r) { |
||
| 55 | $progress["previous"] = Db::queryRow("select * from zz_ranks_progress where type = :type and overallRank = :rank and dttm >= date(date_sub(now(), interval 7 day)) order by dttm limit 1", array(":type" => $type, ":rank" => ($r - 1)), 300); |
||
| 56 | $progress["next"] = Db::queryRow("select * from zz_ranks_progress where type = :type and overallRank = :rank and dttm >= date(date_sub(now(), interval 7 day)) order by dttm limit 1", array(":type" => $type, ":rank" => ($r + 1)), 300); |
||
| 57 | Info::addInfo($progress); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($overRide) { |
||
| 62 | $rank = $recentRank = array(); |
||
| 63 | |||
| 64 | $tables = array(); |
||
| 65 | $whereClauses = array(); |
||
| 66 | if (!isset($parameters["kills"]) && !isset($parameters["losses"])) $parameters["mixed"] = true; |
||
| 67 | Filters::buildFilters($tables, $whereClauses, $whereClauses, $parameters, true); |
||
| 68 | |||
| 69 | $whereStatement = implode(" and ", $whereClauses); |
||
| 70 | |||
| 71 | $query = "select groupID, sum(if(isVictim, 0, 1)) destroyed, sum(if(isVictim, 0, total_price)) iskDestroyed, sum(if(isVictim, 0, points)) pointsDestroyed, sum(if(isVictim, 1, 0)) lost, sum(if(isVictim, total_price, 0)) iskLost, sum(if(isVictim, points, 0)) pointsLost from (select vGroupID groupID, isVictim, total_price, points from zz_participants p where $whereStatement group by killID) as foo group by groupID"; |
||
| 72 | $stats = Db::query($query); |
||
| 73 | } else { |
||
| 74 | if ($type == "system" || $type == "region") $stats = Db::query("select groupID, lost destroyed, 0 lost, pointsLost pointsDestroyed, 0 pointsLost, iskLost iskDestroyed, 0 iskLost from zz_stats where type='$type' and typeID = $id", array(":id" => $id), 300); |
||
| 75 | else $stats = Db::query("select groupID, destroyed, lost, pointsDestroyed, pointsLost, iskDestroyed, iskLost from zz_stats where type='$type' and typeID = :id", array(":id" => $id), 0); |
||
| 76 | } |
||
| 77 | |||
| 78 | $infoStats = array(); |
||
| 79 | |||
| 80 | $data["shipsDestroyed"] = 0; |
||
| 81 | $data["shipsLost"] = 0; |
||
| 82 | $data["pointsDestroyed"] = 0; |
||
| 83 | $data["pointsLost"] = 0; |
||
| 84 | $data["iskDestroyed"] = 0; |
||
| 85 | $data["iskLost"] = 0; |
||
| 86 | |||
| 87 | foreach ($stats as $stat) { |
||
| 88 | $infoStat = Info::addInfo($stat); |
||
| 89 | if ($infoStat["groupID"] == 0) $infoStat["groupName"] = "Unknown"; |
||
| 90 | $infoStats[$infoStat["groupName"]] = $infoStat; |
||
| 91 | $data["shipsDestroyed"] += $infoStat["destroyed"]; |
||
| 92 | $data["shipsLost"] += $infoStat["lost"]; |
||
| 93 | $data["iskDestroyed"] += $infoStat["iskDestroyed"]; |
||
| 94 | $data["iskLost"] += $infoStat["iskLost"]; |
||
| 95 | $data["pointsDestroyed"] += $infoStat["pointsDestroyed"]; |
||
| 96 | $data["pointsLost"] += $infoStat["pointsLost"]; |
||
| 97 | } |
||
| 98 | unset($stats); |
||
| 99 | ksort($infoStats); |
||
| 100 | $data["stats"] = $infoStats; |
||
| 101 | if ($rank != null && $recentRank != null) $rank["recentRank"] = $recentRank; |
||
| 102 | if ($rank != null && $progress != null) $data["progress"] = $progress; |
||
| 103 | if ($rank != null) $data["ranks"] = $rank; |
||
| 104 | Cache::set($key, $data, 300); |
||
| 105 | return $data; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param array $data |
||
| 110 | * @param integer $id |
||
| 111 | * @param array $parameters |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public static function getCorpSummary(&$data, $id, $parameters = array()) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $data |
||
| 121 | * @param integer $id |
||
| 122 | * @param array $parameters |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | public static function getAlliSummary(&$data, $id, $parameters = array()) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param array $data |
||
| 132 | * @param integer $id |
||
| 133 | * @param array $parameters |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | public static function getFactionSummary(&$data, $id, $parameters = array()) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param array $data |
||
| 143 | * @param integer $id |
||
| 144 | * @param array $parameters |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public static function getShipSummary(&$data, $id, $parameters = array()) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param array $data |
||
| 154 | * @param integer $id |
||
| 155 | * @param array $parameters |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public static function getGroupSummary(&$data, $id, $parameters = array()) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param array $data |
||
| 165 | * @param integer $id |
||
| 166 | * @param array $parameters |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | public static function getRegionSummary(&$data, $id, $parameters = array()) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param array $data |
||
| 176 | * @param integer $id |
||
| 177 | * @param array $parameters |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | public static function getSystemSummary(&$data, $id, $parameters = array()) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param $type |
||
| 187 | * @param $typeID |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public static function getMonthlyHistory($type, $typeID) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param $kills |
||
| 197 | * @param array $parameters |
||
| 198 | * @param $key |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public static function buildSummary(&$kills, $parameters = array(), $key) |
||
| 202 | { |
||
| 203 | if ($kills == null || !is_array($kills) || sizeof($kills) == 0) return array(); |
||
| 204 | |||
| 205 | $sem = Semaphore::fetch($parameters["solarSystemID"]); |
||
| 206 | |||
| 207 | $key = "related:$key"; |
||
| 208 | $mc = Cache::get($key); |
||
| 209 | if ($mc) { |
||
| 210 | Semaphore::release($sem); |
||
| 211 | return $mc; |
||
| 212 | } |
||
| 213 | |||
| 214 | $teamAKills = array(); |
||
| 215 | $teamBKills = array(); |
||
| 216 | $teamAList = array(); |
||
| 217 | $teamBList = array(); |
||
| 218 | $killHash = array(); |
||
| 219 | |||
| 220 | self::determineSides($kills, $teamAKills, $teamBKills); |
||
| 221 | foreach ($kills as $kill) { |
||
| 222 | self::addRelatedPilots($killHash, $kill, $kill["victim"]); |
||
| 223 | } |
||
| 224 | //$teamAEntities = self::getEntities($teamAKills); |
||
| 225 | //$teamBEntities = self::getEntities($teamBKills); |
||
| 226 | //$killHash = array_keys($killHash); |
||
| 227 | |||
| 228 | $teamATotals = self::getStatsKillList($teamAKills); |
||
| 229 | $teamBTotals = self::getStatsKillList($teamBKills); |
||
| 230 | |||
| 231 | self::hashPilots($teamBKills, $teamBList, $teamAList); |
||
| 232 | self::hashPilots($teamAKills, $teamAList, $teamBList); |
||
| 233 | |||
| 234 | $teamATotals["pilotCount"] = self::getUniquePilotCount($teamAList); |
||
| 235 | $teamBTotals["pilotCount"] = self::getUniquePilotCount($teamBList); |
||
| 236 | |||
| 237 | foreach ($teamAList as $a => $value) if (in_array($a, array_keys($teamBList))) unset($teamAList[$a]); |
||
| 238 | foreach ($teamBList as $b => $value) if (in_array($b, array_keys($teamAList))) unset($teamBList[$b]); |
||
| 239 | |||
| 240 | uksort($teamAList, "self::shipGroupSort"); |
||
| 241 | uksort($teamBList, "self::shipGroupSort"); |
||
| 242 | |||
| 243 | $teamAList = self::addInfo($teamAList, $killHash); |
||
| 244 | $teamBList = self::addInfo($teamBList, $killHash); |
||
| 245 | |||
| 246 | |||
| 247 | $retValue = array( |
||
| 248 | "teamA" => array( |
||
| 249 | "list" => $teamAList, |
||
| 250 | "kills" => $teamAKills, |
||
| 251 | "totals" => $teamATotals, |
||
| 252 | ), |
||
| 253 | "teamB" => array( |
||
| 254 | "list" => $teamBList, |
||
| 255 | "kills" => $teamBKills, |
||
| 256 | "totals" => $teamBTotals, |
||
| 257 | ), |
||
| 258 | ); |
||
| 259 | |||
| 260 | //$teamAScore = log($retValue["teamA"]["totals"]["total_price"] + 1) * (log($retValue["teamA"]["totals"]["total_points"]) + 1); |
||
| 261 | //$teamBScore = log($retValue["teamB"]["totals"]["total_price"] + 1) * (log($retValue["teamA"]["totals"]["total_points"]) + 1); |
||
| 262 | /*if ($teamBScore > $teamAScore) { |
||
| 263 | $temp = $retValue["teamB"]; |
||
| 264 | $retValue["teamB"] = $retValue["teamA"]; |
||
| 265 | $retValue["teamA"] = $temp; |
||
| 266 | }*/ |
||
| 267 | |||
| 268 | Semaphore::release($sem); |
||
| 269 | Cache::set($key, $retValue, 900); |
||
| 270 | return $retValue; |
||
| 271 | } |
||
| 272 | |||
| 273 | /*private static function getEntities(&$kills) |
||
| 274 | { |
||
| 275 | $retValue = array(); |
||
| 276 | $retValue["chars"] = array(); |
||
| 277 | $retValue["corps"] = array(); |
||
| 278 | $retValue["allis"] = array(); |
||
| 279 | $retValue["factions"] = array(); |
||
| 280 | foreach ($kills as $kill) { |
||
| 281 | $victim = $kill["victim"]; |
||
| 282 | $retValue["chars"][$victim["characterID"]] = true; |
||
| 283 | $retValue["corps"][$victim["corporationID"]] = true; |
||
| 284 | if ($victim["allianceID"] != 0) $retValue["allis"][$victim["allianceID"]] = true; |
||
| 285 | if ($victim["factionID"] != 0) $retValue["factions"][$victim["factionID"]] = true; |
||
| 286 | } |
||
| 287 | $retValue["chars"] = array_keys($retValue["chars"]); |
||
| 288 | $retValue["corps"] = array_keys($retValue["corps"]); |
||
| 289 | $retValue["allis"] = array_keys($retValue["allis"]); |
||
| 290 | $retValue["factions"] = array_keys($retValue["factions"]); |
||
| 291 | return $retValue; |
||
| 292 | }*/ |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param $kills |
||
| 296 | * @param $teamA |
||
| 297 | * @param $teamB |
||
| 298 | */ |
||
| 299 | private static function determineSides(&$kills, &$teamA, &$teamB) |
||
| 300 | { |
||
| 301 | $chars = array(); |
||
| 302 | $corps = array(); |
||
| 303 | $allis = array(); |
||
| 304 | foreach ($kills as $kill) { |
||
| 305 | $victim = $kill["victim"]; |
||
| 306 | $price = $kill["info"]["total_price"]; |
||
| 307 | $points = $kill["info"]["points"]; |
||
| 308 | self::increment($chars, $victim["characterID"], $price, $points); |
||
| 309 | self::increment($corps, $victim["corporationID"], $price, $points); |
||
| 310 | self::increment($allis, $victim["allianceID"], $price, $points); |
||
| 311 | } |
||
| 312 | if (sizeof($allis)) self::filterKills($kills, $teamB, $teamA, "allianceID", $allis); |
||
| 313 | else if (sizeof($corps)) self::filterKills($kills, $teamB, $teamA, "corporationID", $corps); |
||
| 314 | else if (sizeof($chars)) self::filterKills($kills, $teamB, $teamA, "characterID", $chars); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param $array |
||
| 319 | * @param $key |
||
| 320 | * @param $price |
||
| 321 | * @param $points |
||
| 322 | */ |
||
| 323 | private static function increment(&$array, $key, $price, $points) |
||
| 324 | { |
||
| 325 | if ($key == 0) return; |
||
| 326 | if (!isset($array["$key"])) { |
||
| 327 | $array["$key"] = array(); |
||
| 328 | $array["$key"]["count"] = 0; |
||
| 329 | $array["$key"]["price"] = 0; |
||
| 330 | $array["$key"]["points"] = 0; |
||
| 331 | } |
||
| 332 | $array["$key"]["count"]++; |
||
| 333 | $array["$key"]["price"] += $price; |
||
| 334 | $array["$key"]["points"] += $points; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param array $kills |
||
| 339 | * @param array $array1 |
||
| 340 | * @param array $array2 |
||
| 341 | * @param string $key |
||
| 342 | * @param array $keyArray |
||
| 343 | */ |
||
| 344 | private static function filterKills(&$kills, &$array1, &$array2, $key, $keyArray) |
||
| 345 | { |
||
| 346 | $valueArray = array(); |
||
| 347 | foreach ($keyArray as $rowKey => $row) { |
||
| 348 | $valueArray[$rowKey] = log($row["count"]) * $row["price"]; |
||
| 349 | } |
||
| 350 | arsort($valueArray); |
||
| 351 | $keys = array_keys($valueArray); |
||
| 352 | $keyValue = $keys[0]; |
||
| 353 | foreach ($kills as $killID => $kill) { |
||
| 354 | $victim = $kill["victim"]; |
||
| 355 | if ($victim[$key] == $keyValue) $array1[$killID] = $kill; |
||
| 356 | else if ($victim[$key] == 0) $array1[$killID] = $kill; |
||
| 357 | else $array2[$killID] = $kill; |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param array $array |
||
| 363 | * @param array $kill |
||
| 364 | * @param array $pilot |
||
| 365 | */ |
||
| 366 | protected static function addRelatedPilots(&$array, &$kill, &$pilot) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param array $kills |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | private static function getStatsKillList(&$kills) |
||
| 390 | { |
||
| 391 | $totalPrice = 0; |
||
| 392 | $totalPoints = 0; |
||
| 393 | $groupIDs = array(); |
||
| 394 | $totalShips = 0; |
||
| 395 | foreach ($kills as $kill) { |
||
| 396 | $info = $kill["info"]; |
||
| 397 | $victim = $kill["victim"]; |
||
| 398 | $totalPrice += $info["total_price"]; |
||
| 399 | $totalPoints += $info["points"]; |
||
| 400 | $groupID = $victim["groupID"]; |
||
| 401 | if (!isset($groupIDs[$groupID])) { |
||
| 402 | $groupIDs[$groupID] = array(); |
||
| 403 | $groupIDs[$groupID]["count"] = 0; |
||
| 404 | $groupIDs[$groupID]["isk"] = 0; |
||
| 405 | $groupIDs[$groupID]["points"] = 0; |
||
| 406 | } |
||
| 407 | $groupIDs[$groupID]["groupID"] = $groupID; |
||
| 408 | $groupIDs[$groupID]["count"]++; |
||
| 409 | $groupIDs[$groupID]["isk"] += $info["total_price"]; |
||
| 410 | $groupIDs[$groupID]["points"] += $info["points"]; |
||
| 411 | $totalShips++; |
||
| 412 | } |
||
| 413 | Info::addInfo($groupIDs); |
||
| 414 | return array( |
||
| 415 | "total_price" => $totalPrice, "groupIDs" => $groupIDs, "totalShips" => $totalShips, |
||
| 416 | "total_points" => $totalPoints |
||
| 417 | ); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param array $kills |
||
| 422 | * @param array $teamBs |
||
| 423 | * @param array $teamAs |
||
| 424 | */ |
||
| 425 | protected static function hashPilots($kills, &$teamBs, &$teamAs) |
||
| 426 | { |
||
| 427 | $pilotsAdded = array(); |
||
| 428 | ksort($kills); |
||
| 429 | |||
| 430 | $validEntities = array(); |
||
| 431 | $validEntities["factions"] = array(); |
||
| 432 | $validEntities["allis"] = array(); |
||
| 433 | $validEntities["corps"] = array(); |
||
| 434 | // Preprocess the opposition |
||
| 435 | foreach ($kills as $killID => $kill) { |
||
| 436 | $victim = $kill['victim']; |
||
| 437 | |||
| 438 | if ($victim["factionID"] != 0) $validEntities["factions"][] = $victim["factionID"]; |
||
| 439 | if ($victim["allianceID"] != 0) $validEntities["allis"][] = $victim["allianceID"]; |
||
| 440 | if ($victim["corporationID"] != 0) $validEntities["corps"][] = $victim["corporationID"]; |
||
| 441 | } |
||
| 442 | |||
| 443 | foreach ($kills as $killID => $kill) { |
||
| 444 | $victim = $kill['victim']; |
||
| 445 | $characterID = $victim["characterID"]; |
||
| 446 | $shipTypeID = $victim["shipTypeID"]; |
||
| 447 | if (($shipTypeID == 0 || $shipTypeID == 670) && in_array($characterID, $pilotsAdded)) continue; |
||
| 448 | if ($shipTypeID != 670 && $shipTypeID != 0) $pilotsAdded[] = $characterID; |
||
| 449 | self::addRelatedPilots($teamBs, $kill, $victim); |
||
| 450 | |||
| 451 | $attackers = Db::query("select * from zz_participants where killID = :killID and isVictim = '0'", |
||
| 452 | array(":killID" => $killID), 3600); |
||
| 453 | foreach ($attackers as $attacker) { |
||
| 454 | $shipTypeID = $attacker["shipTypeID"]; |
||
| 455 | |||
| 456 | if ($attacker["factionID"] != 0 && in_array($attacker["factionID"], $validEntities["factions"])) continue; |
||
| 457 | if ($attacker["allianceID"] != 0 && in_array($attacker["allianceID"], $validEntities["allis"])) continue; |
||
| 458 | if (in_array($attacker["corporationID"], $validEntities["corps"])) continue; |
||
| 459 | |||
| 460 | if ($shipTypeID != 0 && $shipTypeID != 670) self::addRelatedPilots($teamAs, $kill, $attacker); |
||
| 461 | if (!in_array($attacker["characterID"], $pilotsAdded)) $pilotsAdded[] = $attacker["characterID"]; |
||
| 462 | } |
||
| 463 | foreach ($attackers as $attacker) { |
||
| 464 | //if (!in_array($attacker["characterID"], $pilotsAdded)) self::addRelatedPilots($teamAs, $kill, $attacker); |
||
| 465 | } |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param array $array |
||
| 471 | * @return int |
||
| 472 | */ |
||
| 473 | private static function getUniquePilotCount($array) |
||
| 474 | { |
||
| 475 | $uniquePilots = array(); |
||
| 476 | foreach ($array as $hash => $kill) { |
||
| 477 | $split = explode("|", $hash); |
||
| 478 | $characterID = $split[2]; |
||
| 479 | if ($characterID > 0) $uniquePilots["$characterID"] = true; |
||
| 480 | } |
||
| 481 | return sizeof($uniquePilots); |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param array $array |
||
| 486 | * @param array $killHash |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | private static function addInfo($array, $killHash) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @param array $a |
||
| 510 | * @param array $b |
||
| 511 | * @return int|null|string |
||
| 512 | */ |
||
| 513 | public static function shipGroupSort($a, $b) |
||
| 514 | { |
||
| 515 | $split1 = explode("|", $a); |
||
| 544 | } |
||
| 545 |
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.