Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
11 | class AlertCombatEndpointController extends AlertEndpointController |
||
12 | { |
||
13 | public function __construct( |
||
14 | classRepository $classRepository, |
||
15 | combatRepository $combatRepository |
||
16 | ) { |
||
17 | $this->classRepository = $classRepository; |
||
|
|||
18 | $this->combatRepository = $combatRepository; |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * Retrieves combat totals on a global and per-server basis |
||
23 | * @param ServerRequestInterface $request |
||
24 | * @param ResponseInterface $response |
||
25 | * @return ResponseInterface |
||
26 | */ |
||
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 | |||
120 | public function getClassTotals(ServerRequestInterface $request, ResponseInterface $response) |
||
252 | |||
253 | private function findClassGrouping($classID) |
||
267 | |||
268 | private function findClassFaction($classID) |
||
282 | } |
||
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.