| Conditions | 9 |
| Paths | 1 |
| Total Lines | 83 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 180 | public function getScore(Collection $matches) |
||
| 181 | { |
||
| 182 | $score = new Collection(); |
||
| 183 | |||
| 184 | $matches->map(function ($match) use ($score) { |
||
| 185 | |||
| 186 | $homeTeam = $score->pull($match->homeTournamentTeam->id); |
||
| 187 | $awayTeam = $score->pull($match->awayTournamentTeam->id); |
||
| 188 | |||
| 189 | $defaultTeamData = [ |
||
| 190 | 'matches' => 0, |
||
| 191 | 'position' => 0, |
||
| 192 | 'wins' => 0, |
||
| 193 | 'draws' => 0, |
||
| 194 | 'losts' => 0, |
||
| 195 | 'points' => 0, |
||
| 196 | 'goalsScored' => 0, |
||
| 197 | 'goalsAgainsted' => 0, |
||
| 198 | 'goalsDifference' => 0, |
||
| 199 | ]; |
||
| 200 | |||
| 201 | if (!$homeTeam) { |
||
| 202 | $homeTeam = array_merge( |
||
| 203 | [ |
||
| 204 | 'teamId' => $match->homeTournamentTeam->id, |
||
| 205 | 'name' => $match->homeTournamentTeam->team->name, |
||
| 206 | ], |
||
| 207 | $defaultTeamData |
||
| 208 | ); |
||
| 209 | } |
||
| 210 | |||
| 211 | if (!$awayTeam) { |
||
| 212 | $awayTeam = array_merge( |
||
| 213 | [ |
||
| 214 | 'teamId' => $match->awayTournamentTeam->id, |
||
| 215 | 'name' => $match->awayTournamentTeam->team->name, |
||
| 216 | ], |
||
| 217 | $defaultTeamData |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | $teams =$this->matchScore($match, $homeTeam, $awayTeam); |
||
| 222 | $score->put($match->homeTournamentTeam->id, $teams['homeTeam']); |
||
| 223 | $score->put($match->awayTournamentTeam->id, $teams['awayTeam']); |
||
| 224 | }); |
||
| 225 | |||
| 226 | // sort by points and goal difference |
||
| 227 | $score = $score->sort(function ($a, $b) { |
||
| 228 | if ($b['points'] === $a['points']) { |
||
| 229 | return $b['goalsDifference'] - $a['goalsDifference']; |
||
| 230 | } |
||
| 231 | |||
| 232 | return $b['points'] - $a['points']; |
||
| 233 | }); |
||
| 234 | |||
| 235 | $previousRow = null; |
||
| 236 | $position = 1; |
||
| 237 | $score = $score->map(function ($row) use (&$previousRow, &$position) { |
||
| 238 | if ($previousRow |
||
| 239 | && $previousRow['points'] > 0 |
||
| 240 | && $previousRow['points'] == $row['points'] |
||
| 241 | && $previousRow['goalsDifference'] == $row['goalsDifference'] |
||
| 242 | && $previousRow['goalsScored'] == $row['goalsScored'] |
||
| 243 | ) { |
||
| 244 | $row['position'] = $previousRow['position']; |
||
| 245 | } else { |
||
| 246 | $row['position'] = $position; |
||
| 247 | } |
||
| 248 | |||
| 249 | $position++; |
||
| 250 | |||
| 251 | $previousRow = $row; |
||
| 252 | |||
| 253 | return $row; |
||
| 254 | }); |
||
| 255 | |||
| 256 | // alphabetical sort for teams on the same position |
||
| 257 | $score = $score->sortBy(function ($team) { |
||
| 258 | return $team['position'] . '-' . $team['name']; |
||
| 259 | }, SORT_NUMERIC); |
||
| 260 | |||
| 261 | return $score; |
||
| 262 | } |
||
| 263 | |||
| 288 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.