Conditions | 11 |
Paths | 9 |
Total Lines | 92 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |
||
27 | public function getCombatTotals(ServerRequestInterface $request, ResponseInterface $response) |
||
28 | { |
||
29 | try { |
||
30 | $servers = $this->validateQueryStringArguments($_GET['servers'], 'servers'); |
||
31 | $zones = $this->validateQueryStringArguments($_GET['zones'], 'zones'); |
||
32 | } catch (InvalidArgumentException $e) { |
||
33 | return $this->respondWithError($e->getMessage(), self::CODE_WRONG_ARGS); |
||
34 | } |
||
35 | |||
36 | $serversExploded = explode(',', $servers); |
||
37 | $zonesExploded = explode(',', $zones); |
||
38 | $zonesIn = $this->combatRepository->generateWhereInString($zonesExploded); |
||
39 | |||
40 | $results = []; |
||
41 | |||
42 | foreach ($serversExploded as $server) { |
||
43 | $metrics = ['kills', 'deaths', 'teamkills', 'suicides', 'headshots']; |
||
44 | $factions = ['vs', 'nc', 'tr']; |
||
45 | |||
46 | // Check if we have an entry in Redis |
||
47 | $data = $this->getRedisUtility()->checkRedis('api', 'combatTotals', "{$server}-data"); |
||
48 | $dataArchive = $this->getRedisUtility()->checkRedis('api', 'combatTotals', "{$server}-dataArchive"); |
||
49 | |||
50 | $mergedArray = []; |
||
51 | |||
52 | if (! $data || ! $dataArchive) { |
||
53 | $sums = []; |
||
54 | foreach ($metrics as $metric) { |
||
55 | foreach ($factions as $faction) { |
||
56 | $dbMetric = $metric . strtoupper($faction); // e.g. killsVS |
||
57 | $dataMetric = $metric . strtoupper($faction); // e.g. killsVS |
||
58 | |||
59 | // Handle teamkills inconsistency |
||
60 | if ($metric === 'teamkills') { |
||
61 | $dbMetric = 'teamKills' . strtoupper($faction); |
||
62 | } |
||
63 | $sums[] = "SUM(factions.{$dbMetric}) AS $dataMetric"; |
||
64 | } |
||
65 | |||
66 | // Totals |
||
67 | $dbMetric = 'total' . ucfirst($metric); // e.g. killsVS |
||
68 | $dataMetric = 'total' . ucfirst($metric); // e.g. killsVS |
||
69 | |||
70 | // Handle teamkills inconsistency |
||
71 | if ($metric === 'teamkills') { |
||
72 | $dbMetric = 'totalTKs'; // Christ knows why |
||
73 | } |
||
74 | $sums[] = "SUM(factions.{$dbMetric}) AS $dataMetric"; |
||
75 | } |
||
76 | |||
77 | $query = $this->combatRepository->newQuery('single', true); |
||
78 | $query->cols($sums); |
||
79 | $query->from('ws_factions AS factions'); |
||
80 | $query->join( |
||
81 | 'INNER', |
||
82 | 'ws_results AS results', |
||
83 | "factions.resultID = results.ResultID" |
||
84 | ); |
||
85 | $query->where('results.ResultServer = ?', $server); |
||
86 | $query->where("results.ResultAlertCont IN {$zonesIn}"); |
||
87 | $query->where('results.Valid = ?', 1); |
||
88 | |||
89 | $data = $this->combatRepository->fireStatementAndReturn($query, true); |
||
90 | $dataArchive = $this->combatRepository->fireStatementAndReturn($query, true, false, true); |
||
91 | |||
92 | // Store the results in Redis |
||
93 | $this->getRedisUtility()->storeInRedis('api', 'combatTotals', "{$server}-data", $data, 3600); |
||
94 | $this->getRedisUtility()->storeInRedis('api', 'combatTotals', "{$server}-dataArchive", $dataArchive, 3600); |
||
95 | } |
||
96 | |||
97 | $metrics = ['kills', 'deaths', 'teamkills', 'suicides', 'headshots']; |
||
98 | $factions = ['vs', 'nc', 'tr']; |
||
99 | |||
100 | // Merge the two arrays together |
||
101 | foreach ($metrics as $metric) { |
||
102 | // Tot up totals |
||
103 | $dbMetric = 'total' . ucfirst($metric); |
||
104 | $mergedArray['totals'][$metric] = (int) $data[$dbMetric] + (int) $dataArchive[$dbMetric]; |
||
105 | $results['all']['totals'][$metric] += $mergedArray['totals'][$metric]; |
||
106 | |||
107 | foreach ($factions as $faction) { |
||
108 | $dbMetric = $metric . strtoupper($faction); |
||
109 | $mergedArray[$metric][$faction] = (int) $data[$dbMetric] + (int) $dataArchive[$dbMetric]; |
||
110 | $results['all'][$metric][$faction] += $mergedArray[$metric][$faction]; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | $results[$server] = $mergedArray; |
||
115 | } |
||
116 | |||
117 | return $this->respondWithData($results); |
||
118 | } |
||
119 | |||
283 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.