| Conditions | 13 | 
| Paths | 137 | 
| Total Lines | 102 | 
| Code Lines | 65 | 
| 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 | ||
| 34 |     private function endChallenge($params) { | ||
| 35 | $id = (int)$params['id']; | ||
| 36 | |||
| 37 | |||
| 38 | $ch = Yii::app()->db->createCommand() | ||
| 39 |             ->select('*') | ||
| 40 |             ->from('challenge') | ||
| 41 |             ->where('id=:id', [':id'=>$id]) | ||
| 42 | ->queryRow(); | ||
| 43 | |||
| 44 | if ($ch['winner']) return false; | ||
| 45 | |||
| 46 |         if ($ch['point_caller'] <> $ch['point_opponent']) { | ||
| 47 | //not equal points | ||
| 48 | $winnerTag = $ch['point_caller'] < $ch['point_opponent'] ? 'opponent' : 'caller'; //caller only lose, if she has less points, that opponent. | ||
| 49 |         } else { | ||
| 50 | //equal points | ||
| 51 | $winnerTag = $ch['cnt_won_caller'] < $ch['cnt_won_opponent'] ? 'opponent' : 'caller'; //caller only lose, if she has less won games, that opponent. | ||
| 52 | } | ||
| 53 | |||
| 54 | $looserTag = $winnerTag == 'caller' ? 'opponent' : 'caller'; | ||
| 55 | |||
| 56 | $forum = new Forum; | ||
| 57 | |||
| 58 | //without games | ||
| 59 |         if (!$ch['cnt_won_caller'] and !$ch['cnt_won_opponent']) { | ||
| 60 | //close unplayed challenge, no winners | ||
| 61 |             $msg = "{$ch['name_caller']} - {$ch['name_opponent']}: ez a verseny párbajok nélkül ért véget, így jutalmat sem kaptok."; | ||
| 62 | $forum->id = $ch['caller']; | ||
| 63 | $forum->save($msg, true); | ||
| 64 | $forum->id = $ch['opponent']; | ||
| 65 | $forum->save($msg, true); | ||
| 66 | |||
| 67 | //update the winner-state | ||
| 68 | Yii::app()->db->createCommand() | ||
| 69 |                 ->update('challenge', ['winner'=>3], 'id=:id', [':id'=>$id]); | ||
| 70 | return false; | ||
| 71 | } | ||
| 72 | |||
| 73 | $forum->id = $ch[$winnerTag]; | ||
| 74 | |||
| 75 | $club = new Club; | ||
| 76 | $club->id = $ch[$winnerTag]; //winner club | ||
| 77 | $club->fetch(); | ||
| 78 | $club->fetchMembers(); | ||
| 79 | |||
| 80 | |||
| 81 |         $winnerMsg = "Gratulálok! :) Győztetek a következő versenyben: <b> {$ch['name_caller']} - {$ch['name_opponent']}</b>. "; | ||
| 82 |         $looserMsg = "Sajnos elbuktátok a következő versenyt: <b> {$ch['name_caller']} - {$ch['name_opponent']}</b>. "; | ||
| 83 | |||
| 84 | //distribute the loot | ||
| 85 | $members = [$club->owner=>$club->ownerName]; | ||
| 86 |         foreach ($club->members as $member) { | ||
| 87 | $members[$member['uid']] = $member['user']; | ||
| 88 | } | ||
| 89 | $cntMembers = count($members); | ||
| 90 | $loot = (int)$ch['loot_'.$winnerTag]; | ||
| 91 | $lootPerMember = floor($loot / $cntMembers); | ||
| 92 | |||
| 93 | $forum->id = $ch[$winnerTag]; | ||
| 94 |         if ($lootPerMember > 0) { | ||
| 95 |             $winnerMsg .= "Minden tag <b> {$lootPerMember}$</b>-t kap a zsákmányból. Név szerint: "; | ||
| 96 | |||
| 97 | $contest = new Contest; | ||
| 98 | $listPlayers = []; | ||
| 99 | $p = new Player; | ||
| 100 |             foreach ($members as $uid => $member) { | ||
| 101 | $p->setAllAttributes($uid); | ||
| 102 | $incr = ['dollar'=>$lootPerMember]; | ||
| 103 | $p->updateAttributes($incr, []); | ||
| 104 | $contest->addPoints($p->uid, Contest::ACT_DUEL, 0, 0, $incr['dollar']); | ||
| 105 | |||
| 106 | $listPlayers[] = $p->user; | ||
| 107 | } | ||
| 108 |             $winnerMsg .= join(', ', $listPlayers) . '. '; | ||
| 109 |         } else { | ||
| 110 |             $winnerMsg .= "Sajnos a zsákmány ({$loot}$) túl alacsony ahhoz, hogy osztozzatok rajta. "; | ||
| 111 | } | ||
| 112 | $lootLosers = (int)$ch['loot_'.$looserTag]; | ||
| 113 |         $looserMsg .= "Mivel nem nyertetek, a zsákmány ({$lootLosers}$) a horgásszövetség tulajdonába kerül. Köszönik szépen!"; | ||
| 114 | |||
| 115 | //refresh the toplist | ||
| 116 | $winnerPoints = (int)$ch['point_'.$winnerTag]; | ||
| 117 | if (!$winnerPoints) $winnerPoints = 1; //when result is 0:0 | ||
| 118 | |||
| 119 |         Yii::app()->redis->getClient()->zIncrBy('board_c:'.date('Ym'), $winnerPoints, $ch[$winnerTag]); | ||
| 120 | |||
| 121 |         $winnerMsg .= "A klub <b> {$winnerPoints} pontot </b> erősödött a ranglistán."; | ||
| 122 | |||
| 123 | //report | ||
| 124 | $forum->id = $ch[$winnerTag]; | ||
| 125 | $forum->save($winnerMsg, true); | ||
| 126 | $forum->id = $ch[$looserTag]; | ||
| 127 | $forum->save($looserMsg, true); | ||
| 128 | |||
| 129 | //refresh the winner state of the challenge | ||
| 130 | $winner = $winnerTag=='caller' ? 1 : 2; | ||
| 131 | Yii::app()->db->createCommand() | ||
| 132 |             ->update('challenge', ['winner'=>$winner], 'id=:id', [':id'=>$id]); | ||
| 133 | |||
| 134 | return true; | ||
| 135 | } | ||
| 136 | |||
| 142 | 
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.