Related   B
last analyzed

Complexity

Total Complexity 51

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 51
lcom 1
cbo 4
dl 0
loc 255
rs 8.3206

13 Methods

Rating   Name   Duplication   Size   Complexity  
A addMoreInvolved() 0 14 4
A getKills() 0 13 4
A getKillIDs() 0 13 4
A findWinners() 0 13 4
A addInvolvedEntity() 0 13 4
A assignSides() 0 10 4
B buildSummary() 0 60 6
A addAllInvolved() 0 13 2
B addInvolved() 0 8 5
C getInvolved() 0 27 7
B getStatsKillList() 0 32 3
A addInfo() 0 13 3
A compareShips() 0 6 1

How to fix   Complexity   

Complex Class

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
2
/* zKillboard
3
 * Copyright (C) 2012-2015 EVE-KILL Team and EVSCO.
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 * Parser for raw killmails from ingame EVE.
21
 */
22
23
class Related
24
{
25
	private static $killstorage = array();
26
27
	public static function buildSummary(&$kills, $parameters, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
140
	{
141
		foreach($kills as $kill)
142
		{
143
			$victim = $kill["victim"];
144
			Info::addInfo($victim);
145
			if ($victim["characterID"] > 0 && $victim["groupID"] != 29)
146
			{
147
				$key = $victim["characterID"] . ":" . $victim["corporationID"] . ":" . $victim["allianceID"] . ":" . $victim["shipTypeID"];
148
				$victim["destroyed"] = $victim["killID"];
149
				$team[$key] = $victim;
150
			}
151
		}
152
	}
153
154
	private static function getKills(&$kills, $team)
155
	{
156
		$teamsKills = array();
157
		foreach($kills as $killID=>$kill)
158
		{
159
			$victim = $kill["victim"];
160
			$add = in_array($victim["allianceID"], $team) || in_array($victim["corporationID"], $team);
161
162
			if ($add)
163
				$teamsKills[$killID] = $kill;
164
		}
165
		return $teamsKills;
166
	}
167
168
	private static function getKillIDs(&$kills, $team)
169
	{
170
		$killIDs = array();
171
		foreach($kills as $killID => $kill)
172
		{
173
			$victim = $kill["victim"];
174
			$add = in_array($victim["allianceID"], $team) || in_array($victim["corporationID"], $team);
175
176
			if($add)
177
				$killIDs[] = $killID;
178
		}
179
	return $killIDs;
180
	}
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)
0 ignored issues
show
Unused Code introduced by
The parameter $typeColumn is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
233
	{
234
		$involvedArray = array();
235
		foreach ($kills as $killID=>$kill) {
236
			$finalBlow = $kill["finalBlow"];
237
			$added = self::addInvolvedEntity($involvedArray, $killID, $finalBlow["allianceID"]);
238
			if (!$added)
239
				$added = self::addInvolvedEntity($involvedArray, $killID, $finalBlow["corporationID"]);
240
			if (!$added)
241
				$added = self::addInvolvedEntity($involvedArray, $killID, $finalBlow["characterID"]);
0 ignored issues
show
Unused Code introduced by
$added is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
242
		}
243
		return array_keys($involvedArray);
244
	}
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)
254
	{
255
		if ($entity == 0)
256
			return false;
257
		if (!isset($involvedArray["$entity"]))
258
			$involvedArray["$entity"] = array();
259
		if (!in_array($killID, $involvedArray["$entity"]))
260
		{
261
			$involvedArray["$entity"][] = $killID;
262
			return true;
263
		}
264
		return false;
265
	}
266
267
	private static function assignSides($assignees, &$teamA, &$teamB)
268
	{
269
		foreach($assignees as $id)
270
		{
271
			if (!isset($teamA[$id]))
272
				$teamA[] = $id;
273
			if (($key = array_search($id, $teamB)) !== false)
274
				unset($teamB[$key]);
275
		}
276
	}
277
}
278