| Conditions | 11 |
| Paths | 258 |
| Total Lines | 50 |
| Code Lines | 23 |
| 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 |
||
| 27 | function UserKarma($user_id, $karma_action) { |
||
| 28 | global $mysqli; |
||
| 29 | if (empty($user_id)) { |
||
| 30 | die("user_id wasn't passed properly"); |
||
|
|
|||
| 31 | } |
||
| 32 | if (empty($karma_action)) { |
||
| 33 | die("karma values wasn't passed properly"); |
||
| 34 | } |
||
| 35 | |||
| 36 | $sql_karma = $mysqli->query("SELECT karma FROM users WHERE user_id = '$user_id'") or die("failed to query users"); |
||
| 37 | list($karma_value) = $sql_karma->fetch_row(); |
||
| 38 | |||
| 39 | // For downloads |
||
| 40 | if ($karma_action == "game_downloads") { |
||
| 41 | $karma_value = $karma_value - 5; |
||
| 42 | } |
||
| 43 | |||
| 44 | // For game comments |
||
| 45 | if ($karma_action == "game_comment") { |
||
| 46 | $karma_value = $karma_value + 5; |
||
| 47 | } |
||
| 48 | |||
| 49 | // For game submissions |
||
| 50 | if ($karma_action == "game_submission") { |
||
| 51 | $karma_value = $karma_value + 10; |
||
| 52 | } |
||
| 53 | |||
| 54 | // For submitting weblinks |
||
| 55 | if ($karma_action == "weblink") { |
||
| 56 | $karma_value = $karma_value + 3; |
||
| 57 | } |
||
| 58 | |||
| 59 | // For submitting news |
||
| 60 | if ($karma_action == "news_update") { |
||
| 61 | $karma_value = $karma_value + 3; |
||
| 62 | } |
||
| 63 | |||
| 64 | // For submitting game_reviews |
||
| 65 | if ($karma_action == "game_review") { |
||
| 66 | $karma_value = $karma_value + 3; |
||
| 67 | } |
||
| 68 | |||
| 69 | // For submitting game_reviews |
||
| 70 | if ($karma_action == "demo_submission") { |
||
| 71 | $karma_value = $karma_value + 10; |
||
| 72 | } |
||
| 73 | |||
| 74 | $update_karma = $mysqli->query("UPDATE users SET karma='$karma_value' WHERE user_id = '$user_id'"); |
||
| 75 | |||
| 76 | return; |
||
| 77 | } |
||
| 96 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.