| Conditions | 8 |
| Paths | 2 |
| Total Lines | 92 |
| Code Lines | 64 |
| Lines | 5 |
| Ratio | 5.43 % |
| 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 | { |
||
| 65 | $team_a_players = explode(',', $match['team_a_players']); |
||
| 66 | $team_b_players = explode(',', $match['team_b_players']); |
||
| 67 | |||
| 68 | $dataBuilder = function(array $playerIDs, $isTeamA) use (&$insertData, $match) { |
||
| 69 | foreach ($playerIDs as $playerID) { |
||
| 70 | if (empty($playerID)) { |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | $workspace = [ |
||
| 75 | 'match_id' => $match['id'], |
||
| 76 | 'user_id' => $playerID, |
||
| 77 | 'team_loyalty' => (int)$isTeamA, |
||
| 78 | ]; |
||
| 79 | |||
| 80 | View Code Duplication | if ($match['team_a'] !== null && $isTeamA) { |
|
| 81 | $workspace['team_id'] = $match['team_a']; |
||
| 82 | } elseif ($match['team_b'] !== null && !$isTeamA) { |
||
| 83 | $workspace['team_id'] = $match['team_b']; |
||
| 84 | } |
||
| 85 | |||
| 86 | $insertData[] = $workspace; |
||
| 87 | } |
||
| 88 | }; |
||
| 89 | |||
| 90 | $dataBuilder($team_a_players, true); |
||
| 91 | $dataBuilder($team_b_players, false); |
||
| 92 | } |
||
| 93 | |||
| 94 | $playerParticipationTable->insert($insertData); |
||
| 95 | $playerParticipationTable->saveData(); |
||
| 96 | |||
| 97 | $matchesTable = $this->table('matches'); |
||
| 98 | $matchesTable |
||
| 99 | ->removeColumn('team_a_players') |
||
| 100 | ->removeColumn('team_b_players') |
||
| 101 | ->update() |
||
| 102 | ; |
||
| 103 | } |
||
| 104 | |||
| 146 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.