Conditions | 13 |
Paths | 72 |
Total Lines | 89 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
34 | public function playerLeaderboards(OutputInterface $output) |
||
35 | { |
||
36 | $metrics = [ |
||
37 | 'playerKills', |
||
38 | 'playerDeaths', |
||
39 | 'playerTeamkills', |
||
40 | 'playerSuicides', |
||
41 | 'headshots' |
||
42 | ]; |
||
43 | $servers = [0, 1, 10, 13, 17, 25, 1000, 2000]; |
||
44 | |||
45 | foreach ($servers as $server) { |
||
46 | foreach ($metrics as $metric) { |
||
47 | $this->markAsBeingUpdated($metric, $server); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | foreach ($servers as $server) { |
||
52 | foreach ($metrics as $metric) { |
||
53 | $count = 0; |
||
54 | $limit = 10000; |
||
55 | $ladderLimit = 10000; |
||
56 | $pos = 1; |
||
57 | |||
58 | $output->writeln("Running metric: {$metric} for server {$server}"); |
||
59 | |||
60 | $list = "ps2alerts:api:leaderboards:players:{$metric}:list-{$server}"; |
||
61 | |||
62 | // Delete the list for reprocessing |
||
63 | if ($this->redis->exists($list)) { |
||
64 | $this->redis->del($list); |
||
65 | } |
||
66 | |||
67 | // Continue with loop until we don't have a count % modulus returning from the query |
||
68 | while ($count < $ladderLimit && $count % $limit === 0 || $count === 0) { |
||
69 | $per = ($count / $ladderLimit) * 100; |
||
70 | $output->writeln("========= {$count} / {$ladderLimit} ({$per}%) ========="); |
||
71 | |||
72 | $query = $this->auraFactory->newSelect(); |
||
73 | $query->cols(['*']); |
||
74 | $query->from('ws_players_total'); |
||
75 | |||
76 | if ($server !== 0) { |
||
77 | $query->where('playerServer', $server); |
||
78 | } |
||
79 | |||
80 | $query->orderBy([$metric . ' DESC']); |
||
81 | $query->limit($limit); |
||
82 | $query->offset($count); |
||
83 | |||
84 | $statement = $this->db->prepare($query->getStatement()); |
||
85 | $statement->execute($query->getBindValues()); |
||
86 | |||
87 | $count = $count + $statement->rowCount(); |
||
88 | |||
89 | while ($player = $statement->fetch(\PDO::FETCH_OBJ)) { |
||
90 | $playerPosKey = "ps2alerts:api:leaderboards:players:pos:{$player->playerID}"; |
||
91 | |||
92 | // If player record doesn't exist |
||
93 | if (!$this->redis->exists($playerPosKey)) { |
||
94 | $data = [ |
||
95 | 'id' => $player->playerID, |
||
96 | 'updated' => date('U', strtotime('now')) |
||
97 | ]; |
||
98 | } else { |
||
99 | $data = json_decode($this->redis->get($playerPosKey), true); |
||
100 | } |
||
101 | |||
102 | // For first time running |
||
103 | if (!empty($data[$server][$metric])) { |
||
104 | $data[$server][$metric]['old'] = $data[$server][$metric]['new']; |
||
105 | } else { |
||
106 | $data[$server][$metric]['old'] = 0; |
||
107 | } |
||
108 | |||
109 | $data[$server][$metric]['new'] = $pos; |
||
110 | $data['updated'] = date('U', strtotime('now')); |
||
111 | |||
112 | $this->redis->set($playerPosKey, json_encode($data)); |
||
113 | $this->redis->rpush($list, $player->playerID); |
||
114 | |||
115 | $pos++; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | $this->markAsComplete($metric, $server); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
162 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.