Total Complexity | 40 |
Total Lines | 457 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like idea often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use idea, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class idea extends base |
||
19 | { |
||
20 | /** |
||
21 | * Returns the specified idea. |
||
22 | * |
||
23 | * @param int $id The ID of the idea to return. |
||
24 | * |
||
25 | * @return array|false The idea row set, or false if not found. |
||
26 | */ |
||
27 | public function get_idea($id) |
||
28 | { |
||
29 | $sql = 'SELECT * |
||
30 | FROM ' . $this->table_ideas . ' |
||
31 | WHERE idea_id = ' . (int) $id; |
||
32 | $result = $this->db->sql_query_limit($sql, 1); |
||
33 | $row = $this->db->sql_fetchrow($result); |
||
34 | $this->db->sql_freeresult($result); |
||
35 | |||
36 | return $row; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Returns an idea specified by its topic ID. |
||
41 | * |
||
42 | * @param int $id The ID of the idea to return. |
||
43 | * |
||
44 | * @return array|false The idea row set, or false if not found. |
||
45 | */ |
||
46 | public function get_idea_by_topic_id($id) |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Updates the status of an idea. |
||
60 | * |
||
61 | * @param int $idea_id The ID of the idea. |
||
62 | * @param int $status The ID of the status. |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function set_status($idea_id, $status) |
||
67 | { |
||
68 | $sql_ary = array( |
||
69 | 'idea_status' => (int) $status, |
||
70 | ); |
||
71 | |||
72 | $this->update_idea_data($sql_ary, $idea_id, $this->table_ideas); |
||
73 | |||
74 | // Send a notification |
||
75 | $method = $this->notification_exists($idea_id) ? 'update_notifications' : 'add_notifications'; |
||
76 | $this->notification_manager->$method('phpbb.ideas.notification.type.status', [ |
||
77 | 'idea_id' => (int) $idea_id, |
||
78 | 'status' => (int) $status, |
||
79 | ]); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Sets the ID of the duplicate for an idea. |
||
84 | * |
||
85 | * @param int $idea_id ID of the idea to be updated. |
||
86 | * @param string $duplicate Idea ID of duplicate. |
||
87 | * |
||
88 | * @return bool True if set, false if invalid. |
||
89 | */ |
||
90 | public function set_duplicate($idea_id, $duplicate) |
||
91 | { |
||
92 | if ($duplicate && !is_numeric($duplicate)) |
||
93 | { |
||
94 | return false; |
||
95 | } |
||
96 | |||
97 | $sql_ary = array( |
||
98 | 'duplicate_id' => (int) $duplicate, |
||
99 | ); |
||
100 | |||
101 | $this->update_idea_data($sql_ary, $idea_id, $this->table_ideas); |
||
102 | |||
103 | return true; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Sets the RFC link of an idea. |
||
108 | * |
||
109 | * @param int $idea_id ID of the idea to be updated. |
||
110 | * @param string $rfc Link to the RFC. |
||
111 | * |
||
112 | * @return bool True if set, false if invalid. |
||
113 | */ |
||
114 | public function set_rfc($idea_id, $rfc) |
||
115 | { |
||
116 | $match = '/^https?:\/\/area51\.phpbb\.com\/phpBB\/viewtopic\.php/'; |
||
117 | if ($rfc && !preg_match($match, $rfc)) |
||
118 | { |
||
119 | return false; |
||
120 | } |
||
121 | |||
122 | $sql_ary = array( |
||
123 | 'rfc_link' => $rfc, // string is escaped by build_array() |
||
124 | ); |
||
125 | |||
126 | $this->update_idea_data($sql_ary, $idea_id, $this->table_ideas); |
||
127 | |||
128 | return true; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Sets the ticket ID of an idea. |
||
133 | * |
||
134 | * @param int $idea_id ID of the idea to be updated. |
||
135 | * @param string $ticket Ticket ID. |
||
136 | * |
||
137 | * @return bool True if set, false if invalid. |
||
138 | */ |
||
139 | public function set_ticket($idea_id, $ticket) |
||
140 | { |
||
141 | if ($ticket && !is_numeric($ticket)) |
||
142 | { |
||
143 | return false; |
||
144 | } |
||
145 | |||
146 | $sql_ary = array( |
||
147 | 'ticket_id' => (int) $ticket, |
||
148 | ); |
||
149 | |||
150 | $this->update_idea_data($sql_ary, $idea_id, $this->table_ideas); |
||
151 | |||
152 | return true; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Sets the implemented version of an idea. |
||
157 | * |
||
158 | * @param int $idea_id ID of the idea to be updated. |
||
159 | * @param string $version Version of phpBB the idea was implemented in. |
||
160 | * |
||
161 | * @return bool True if set, false if invalid. |
||
162 | */ |
||
163 | public function set_implemented($idea_id, $version) |
||
164 | { |
||
165 | $match = '/^\d\.\d\.\d+(-\w+)?$/'; |
||
166 | if ($version && !preg_match($match, $version)) |
||
167 | { |
||
168 | return false; |
||
169 | } |
||
170 | |||
171 | $sql_ary = array( |
||
172 | 'implemented_version' => $version, // string is escaped by build_array() |
||
173 | ); |
||
174 | |||
175 | $this->update_idea_data($sql_ary, $idea_id, $this->table_ideas); |
||
176 | |||
177 | return true; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Sets the title of an idea. |
||
182 | * |
||
183 | * @param int $idea_id ID of the idea to be updated. |
||
184 | * @param string $title New title. |
||
185 | * |
||
186 | * @return boolean True if updated, false if invalid length. |
||
187 | */ |
||
188 | public function set_title($idea_id, $title) |
||
189 | { |
||
190 | if (utf8_clean_string($title) === '') |
||
191 | { |
||
192 | return false; |
||
193 | } |
||
194 | |||
195 | $sql_ary = array( |
||
196 | 'idea_title' => truncate_string($title, ext::SUBJECT_LENGTH), |
||
197 | ); |
||
198 | |||
199 | $this->update_idea_data($sql_ary, $idea_id, $this->table_ideas); |
||
200 | |||
201 | return true; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get the title of an idea. |
||
206 | * |
||
207 | * @param int $id ID of an idea |
||
208 | * |
||
209 | * @return string The idea's title, empty string if not found |
||
210 | */ |
||
211 | public function get_title($id) |
||
212 | { |
||
213 | $sql = 'SELECT idea_title |
||
214 | FROM ' . $this->table_ideas . ' |
||
215 | WHERE idea_id = ' . (int) $id; |
||
216 | $result = $this->db->sql_query_limit($sql, 1); |
||
217 | $idea_title = $this->db->sql_fetchfield('idea_title'); |
||
218 | $this->db->sql_freeresult($result); |
||
219 | |||
220 | return $idea_title ?: ''; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Submit new idea data to the ideas table |
||
225 | * |
||
226 | * @param array $data An array of post data from a newly posted idea |
||
227 | * |
||
228 | * @return int The ID of the new idea. |
||
229 | */ |
||
230 | public function submit($data) |
||
231 | { |
||
232 | $sql_ary = [ |
||
233 | 'idea_title' => $data['topic_title'], |
||
234 | 'idea_author' => $data['poster_id'], |
||
235 | 'idea_date' => $data['post_time'], |
||
236 | 'topic_id' => $data['topic_id'], |
||
237 | ]; |
||
238 | |||
239 | $idea_id = $this->insert_idea_data($sql_ary, $this->table_ideas); |
||
240 | |||
241 | // Initial vote |
||
242 | if (($idea = $this->get_idea($idea_id)) !== false) |
||
243 | { |
||
244 | $this->vote($idea, $data['poster_id'], 1); |
||
245 | } |
||
246 | |||
247 | return $idea_id; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Deletes an idea and the topic to go with it. |
||
252 | * |
||
253 | * @param int $id The ID of the idea to be deleted. |
||
254 | * @param int $topic_id The ID of the idea topic. Optional, but preferred. |
||
255 | * |
||
256 | * @return boolean Whether the idea was deleted or not. |
||
257 | */ |
||
258 | public function delete($id, $topic_id = 0) |
||
259 | { |
||
260 | if (!$topic_id) |
||
261 | { |
||
262 | $idea = $this->get_idea($id); |
||
263 | $topic_id = $idea ? $idea['topic_id'] : 0; |
||
264 | } |
||
265 | |||
266 | // Delete topic |
||
267 | delete_posts('topic_id', $topic_id); |
||
268 | |||
269 | // Delete idea |
||
270 | $deleted = $this->delete_idea_data($id, $this->table_ideas); |
||
271 | |||
272 | if ($deleted) |
||
273 | { |
||
274 | // Delete votes |
||
275 | $this->delete_idea_data($id, $this->table_votes); |
||
276 | |||
277 | // Delete notifications |
||
278 | $this->notification_manager->delete_notifications('phpbb.ideas.notification.type.status', $id); |
||
279 | } |
||
280 | |||
281 | return $deleted; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Submits a vote on an idea. |
||
286 | * |
||
287 | * @param array $idea The idea returned by get_idea(). |
||
288 | * @param int $user_id The ID of the user voting. |
||
289 | * @param int $value Up (1) or down (0)? |
||
290 | * |
||
291 | * @return array|string Array of information or string on error. |
||
292 | */ |
||
293 | public function vote(&$idea, $user_id, $value) |
||
367 | ); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Remove a user's vote from an idea |
||
372 | * |
||
373 | * @param array $idea The idea returned by get_idea(). |
||
374 | * @param int $user_id The ID of the user voting. |
||
375 | * |
||
376 | * @return array Array of information. |
||
377 | */ |
||
378 | public function remove_vote(&$idea, $user_id) |
||
379 | { |
||
380 | // Only change something if user has already voted |
||
381 | if ($row = $this->get_users_vote($idea['idea_id'], $user_id)) |
||
382 | { |
||
383 | $sql = 'DELETE FROM ' . $this->table_votes . ' |
||
384 | WHERE idea_id = ' . (int) $idea['idea_id'] . ' |
||
385 | AND user_id = ' . (int) $user_id; |
||
386 | $this->db->sql_query($sql); |
||
387 | |||
388 | $idea['idea_votes_' . ($row['vote_value'] == 1 ? 'up' : 'down')]--; |
||
389 | |||
390 | $sql_ary = array( |
||
391 | 'idea_votes_up' => $idea['idea_votes_up'], |
||
392 | 'idea_votes_down' => $idea['idea_votes_down'], |
||
393 | ); |
||
394 | |||
395 | $this->update_idea_data($sql_ary, $idea['idea_id'], $this->table_ideas); |
||
396 | } |
||
397 | |||
398 | return array( |
||
399 | 'message' => $this->language->lang('UPDATED_VOTE'), |
||
400 | 'votes_up' => $idea['idea_votes_up'], |
||
401 | 'votes_down' => $idea['idea_votes_down'], |
||
402 | 'points' => $this->language->lang('TOTAL_POINTS', $idea['idea_votes_up'] - $idea['idea_votes_down']), |
||
403 | 'voters' => $this->get_voters($idea['idea_id']), |
||
404 | ); |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Returns voter info on an idea. |
||
409 | * |
||
410 | * @param int $id ID of the idea. |
||
411 | * |
||
412 | * @return array Array of row data |
||
413 | */ |
||
414 | public function get_voters($id) |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * Get a user's stored vote value for a given idea |
||
439 | * |
||
440 | * @param int $idea_id The idea id |
||
441 | * @param int $user_id The user id |
||
442 | * @return mixed Array with the row data, false if the row does not exist |
||
443 | */ |
||
444 | protected function get_users_vote($idea_id, $user_id) |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Check if a notification already exists |
||
459 | * |
||
460 | * @param int $item_id The item identifier for the notification |
||
461 | * @return bool |
||
462 | */ |
||
463 | protected function notification_exists($item_id) |
||
475 | } |
||
476 | } |
||
477 |