Conditions | 11 |
Paths | 7 |
Total Lines | 49 |
Code Lines | 38 |
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 |
||
97 | private function generateLosingSide(int &$r, \TournamentGenerator\Round &$round, array &$allGroups, array &$previousLosingGroups = [], array &$previousGroups = [], array &$losingGroups = []) { |
||
98 | $losingGroupTeamsCount = count($previousLosingGroups)+count($previousGroups); |
||
99 | $order = 2; |
||
100 | if (\TournamentGenerator\isPowerOf2($losingGroupTeamsCount)) { // IF THE NUMBER OF TEAMS IS A POWER OF 2, GENERATE GROUPS WITHOUT BYES |
||
101 | for ($g=1; $g <= $losingGroupTeamsCount/2; $g++) { |
||
102 | $group = $round->group('Round '.$r.' - loss '.$g)->setInGame(2)->setType(\TournamentGenerator\Constants::ROUND_TWO)->setOrder($order); |
||
103 | $allGroups[] = $group; |
||
104 | $order += 2; |
||
105 | $losingGroups[] = $group; |
||
106 | $lastLosingGroup = $group; // KEEP THE LAST GROUP FOR FINALE |
||
107 | if ($r === 2) { // FIRST LOSING ROUND |
||
108 | $previousGroups[2*($g-1)]->progression($group, 1, 1); // PROGRESS FROM STARTING GROUP |
||
109 | $previousGroups[(2*($g-1))+1]->progression($group, 1, 1); // PROGREESS FROM STARTING GROUP |
||
110 | } |
||
111 | elseif ($losingGroupTeamsCount >= 2) { |
||
112 | $previousLosingGroups[$g-1]->progression($group, 0, 1); // PROGRESS FROM LOSING GROUP BEFORE |
||
113 | if (isset(array_reverse($previousGroups)[$g-1])) array_reverse($previousGroups)[$g-1]->progression($group, 1, 1); // PROGREESS FROM WINNING GROUP BEFORE |
||
114 | else $previousLosingGroups[$g]->progression($group, 0, 1); // PROGRESS OTHER TEAM FROM LOSING GROUP BEEFORE |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | else { // IF THE NUMBER OF TEAMS IS NOT A POWER OF 2, GENERATE GROUPS WITH BYES |
||
119 | // LOOK FOR THE CLOSEST LOWER POWER OF 2 |
||
120 | $losingByes = $losingGroupTeamsCount-bindec(str_pad(1, strlen(decbin($losingGroupTeamsCount)), 0, STR_PAD_RIGHT)); |
||
121 | $n = (floor(count($previousLosingGroups)/2)+$losingByes); |
||
122 | $byesGroupsNums = []; |
||
123 | $byesProgressed = 0; |
||
124 | for ($i=0; $i < $losingByes; $i++) { |
||
125 | $byesGroupsNums[] = $n-($i*2); |
||
126 | } |
||
127 | $lastGroup = 0; |
||
128 | for ($g=1; $g <= ((count($previousLosingGroups)/2)+$losingByes); $g++) { |
||
129 | $group = $round->group('Round '.$r.' - loss '.$g)->setInGame(2)->setType(\TournamentGenerator\Constants::ROUND_TWO)->setOrder($order); |
||
130 | $allGroups[] = $group; |
||
131 | $order += 2; |
||
132 | $losingGroups[] = $group; |
||
133 | $lastLosingGroup = $group; // KEEP THE LAST GROUP FOR FINALE |
||
134 | if (in_array($g, $byesGroupsNums) && isset($previousGroups[$byesProgressed])) { // EMPTY GROUP FROM BYE |
||
135 | $previousGroups[$byesProgressed]->progression($group, 1, 1); // PROGRESS FROM WINNING GROUP BEFORE |
||
136 | $byesProgressed++; |
||
137 | } |
||
138 | else { |
||
139 | $previousLosingGroups[$lastGroup]->progression($group, 0, 1); // PROGRESS FROM LOSING GROUP BEFORE |
||
140 | if (isset($previousLosingGroups[$lastGroup + 1])) $previousLosingGroups[$lastGroup + 1]->progression($group, 0, 1); // PROGREESS FROM LOSING GROUP BEFORE |
||
141 | $lastGroup += 2; |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | return $lastLosingGroup; |
||
146 | } |
||
149 |