Conditions | 8 |
Paths | 6 |
Total Lines | 111 |
Code Lines | 46 |
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 |
||
10 | public function up() |
||
11 | { |
||
12 | $pageCountQuery = " |
||
13 | SELECT |
||
14 | COUNT(*) |
||
15 | FROM |
||
16 | matches |
||
17 | WHERE |
||
18 | team_a_players IS NOT NULL AND team_a_players != '' AND |
||
19 | team_b_players IS NOT NULL AND team_b_players != '' AND |
||
20 | match_type = 'official' AND status = 'entered' |
||
21 | "; |
||
22 | $matchesQuery = " |
||
23 | SELECT |
||
24 | * |
||
25 | FROM |
||
26 | matches |
||
27 | WHERE |
||
28 | team_a_players IS NOT NULL AND team_a_players != '' AND |
||
29 | team_b_players IS NOT NULL AND team_b_players != '' AND |
||
30 | match_type = 'official' AND status = 'entered' AND |
||
31 | id > {id} |
||
32 | ORDER BY |
||
33 | `timestamp` |
||
34 | LIMIT 1000 |
||
35 | "; |
||
36 | |||
37 | $pageCount = ceil($this->fetchRow($pageCountQuery)[0] / 1000); |
||
38 | $lastSeason = []; |
||
39 | $lastID = 0; |
||
40 | |||
41 | for ($i = 1; $i <= $pageCount; $i++) { |
||
42 | $matches = $this->fetchAll(strtr($matchesQuery, [ |
||
43 | '{id}' => $lastID, |
||
44 | ])); |
||
45 | |||
46 | foreach ($matches as $match) { |
||
47 | $timestamp = new \DateTime($match['timestamp']); |
||
48 | $seasonInfo = [ |
||
49 | 'season' => $this->literalSeasonFromMonth($timestamp->format('n')), |
||
50 | 'year' => (int)$timestamp->format('Y'), |
||
51 | ]; |
||
52 | |||
53 | // Clear the model cache every season since Elos are cached internally |
||
54 | if ($seasonInfo != $lastSeason) { |
||
55 | $lastSeason = $seasonInfo; |
||
56 | } |
||
57 | |||
58 | $teamA = explode(',', $match['team_a_players']); |
||
59 | $teamB = explode(',', $match['team_b_players']); |
||
60 | |||
61 | if (empty($teamA) || empty($teamB)) { |
||
62 | continue; |
||
63 | } |
||
64 | |||
65 | $getElo = function($n) use ($seasonInfo) { |
||
66 | $query = " |
||
67 | SELECT |
||
68 | elo_new |
||
69 | FROM |
||
70 | player_elo |
||
71 | WHERE |
||
72 | user_id = $n AND |
||
73 | season_period = '{$seasonInfo['season']}' AND |
||
74 | season_year = {$seasonInfo['year']} |
||
75 | ORDER BY |
||
76 | match_id DESC |
||
77 | LIMIT 1 |
||
78 | "; |
||
79 | |||
80 | $result = $this->fetchRow($query); |
||
81 | |||
82 | if (empty($result)) { |
||
83 | return 1200; |
||
84 | } |
||
85 | |||
86 | return $result[0]; |
||
87 | }; |
||
88 | |||
89 | $teamA_Avg = array_sum(array_map($getElo, $teamA)) / count($teamA); |
||
90 | $teamB_Avg = array_sum(array_map($getElo, $teamB)) / count($teamB); |
||
91 | |||
92 | $diff = self::calculateEloDiff( |
||
93 | $teamA_Avg, $teamB_Avg, $match['team_a_points'], $match['team_b_points'], $match['duration'] |
||
94 | ); |
||
95 | |||
96 | // We need to disable transactions so our Player::adjustElo() fxn won't hold up execution |
||
97 | $this->getAdapter()->commitTransaction(); |
||
98 | |||
99 | $this->query("UPDATE matches SET player_elo_diff = {$diff} WHERE id = {$match['id']} LIMIT 1;"); |
||
100 | |||
101 | $walkFxn = function ($v, $k, $positive) use ($diff, $match, $seasonInfo, $getElo) { |
||
102 | $eloDiff = ($positive) ? $diff : -$diff; |
||
103 | $prevElo = $getElo($v); |
||
104 | $newElo = $prevElo + $eloDiff; |
||
105 | $query = " |
||
106 | INSERT INTO player_elo VALUES ($v, {$match['id']}, '{$seasonInfo['season']}', {$seasonInfo['year']}, $prevElo, $newElo) |
||
107 | "; |
||
108 | |||
109 | $this->execute($query); |
||
110 | }; |
||
111 | |||
112 | array_walk($teamA, $walkFxn, true); |
||
113 | array_walk($teamB, $walkFxn, false); |
||
114 | |||
115 | $this->getAdapter()->beginTransaction(); |
||
116 | |||
117 | $lastSeason = $match['id']; |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | |||
171 |
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.