| Conditions | 26 |
| Paths | > 20000 |
| Total Lines | 177 |
| Code Lines | 108 |
| Lines | 84 |
| Ratio | 47.46 % |
| Changes | 9 | ||
| Bugs | 1 | 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 namespace GolfLeague\Services; |
||
| 167 | public function finalize($matchdata) |
||
| 168 | { |
||
| 169 | // post CTP1 and CTP2 |
||
| 170 | $ctp1 = array( |
||
| 171 | 'match_id' => $matchdata['match'], |
||
| 172 | 'player_id' => $matchdata['ctp1'], |
||
| 173 | 'hole_id' => $matchdata['ctp1hole'], |
||
| 174 | 'money' => $this->prizeMoney->getCtp() |
||
| 175 | ); |
||
| 176 | $this->ctp->create($ctp1); |
||
| 177 | $ctp2 = array( |
||
| 178 | 'match_id' => $matchdata['match'], |
||
| 179 | 'player_id' => $matchdata['ctp2'], |
||
| 180 | 'hole_id' => $matchdata['ctp2hole'], |
||
| 181 | 'money' => $this->prizeMoney->getCtp() |
||
| 182 | ); |
||
| 183 | $this->ctp->create($ctp2); |
||
| 184 | |||
| 185 | //calculate Gross winner and post to grossWinnersTable |
||
| 186 | |||
| 187 | $matchRound = $this->matchRoundRepo->getByMatch($matchdata['match']); |
||
| 188 | |||
| 189 | |||
| 190 | $lowGross = array(); |
||
| 191 | foreach ($matchRound as $key => $match){ |
||
| 192 | $lowGross[$match['player']->id] = $match['score']; |
||
| 193 | } |
||
| 194 | $arrayLowGross = array_keys($lowGross, min($lowGross)); |
||
| 195 | View Code Duplication | foreach($arrayLowGross as $key => $lowgrossPlayer) { |
|
| 196 | $grossWinner = new Grosswinner; |
||
| 197 | $grossWinner->player_id = $lowgrossPlayer; |
||
| 198 | $grossWinner->match_id = $matchdata['match']; |
||
| 199 | $grossWinner->score = $lowGross[$lowgrossPlayer]; |
||
| 200 | $grossWinner->money = $this->prizeMoney->getlowScore() / count($arrayLowGross); |
||
| 201 | $grossWinner->save(); |
||
| 202 | } |
||
| 203 | |||
| 204 | //Calculate NET winner |
||
| 205 | $lowNet = array(); |
||
| 206 | $scores =array(); |
||
| 207 | foreach ($matchRound as $key => $match){ |
||
| 208 | $netScore = ($match['score'] - round($match['player']->handicap,0)); //calculate net score |
||
| 209 | $lowNet[$match['player']->id] = $netScore; |
||
| 210 | } |
||
| 211 | $arrayLowNet = array_keys($lowNet, min($lowNet)); |
||
| 212 | |||
| 213 | View Code Duplication | foreach($arrayLowNet as $key => $lownetPlayer) { |
|
| 214 | $netWinner = new Netwinner; |
||
| 215 | $netWinner->player_id = $lownetPlayer; |
||
| 216 | $netWinner->match_id = $matchdata['match']; |
||
| 217 | $netWinner->score = $lowNet[$lownetPlayer]; |
||
| 218 | $netWinner->money = $this->prizeMoney->getlowScore() / count($arrayLowNet); |
||
| 219 | $netWinner->save(); |
||
| 220 | } |
||
| 221 | |||
| 222 | //Calculate Skins |
||
| 223 | |||
| 224 | //determine A and B players |
||
| 225 | //using pivot table match_player |
||
| 226 | $match = $this->match->find($matchdata['match']); |
||
| 227 | $aPlayers = array(); |
||
| 228 | $bPlayers = array(); |
||
| 229 | foreach($match->players as $player) |
||
| 230 | { |
||
| 231 | if($player->pivot->level_id == 1){ |
||
| 232 | $aPlayers[] = $player->pivot->player_id; |
||
| 233 | |||
| 234 | } |
||
| 235 | if($player->pivot->level_id == 2){ |
||
| 236 | $bPlayers[] = $player->pivot->player_id; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | //Create Skins arrays |
||
| 241 | //player_id, 'holescores' |
||
| 242 | foreach($matchRound as $key => $round) { |
||
| 243 | View Code Duplication | if (in_array($round->player_id,$aPlayers)){ |
|
| 244 | $aPlayersSkins[$key]['player_id'] = $round->player_id; |
||
| 245 | $aPlayersSkins[$key]['holescores'] = $round->holescores; |
||
| 246 | } |
||
| 247 | View Code Duplication | if (in_array($round->player_id,$bPlayers)){ |
|
| 248 | $bPlayersSkins[$key]['player_id'] = $round->player_id; |
||
| 249 | $bPlayersSkins[$key]['holescores'] = $round->holescores; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | //Run A Skins analysis |
||
| 254 | $scores = array(); |
||
| 255 | View Code Duplication | foreach($aPlayersSkins as $key => $playerSkin){ |
|
| 256 | foreach($playerSkin['holescores'] as $hole => $holescore){ |
||
| 257 | $scores[$holescore['hole_id']][$playerSkin['player_id']] = $holescore['score']; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | View Code Duplication | foreach($scores as $hole_id => $hole) { |
|
| 262 | $minScore = min($hole); |
||
| 263 | $winners[$hole_id] = array_keys($hole, min($hole)); //gets player id of lowest score |
||
| 264 | } |
||
| 265 | $aSkinsWon = 0; |
||
| 266 | View Code Duplication | foreach($winners as $key => $winner) { |
|
| 267 | if(count($winner) === 1) { |
||
| 268 | //post to DB |
||
| 269 | $skinWinner = new Skin; |
||
| 270 | $skinWinner->player_id = $winner[0]; |
||
| 271 | $skinWinner->level_id = 1; |
||
| 272 | $skinWinner->match_id = intval($matchdata['match']); |
||
| 273 | $skinWinner->hole_id = $key; |
||
| 274 | $skinWinner->save(); |
||
| 275 | $aSkinsWon++; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | //Run B Skins analysis |
||
| 280 | $scores = array(); |
||
| 281 | View Code Duplication | foreach($bPlayersSkins as $key => $playerSkin){ |
|
| 282 | foreach($playerSkin['holescores'] as $hole => $holescore){ |
||
| 283 | $scores[$holescore['hole_id']][$playerSkin['player_id']] = $holescore['score']; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | View Code Duplication | foreach($scores as $hole_id => $hole) { |
|
| 288 | $minScore = min($hole); |
||
| 289 | $winners[$hole_id] = array_keys($hole, min($hole)); //gets player id of lowest score |
||
| 290 | } |
||
| 291 | $bSkinsWon = 0; |
||
| 292 | View Code Duplication | foreach($winners as $key => $winner) { |
|
| 293 | if(count($winner) === 1) { |
||
| 294 | //post to DB |
||
| 295 | $skinWinner = new Skin; |
||
| 296 | $skinWinner->player_id = $winner[0]; |
||
| 297 | $skinWinner->level_id = 2; |
||
| 298 | $skinWinner->match_id = intval($matchdata['match']); |
||
| 299 | $skinWinner->hole_id = $key; |
||
| 300 | $skinWinner->save(); |
||
| 301 | $bSkinsWon++; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | $match = Match::find($matchdata['match']); |
||
| 306 | |||
| 307 | //Need to add Carry over money if there no skins are won |
||
| 308 | //check for carry over money |
||
| 309 | $skinsamoney = $match->skinsamoney; // + carryover A money if any |
||
| 310 | $skinsbmoney = $match->skinsbmoney; // + carryover B money if any |
||
| 311 | |||
| 312 | View Code Duplication | if($aSkinsWon > 0) { |
|
| 313 | $moneyperskinA = $skinsamoney / $aSkinsWon; |
||
| 314 | |||
| 315 | $aSkins = Skin::where('match_id', '=', $matchdata['match'])->where('level_id', '=', 1)->get(); |
||
| 316 | foreach ($aSkins as $askin){ |
||
| 317 | $askin->money = $moneyperskinA; |
||
| 318 | $askin->save(); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | View Code Duplication | if($bSkinsWon > 0) { |
|
| 323 | $moneyperskinB = $skinsbmoney / $bSkinsWon; |
||
| 324 | |||
| 325 | $bSkins = Skin::where('match_id', '=', $matchdata['match'])->where('level_id', '=', 2)->get(); |
||
| 326 | foreach ($bSkins as $bskin){ |
||
| 327 | $bskin->money = $moneyperskinB; |
||
| 328 | $bskin->save(); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | //foreach player in pivot table create player and run handicap analysis |
||
| 332 | foreach($match->players as $matchplayer) |
||
| 333 | { |
||
| 334 | $player = Player::find($matchplayer->pivot->player_id); |
||
| 335 | $handicap = new Handicap($player); |
||
| 336 | $player->handicap = $handicap->calculate(); |
||
| 337 | $player->save(); |
||
| 338 | } |
||
| 339 | |||
| 340 | //Fire event to calculate money won and add to pivot table match_player |
||
| 341 | $this->events->fire('match.finalize', $match); |
||
| 342 | |||
| 343 | } |
||
| 344 | |||
| 373 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.