| Conditions | 8 |
| Paths | 5 |
| Total Lines | 74 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 |
||
| 43 | public function update_topic_authors() |
||
| 44 | { |
||
| 45 | // Return if the Ideas Bot does not exist at this point for some reason. |
||
| 46 | if (!$this->config->offsetExists('ideas_poster_id')) |
||
| 47 | { |
||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | // Get real author info for ideas that were posted by the Ideas Bot |
||
| 52 | $topics = []; |
||
| 53 | $sql_array = [ |
||
| 54 | 'SELECT' => 'i.topic_id, i.idea_author, u.username, u.user_colour, t.topic_first_post_id', |
||
| 55 | 'FROM' => [ |
||
| 56 | $this->table_prefix . 'ideas_ideas' => 'i', |
||
| 57 | ], |
||
| 58 | 'LEFT_JOIN' => [ |
||
| 59 | [ |
||
| 60 | 'FROM' => [$this->table_prefix . 'topics' => 't'], |
||
| 61 | 'ON' => 't.topic_id = i.topic_id', |
||
| 62 | ], |
||
| 63 | [ |
||
| 64 | 'FROM' => [$this->table_prefix . 'users' => 'u'], |
||
| 65 | 'ON' => 'u.user_id = i.idea_author', |
||
| 66 | ], |
||
| 67 | ], |
||
| 68 | 'WHERE' => 't.topic_poster = ' . (int) $this->config->offsetGet('ideas_poster_id'), |
||
| 69 | ]; |
||
| 70 | |||
| 71 | $sql = $this->db->sql_build_query('SELECT', $sql_array); |
||
| 72 | $result = $this->db->sql_query($sql); |
||
| 73 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 74 | { |
||
| 75 | $topics[$row['topic_id']] = [ |
||
| 76 | 'topic_poster_id' => $row['idea_author'] ?: ANONYMOUS, |
||
| 77 | 'topic_poster_name' => $row['username'] ?: '', |
||
| 78 | 'topic_poster_colour' => $row['user_colour'] ?: '', |
||
| 79 | 'topic_first_post_id' => $row['topic_first_post_id'] ?: 0, |
||
| 80 | ]; |
||
| 81 | } |
||
| 82 | $this->db->sql_freeresult($result); |
||
| 83 | |||
| 84 | // Begin updating topics and posts |
||
| 85 | $this->db->sql_transaction('begin'); |
||
| 86 | foreach ($topics as $topic_id => $data) |
||
| 87 | { |
||
| 88 | // Update topic author (first poster) |
||
| 89 | $sql = 'UPDATE ' . $this->table_prefix . 'topics |
||
| 90 | SET ' . $this->db->sql_build_array('UPDATE', [ |
||
| 91 | 'topic_poster' => $data['topic_poster_id'], |
||
| 92 | 'topic_first_poster_name' => $data['topic_poster_name'], |
||
| 93 | 'topic_first_poster_colour' => $data['topic_poster_colour'], |
||
| 94 | ]) . ' |
||
| 95 | WHERE topic_id = ' . (int) $topic_id; |
||
| 96 | $this->db->sql_query($sql); |
||
| 97 | |||
| 98 | // Update last poster if it's also the Ideas Bot (i.e: no replies) |
||
| 99 | $sql = 'UPDATE ' . $this->table_prefix . 'topics |
||
| 100 | SET ' . $this->db->sql_build_array('UPDATE', [ |
||
| 101 | 'topic_last_poster_id' => $data['topic_poster_id'], |
||
| 102 | 'topic_last_poster_name' => $data['topic_poster_name'], |
||
| 103 | 'topic_last_poster_colour' => $data['topic_poster_colour'], |
||
| 104 | ]) . ' |
||
| 105 | WHERE topic_id = ' . (int) $topic_id . ' |
||
| 106 | AND topic_last_poster_id = ' . (int) $this->config->offsetGet('ideas_poster_id'); |
||
| 107 | $this->db->sql_query($sql); |
||
| 108 | |||
| 109 | // Update first post's poster id if it's the Ideas Bot |
||
| 110 | $sql = 'UPDATE ' . $this->table_prefix . 'posts' . ' |
||
| 111 | SET poster_id = ' . (int) $data['topic_poster_id'] . ' |
||
| 112 | WHERE post_id = ' . (int) $data['topic_first_post_id'] . ' |
||
| 113 | AND poster_id = ' . (int) $this->config->offsetGet('ideas_poster_id'); |
||
| 114 | $this->db->sql_query($sql); |
||
| 115 | } |
||
| 116 | $this->db->sql_transaction('commit'); |
||
| 117 | } |
||
| 119 |