| Conditions | 5 |
| Paths | 9 |
| Total Lines | 55 |
| Code Lines | 41 |
| 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 |
||
| 51 | function grant_credits_for_wu($wuid) { |
||
| 52 | $max_credit=300; |
||
| 53 | $ndone = 0; |
||
| 54 | $query_r = _mysql_query("select * from result where granted_credit=0 and claimed_credit>0 and workunitid=$wuid"); |
||
| 55 | |||
| 56 | while ($result = _mysql_fetch_object($query_r)) { |
||
| 57 | echo "STARTING RESULT $result->id [Credit $result->claimed_credit] ..."; |
||
| 58 | $ndone++; |
||
| 59 | |||
| 60 | $hostid = $result->hostid; |
||
| 61 | $query_h = _mysql_query("select * from host where id=$hostid"); |
||
| 62 | $host = _mysql_fetch_object($query_h); |
||
| 63 | |||
| 64 | $userid = $result->userid; |
||
| 65 | $query_u = _mysql_query("select * from user where id=$userid"); |
||
| 66 | $user = _mysql_fetch_object($query_u); |
||
| 67 | |||
| 68 | $credit = $result->claimed_credit; |
||
| 69 | if ($credit>$max_credit) { |
||
| 70 | $credit=$max_credit; |
||
| 71 | echo " WARNING: USER $user->name ($userid) CLAIMED $result->claimed_credit CREDITS (getting $credit)!"; |
||
| 72 | } |
||
| 73 | $user->total_credit += $credit; |
||
| 74 | update_average(time(0), $result->sent_time, $credit, $user->expavg_credit, $user->expavg_time); |
||
| 75 | |||
| 76 | $host->total_credit += $credit; |
||
| 77 | update_average(time(0), $result->sent_time, $credit, $host->expavg_credit, $host->expavg_time); |
||
| 78 | |||
| 79 | $turnaround = $result->received_time - $result->sent_time; |
||
| 80 | if ($host->avg_turnaround > 0) |
||
| 81 | $host->avg_turnaround = 0.7*$host->avg_turnaround + 0.3*$turnaround; |
||
| 82 | else |
||
| 83 | $host->avg_turnaround = $turnaround; |
||
| 84 | |||
| 85 | testquery("update result set granted_credit=$credit where id=$result->id"); |
||
| 86 | |||
| 87 | testquery("update user set total_credit=$user->total_credit, expavg_credit=$user->expavg_credit, expavg_time=$user->expavg_time where id=$userid"); |
||
| 88 | |||
| 89 | testquery("update host set total_credit=$host->total_credit, expavg_credit=$host->expavg_credit, expavg_time=$host->expavg_time, avg_turnaround=$host->avg_turnaround where id=$hostid"); |
||
| 90 | |||
| 91 | $teamid = $user->teamid; |
||
| 92 | if ($teamid) { |
||
| 93 | $query_t = _mysql_query("select * from team where id=$teamid"); |
||
| 94 | $team = _mysql_fetch_object($query_t); |
||
| 95 | $team->total_credit += $credit; |
||
| 96 | update_average(time(0), $result->sent_time, $credit, $team->expavg_credit, $team->expavg_time); |
||
| 97 | testquery("update team set total_credit=$team->total_credit, expavg_credit=$team->expavg_credit, expavg_time=$team->expavg_time where id=$teamid"); |
||
| 98 | _mysql_free_result($query_t); |
||
| 99 | } |
||
| 100 | _mysql_free_result($query_h); |
||
| 101 | _mysql_free_result($query_u); |
||
| 102 | echo " DONE\n"; |
||
| 103 | } |
||
| 104 | _mysql_free_result($query_r); |
||
| 105 | return $ndone; |
||
| 106 | } |
||
| 124 |