| Conditions | 7 |
| Paths | 6 |
| Total Lines | 81 |
| 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 |
||
| 30 | public function submit(&$idea, $user_id, $value) |
||
| 31 | { |
||
| 32 | // Validate $vote - must be 0 or 1 |
||
| 33 | if ($value !== 0 && $value !== 1) |
||
| 34 | { |
||
| 35 | return 'INVALID_VOTE'; |
||
| 36 | } |
||
| 37 | |||
| 38 | // Check whether user has already voted - update if they have |
||
| 39 | $sql = 'SELECT idea_id, vote_value |
||
| 40 | FROM ' . $this->table_votes . ' |
||
| 41 | WHERE idea_id = ' . (int) $idea['idea_id'] . ' |
||
| 42 | AND user_id = ' . (int) $user_id; |
||
| 43 | $this->db->sql_query_limit($sql, 1); |
||
| 44 | if ($row = $this->db->sql_fetchrow()) |
||
| 45 | { |
||
| 46 | if ($row['vote_value'] != $value) |
||
| 47 | { |
||
| 48 | $sql = 'UPDATE ' . $this->table_votes . ' |
||
| 49 | SET vote_value = ' . $value . ' |
||
| 50 | WHERE user_id = ' . (int) $user_id . ' |
||
| 51 | AND idea_id = ' . (int) $idea['idea_id']; |
||
| 52 | $this->db->sql_query($sql); |
||
| 53 | |||
| 54 | if ($value == 1) |
||
| 55 | { |
||
| 56 | // Change to upvote |
||
| 57 | $idea['idea_votes_up']++; |
||
| 58 | $idea['idea_votes_down']--; |
||
| 59 | } |
||
| 60 | else |
||
| 61 | { |
||
| 62 | // Change to downvote |
||
| 63 | $idea['idea_votes_up']--; |
||
| 64 | $idea['idea_votes_down']++; |
||
| 65 | } |
||
| 66 | |||
| 67 | $sql_ary = array( |
||
| 68 | 'idea_votes_up' => $idea['idea_votes_up'], |
||
| 69 | 'idea_votes_down' => $idea['idea_votes_down'], |
||
| 70 | ); |
||
| 71 | |||
| 72 | $this->update_idea_data($sql_ary, $idea['idea_id'], $this->table_ideas); |
||
| 73 | } |
||
| 74 | |||
| 75 | return array( |
||
| 76 | 'message' => $this->language->lang('UPDATED_VOTE'), |
||
| 77 | 'votes_up' => $idea['idea_votes_up'], |
||
| 78 | 'votes_down' => $idea['idea_votes_down'], |
||
| 79 | 'points' => $this->language->lang('TOTAL_POINTS', $idea['idea_votes_up'] - $idea['idea_votes_down']), |
||
| 80 | 'voters' => $this->get_voters($idea['idea_id']), |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Insert vote into votes table. |
||
| 85 | $sql_ary = array( |
||
| 86 | 'idea_id' => (int) $idea['idea_id'], |
||
| 87 | 'user_id' => (int) $user_id, |
||
| 88 | 'vote_value' => (int) $value, |
||
| 89 | ); |
||
| 90 | |||
| 91 | $this->insert_idea_data($sql_ary, $this->table_votes); |
||
| 92 | |||
| 93 | // Update number of votes in ideas table |
||
| 94 | $idea['idea_votes_' . ($value ? 'up' : 'down')]++; |
||
| 95 | |||
| 96 | $sql_ary = array( |
||
| 97 | 'idea_votes_up' => $idea['idea_votes_up'], |
||
| 98 | 'idea_votes_down' => $idea['idea_votes_down'], |
||
| 99 | ); |
||
| 100 | |||
| 101 | $this->update_idea_data($sql_ary, $idea['idea_id'], $this->table_ideas); |
||
| 102 | |||
| 103 | return array( |
||
| 104 | 'message' => $this->language->lang('VOTE_SUCCESS'), |
||
| 105 | 'votes_up' => $idea['idea_votes_up'], |
||
| 106 | 'votes_down' => $idea['idea_votes_down'], |
||
| 107 | 'points' => $this->language->lang('TOTAL_POINTS', $idea['idea_votes_up'] - $idea['idea_votes_down']), |
||
| 108 | 'voters' => $this->get_voters($idea['idea_id']), |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 200 |