Total Complexity | 52 |
Total Lines | 414 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Mentions 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 Mentions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Mentions |
||
20 | { |
||
21 | /** |
||
22 | * @var string The character used for mentioning users |
||
23 | */ |
||
24 | protected static $char = '@'; |
||
25 | |||
26 | /** |
||
27 | * Returns mentions for a specific content |
||
28 | * |
||
29 | * @static |
||
30 | * @access public |
||
31 | * @param string $content_type The content type |
||
32 | * @param int $content_id The ID of the desired content |
||
33 | * @param array $members Whether to limit to a specific set of members |
||
34 | * @return array An array of arrays containing info about each member mentioned |
||
35 | */ |
||
36 | public static function getMentionsByContent($content_type, $content_id, array $members = array()) |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Inserts mentioned members |
||
75 | * |
||
76 | * @static |
||
77 | * @access public |
||
78 | * @param string $content_type The content type |
||
79 | * @param int $content_id The ID of the specified content |
||
80 | * @param array $members An array of members who have been mentioned |
||
81 | * @param int $id_member The ID of the member who mentioned them |
||
82 | */ |
||
83 | public static function insertMentions($content_type, $content_id, array $members, $id_member) |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Updates list of mentioned members. |
||
100 | * |
||
101 | * Intended for use when a post is modified. |
||
102 | * |
||
103 | * @static |
||
104 | * @access public |
||
105 | * @param string $content_type The content type |
||
106 | * @param int $content_id The ID of the specified content |
||
107 | * @param array $members An array of members who have been mentioned |
||
108 | * @param int $id_member The ID of the member who mentioned them |
||
109 | * @return array An array of unchanged, removed, and added member IDs. |
||
110 | */ |
||
111 | public static function modifyMentions($content_type, $content_id, array $members, $id_member) |
||
112 | { |
||
113 | global $smcFunc; |
||
114 | |||
115 | $existing_members = self::getMentionsByContent($content_type, $content_id); |
||
116 | |||
117 | $members_to_remove = array_diff_key($existing_members, $members); |
||
118 | $members_to_insert = array_diff_key($members, $existing_members); |
||
119 | $members_unchanged = array_diff_key($existing_members, $members_to_remove, $members_to_insert); |
||
120 | |||
121 | // Delete mentions from the table that have been deleted in the content. |
||
122 | if (!empty($members_to_remove)) |
||
123 | $smcFunc['db_query']('', ' |
||
124 | DELETE FROM {db_prefix}mentions |
||
125 | WHERE content_type = {string:type} |
||
126 | AND content_id = {int:id} |
||
127 | AND id_mentioned IN ({array_int:members})', |
||
128 | array( |
||
129 | 'type' => $content_type, |
||
130 | 'id' => $content_id, |
||
131 | 'members' => array_keys($members_to_remove), |
||
132 | ) |
||
133 | ); |
||
134 | |||
135 | // Insert any new mentions. |
||
136 | if (!empty($members_to_insert)) |
||
137 | self::insertMentions($content_type, $content_id, $members_to_insert, $id_member); |
||
138 | |||
139 | return array( |
||
140 | 'unchanged' => $members_unchanged, |
||
141 | 'removed' => $members_to_remove, |
||
142 | 'added' => $members_to_insert, |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Gets appropriate mentions replaced in the body |
||
148 | * |
||
149 | * @static |
||
150 | * @access public |
||
151 | * @param string $body The text to look for mentions in |
||
152 | * @param array $members An array of arrays containing info about members (each should have 'id' and 'member') |
||
153 | * @return string The body with mentions replaced |
||
154 | */ |
||
155 | public static function getBody($body, array $members) |
||
156 | { |
||
157 | if (empty($body)) |
||
158 | return $body; |
||
159 | |||
160 | foreach ($members as $member) |
||
161 | $body = str_ireplace(static::$char . $member['real_name'], '[member=' . $member['id'] . ']' . $member['real_name'] . '[/member]', $body); |
||
162 | |||
163 | return $body; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Takes a piece of text and finds all the mentioned members in it |
||
168 | * |
||
169 | * @static |
||
170 | * @access public |
||
171 | * @param string $body The body to get mentions from |
||
172 | * @return array An array of arrays containing members who were mentioned (each has 'id_member' and 'real_name') |
||
173 | */ |
||
174 | public static function getMentionedMembers($body) |
||
175 | { |
||
176 | global $smcFunc; |
||
177 | |||
178 | if (empty($body)) |
||
179 | return array(); |
||
180 | |||
181 | $possible_names = self::getPossibleMentions($body); |
||
182 | $existing_mentions = self::getExistingMentions($body); |
||
183 | |||
184 | if ((empty($possible_names) && empty($existing_mentions)) || !allowedTo('mention')) |
||
185 | return array(); |
||
186 | |||
187 | // Make sure we don't pass empty arrays to the query. |
||
188 | if (empty($existing_mentions)) |
||
189 | $existing_mentions = array(0 => ''); |
||
190 | if (empty($possible_names)) |
||
191 | $possible_names = $existing_mentions; |
||
192 | |||
193 | $request = $smcFunc['db_query']('', ' |
||
194 | SELECT id_member, real_name |
||
195 | FROM {db_prefix}members |
||
196 | WHERE id_member IN ({array_int:ids}) |
||
197 | OR real_name IN ({array_string:names}) |
||
198 | ORDER BY LENGTH(real_name) DESC |
||
199 | LIMIT {int:count}', |
||
200 | array( |
||
201 | 'ids' => array_keys($existing_mentions), |
||
202 | 'names' => $possible_names, |
||
203 | 'count' => count($possible_names), |
||
204 | ) |
||
205 | ); |
||
206 | $members = array(); |
||
207 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
208 | { |
||
209 | if (!isset($existing_mentions[$row['id_member']]) && stripos($body, static::$char . $row['real_name']) === false) |
||
210 | continue; |
||
211 | |||
212 | $members[$row['id_member']] = array( |
||
213 | 'id' => $row['id_member'], |
||
214 | 'real_name' => $row['real_name'], |
||
215 | ); |
||
216 | } |
||
217 | $smcFunc['db_free_result']($request); |
||
218 | |||
219 | return $members; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Parses a body in order to see if there are any mentions, returns possible mention names |
||
224 | * |
||
225 | * Names are tagged by "@<username>" format in post, but they can contain |
||
226 | * any type of character up to 60 characters length. So we extract, starting from @ |
||
227 | * up to 60 characters in length (or if we encounter a line break) and make |
||
228 | * several combination of strings after splitting it by anything that's not a word and join |
||
229 | * by having the first word, first and second word, first, second and third word and so on and |
||
230 | * search every name. |
||
231 | * |
||
232 | * One potential problem with this is something like "@Admin Space" can match |
||
233 | * "Admin Space" as well as "Admin", so we sort by length in descending order. |
||
234 | * One disadvantage of this is that we can only match by one column, hence I've chosen |
||
235 | * real_name since it's the most obvious. |
||
236 | * |
||
237 | * If there's an @ symbol within the name, it is counted in the ongoing string and a new |
||
238 | * combination string is started from it as well in order to account for all the possibilities. |
||
239 | * This makes the @ symbol to not be required to be escaped |
||
240 | * |
||
241 | * @static |
||
242 | * @access protected |
||
243 | * @param string $body The text to look for mentions in |
||
244 | * @return array An array of names of members who have been mentioned |
||
245 | */ |
||
246 | protected static function getPossibleMentions($body) |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Like getPossibleMentions(), but for `[member=1]name[/member]` format. |
||
307 | * |
||
308 | * @static |
||
309 | * @access public |
||
310 | * @param string $body The text to look for mentions in. |
||
311 | * @param array $members An array of arrays containing info about members (each should have 'id' and 'member'). |
||
312 | * @return array An array of arrays containing info about members that are in fact mentioned in the body. |
||
313 | */ |
||
314 | public static function getExistingMentions($body) |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Verifies that members really are mentioned in the text. |
||
331 | * |
||
332 | * This function assumes the incoming text has already been processed by |
||
333 | * the Mentions::getBody() function. |
||
334 | * |
||
335 | * @static |
||
336 | * @access public |
||
337 | * @param string $body The text to look for mentions in. |
||
338 | * @param array $members An array of arrays containing info about members (each should have 'id' and 'member'). |
||
339 | * @return array An array of arrays containing info about members that are in fact mentioned in the body. |
||
340 | */ |
||
341 | public static function verifyMentionedMembers($body, array $members) |
||
342 | { |
||
343 | if (empty($body)) |
||
344 | return array(); |
||
345 | |||
346 | // Don't include mentions inside quotations. |
||
347 | $body = preg_replace('~\[quote[^\]]*\](?' . '>(?' . '>[^\[]|\[(?!/?quote[^\]]*\]))|(?0))*\[/quote\]~', '', $body); |
||
348 | |||
349 | foreach ($members as $member) |
||
350 | { |
||
351 | if (strpos($body, '[member=' . $member['id'] . ']' . $member['real_name'] . '[/member]') === false) |
||
352 | unset($members[$member['id']]); |
||
353 | } |
||
354 | |||
355 | return $members; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Retrieves info about the authors of posts quoted in a block of text. |
||
360 | * |
||
361 | * @static |
||
362 | * @access public |
||
363 | * @param string $body A block of text, such as the body of a post. |
||
364 | * @param int $poster_id The member ID of the author of the text. |
||
365 | * @return array Info about any members who were quoted. |
||
366 | */ |
||
367 | public static function getQuotedMembers($body, $poster_id) |
||
433 | } |
||
434 | |||
435 | } |
||
436 | |||
437 | ?> |