Conditions | 15 |
Paths | 432 |
Total Lines | 114 |
Code Lines | 36 |
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 |
||
28 | public function run() |
||
29 | { |
||
30 | |||
31 | //If no names have been entered, then use numbers |
||
32 | |||
33 | if ($this->names != '') { |
||
34 | $teams = $this->names; |
||
35 | |||
36 | //Make sure there's at least 2 teams |
||
37 | |||
38 | if(count($teams) == 1){ |
||
39 | $teams[] = 'Team 2'; |
||
40 | } |
||
41 | |||
42 | $this->noTeams = count($teams); |
||
43 | |||
44 | } |
||
45 | |||
46 | |||
47 | //Calculate the size of the first full round - for example if you have 5 teams, then the first full round will consist of 4 teams |
||
48 | $minimumFirstRoundSize = pow(2, ceil(log($this->noTeams)/log(2))); |
||
49 | $this->noRounds = log($minimumFirstRoundSize, 2); |
||
50 | $noByesToAdd = $minimumFirstRoundSize - $this->noTeams; |
||
51 | |||
52 | //Add the byes to the teams array |
||
53 | for($i = 0; $i < $noByesToAdd; $i++){ |
||
54 | $teams[] = null; |
||
|
|||
55 | } |
||
56 | |||
57 | //Order the teams in a seeded order - this is required regardless of whether it is a seeded tournament or not, as it prevents BYEs playing eachother |
||
58 | for($i = 0; $i < log($this->noTeams / 2, 2); $i++){ |
||
59 | |||
60 | $out = array(); |
||
61 | |||
62 | foreach($teams as $player){ |
||
63 | $splice = pow(2, $i); |
||
64 | $out = array_merge($out, array_splice($teams, 0, $splice)); |
||
65 | $out = array_merge($out, array_splice($teams, -$splice)); |
||
66 | } |
||
67 | |||
68 | $teams = $out; |
||
69 | |||
70 | } |
||
71 | |||
72 | //Randomise the draw if required - keep the position of the byes, but swap the players |
||
73 | |||
74 | // if($this->type != 1){ |
||
75 | // |
||
76 | // $randomTeams = array(); |
||
77 | // $shuffledTeams = array_filter($teams); |
||
78 | // shuffle($shuffledTeams); |
||
79 | // |
||
80 | // foreach($teams as $key => $team){ |
||
81 | // $randomTeams[$key] = is_null($team) ? null : array_pop($shuffledTeams); |
||
82 | // } |
||
83 | // |
||
84 | // $teams = $randomTeams; |
||
85 | // |
||
86 | // } |
||
87 | |||
88 | $roundNumber = 1; |
||
89 | |||
90 | //Group 2 teams into a match |
||
91 | $matches = array_chunk($teams, 2); |
||
92 | |||
93 | //Only check for BYEs if there are more than 2 teams |
||
94 | if($this->noTeams > 2){ |
||
95 | |||
96 | foreach($matches as $key => &$match){ |
||
97 | |||
98 | $matchNumber = $key + 1; |
||
99 | |||
100 | //If both teams are present, then that means they haven't had a BYE to the next round, so they must play in the first round |
||
101 | if($match[0] && $match[1]){ |
||
102 | |||
103 | //Add the match to the first round |
||
104 | $this->brackets[$roundNumber][$matchNumber] = $match; |
||
105 | |||
106 | //Set the match to null as the result of the above match hasn't yet been determined |
||
107 | $match = null; |
||
108 | |||
109 | }else{ |
||
110 | |||
111 | //If only the first or second player exists, then replace the multidimensional array with the existing player |
||
112 | $match = $match[0] ? $match[0] : $match[1]; |
||
113 | |||
114 | } |
||
115 | |||
116 | } |
||
117 | |||
118 | //Now all of the blank spaces except the ones awaiting first round results have gone, group the single dimension array into a multiple dimensional array, so opponents share the same parent array |
||
119 | $matches = array_chunk($matches, 2); |
||
120 | |||
121 | } |
||
122 | |||
123 | //If there's already a match in the match array, then that means the next round is round 2, so increase the round number |
||
124 | if(count($this->brackets)) $roundNumber++; |
||
125 | |||
126 | //Create the first full round of teams, some may be blank if waiting on the results of a previous round |
||
127 | for($i = 0; $i < count($matches); $i++){ |
||
128 | $this->brackets[$roundNumber][$i+1] = $matches[$i]; |
||
129 | } |
||
130 | |||
131 | //Create the result of the empty rows for this tournament |
||
132 | |||
133 | for($roundNumber += 1; $roundNumber <= $this->noRounds; $roundNumber++){ |
||
134 | for($matchNumber = 1; $matchNumber <= ($minimumFirstRoundSize/pow(2, $roundNumber)); $matchNumber++){ |
||
135 | $this->brackets[$roundNumber][$matchNumber] = array(null, null); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | $this->assignPositions(); |
||
140 | |||
141 | } |
||
142 | |||
298 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: