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