| Conditions | 9 |
| Paths | 4 |
| Total Lines | 94 |
| Code Lines | 65 |
| 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 |
||
| 12 | public function up() |
||
| 13 | { |
||
| 14 | $playerParticipationTable = $this->table('match_participation', [ |
||
| 15 | 'id' => false, |
||
| 16 | 'primary_key' => ['match_id', 'user_id'] |
||
| 17 | ]); |
||
| 18 | $playerParticipationTable |
||
| 19 | ->addColumn('match_id', 'integer', [ |
||
| 20 | 'signed' => false, |
||
| 21 | 'limit' => 10, |
||
| 22 | 'null' => false, |
||
| 23 | 'comment' => 'The ID of the match this player participated in', |
||
| 24 | ]) |
||
| 25 | ->addColumn('user_id', 'integer', [ |
||
| 26 | 'signed' => false, |
||
| 27 | 'limit' => 10, |
||
| 28 | 'null' => false, |
||
| 29 | 'comment' => 'The ID of the player who participated in this match', |
||
| 30 | ]) |
||
| 31 | ->addColumn('team_id', 'integer', [ |
||
| 32 | 'signed' => false, |
||
| 33 | 'limit' => 10, |
||
| 34 | 'null' => true, |
||
| 35 | 'comment' => 'The ID of the team this player played for at the time of this match' |
||
| 36 | ]) |
||
| 37 | ->addColumn('callsign', 'string', [ |
||
| 38 | 'null' => true, |
||
| 39 | 'comment' => 'The callsign used by the player during this match.', |
||
| 40 | ]) |
||
| 41 | ->addColumn('ip_address', 'string', [ |
||
| 42 | 'limit' => 46, |
||
| 43 | 'null' => true, |
||
| 44 | 'comment' => 'The IP address used by the player in this match', |
||
| 45 | ]) |
||
| 46 | // Integer operations in SQL are faster than strings; so for team loyalty, we can simplify it to 0 or 1 |
||
| 47 | ->addColumn('team_loyalty', 'integer', [ |
||
| 48 | 'after' => 'callsign', |
||
| 49 | 'limit' => 1, |
||
| 50 | 'null' => false, |
||
| 51 | 'comment' => 'The team color this player played for: 0 will be for "TEAM A" and 1 will be for "TEAM B"' |
||
| 52 | ]) |
||
| 53 | ->addForeignKey('match_id', 'matches', 'id', ['delete' => 'CASCADE']) |
||
| 54 | ->addForeignKey('user_id', 'players', 'id', ['delete' => 'CASCADE']) |
||
| 55 | ->addForeignKey('team_id', 'teams', 'id', ['delete' => 'SET_NULL']) |
||
| 56 | ->create() |
||
| 57 | ; |
||
| 58 | |||
| 59 | $statement = $this->query("SELECT * FROM matches WHERE (team_a_players IS NOT NULL AND team_a_players != '') OR (team_b_players IS NOT NULL AND team_b_players != '')"); |
||
| 60 | $matches = $statement->fetchAll(); |
||
|
|
|||
| 61 | $insertData = []; |
||
| 62 | |||
| 63 | foreach ($matches as $match) { |
||
| 64 | $team_a_players = explode(',', $match['team_a_players']); |
||
| 65 | $team_b_players = explode(',', $match['team_b_players']); |
||
| 66 | |||
| 67 | $dataBuilder = function(array $playerIDs, $isTeamB) use (&$insertData, $match) { |
||
| 68 | foreach ($playerIDs as $playerID) { |
||
| 69 | if (empty($playerID)) { |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | |||
| 73 | $workspace = [ |
||
| 74 | 'match_id' => $match['id'], |
||
| 75 | 'user_id' => $playerID, |
||
| 76 | 'team_loyalty' => (int)$isTeamB, |
||
| 77 | ]; |
||
| 78 | |||
| 79 | if ($match['team_a'] !== null && !$isTeamB) { |
||
| 80 | $workspace['team_id'] = $match['team_a']; |
||
| 81 | } elseif ($match['team_b'] !== null && $isTeamB) { |
||
| 82 | $workspace['team_id'] = $match['team_b']; |
||
| 83 | } |
||
| 84 | |||
| 85 | $insertData[] = $workspace; |
||
| 86 | } |
||
| 87 | }; |
||
| 88 | |||
| 89 | $dataBuilder($team_a_players, false); |
||
| 90 | $dataBuilder($team_b_players, true); |
||
| 91 | } |
||
| 92 | |||
| 93 | // Only attempt to insert data if we actually have data to insert, otherwise an exception will be thrown |
||
| 94 | if (!empty($insertData)) { |
||
| 95 | $playerParticipationTable->insert($insertData); |
||
| 96 | $playerParticipationTable->saveData(); |
||
| 97 | } |
||
| 98 | |||
| 99 | $matchesTable = $this->table('matches'); |
||
| 100 | $matchesTable |
||
| 101 | ->removeColumn('team_a_players') |
||
| 102 | ->removeColumn('team_b_players') |
||
| 103 | ->update() |
||
| 104 | ; |
||
| 105 | } |
||
| 106 | |||
| 148 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.