| Total Complexity | 61 |
| Total Lines | 454 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 | * @var string Regular expression matching BBC that can't contain mentions |
||
| 28 | */ |
||
| 29 | protected static $excluded_bbc_regex = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Returns mentions for a specific content |
||
| 33 | * |
||
| 34 | * @static |
||
| 35 | * @access public |
||
| 36 | * @param string $content_type The content type |
||
| 37 | * @param int $content_id The ID of the desired content |
||
| 38 | * @param array $members Whether to limit to a specific set of members |
||
| 39 | * @return array An array of arrays containing info about each member mentioned |
||
| 40 | */ |
||
| 41 | public static function getMentionsByContent($content_type, $content_id, array $members = array()) |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Inserts mentioned members |
||
| 83 | * |
||
| 84 | * @static |
||
| 85 | * @access public |
||
| 86 | * @param string $content_type The content type |
||
| 87 | * @param int $content_id The ID of the specified content |
||
| 88 | * @param array $members An array of members who have been mentioned |
||
| 89 | * @param int $id_member The ID of the member who mentioned them |
||
| 90 | */ |
||
| 91 | public static function insertMentions($content_type, $content_id, array $members, $id_member) |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Updates list of mentioned members. |
||
| 108 | * |
||
| 109 | * Intended for use when a post is modified. |
||
| 110 | * |
||
| 111 | * @static |
||
| 112 | * @access public |
||
| 113 | * @param string $content_type The content type |
||
| 114 | * @param int $content_id The ID of the specified content |
||
| 115 | * @param array $members An array of members who have been mentioned |
||
| 116 | * @param int $id_member The ID of the member who mentioned them |
||
| 117 | * @return array An array of unchanged, removed, and added member IDs. |
||
| 118 | */ |
||
| 119 | public static function modifyMentions($content_type, $content_id, array $members, $id_member) |
||
| 120 | { |
||
| 121 | global $smcFunc; |
||
| 122 | |||
| 123 | $existing_members = self::getMentionsByContent($content_type, $content_id); |
||
| 124 | |||
| 125 | $members_to_remove = array_diff_key($existing_members, $members); |
||
| 126 | $members_to_insert = array_diff_key($members, $existing_members); |
||
| 127 | $members_unchanged = array_diff_key($existing_members, $members_to_remove, $members_to_insert); |
||
| 128 | |||
| 129 | // Delete mentions from the table that have been deleted in the content. |
||
| 130 | if (!empty($members_to_remove)) |
||
| 131 | $smcFunc['db_query']('', ' |
||
| 132 | DELETE FROM {db_prefix}mentions |
||
| 133 | WHERE content_type = {string:type} |
||
| 134 | AND content_id = {int:id} |
||
| 135 | AND id_mentioned IN ({array_int:members})', |
||
| 136 | array( |
||
| 137 | 'type' => $content_type, |
||
| 138 | 'id' => $content_id, |
||
| 139 | 'members' => array_keys($members_to_remove), |
||
| 140 | ) |
||
| 141 | ); |
||
| 142 | |||
| 143 | // Insert any new mentions. |
||
| 144 | if (!empty($members_to_insert)) |
||
| 145 | self::insertMentions($content_type, $content_id, $members_to_insert, $id_member); |
||
| 146 | |||
| 147 | return array( |
||
| 148 | 'unchanged' => $members_unchanged, |
||
| 149 | 'removed' => $members_to_remove, |
||
| 150 | 'added' => $members_to_insert, |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Gets appropriate mentions replaced in the body |
||
| 156 | * |
||
| 157 | * @static |
||
| 158 | * @access public |
||
| 159 | * @param string $body The text to look for mentions in |
||
| 160 | * @param array $members An array of arrays containing info about members (each should have 'id' and 'member') |
||
| 161 | * @return string The body with mentions replaced |
||
| 162 | */ |
||
| 163 | public static function getBody($body, array $members) |
||
| 164 | { |
||
| 165 | if (empty($body)) |
||
| 166 | return $body; |
||
| 167 | |||
| 168 | foreach ($members as $member) |
||
| 169 | $body = str_ireplace(static::$char . $member['real_name'], '[member=' . $member['id'] . ']' . $member['real_name'] . '[/member]', $body); |
||
| 170 | |||
| 171 | return $body; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Takes a piece of text and finds all the mentioned members in it |
||
| 176 | * |
||
| 177 | * @static |
||
| 178 | * @access public |
||
| 179 | * @param string $body The body to get mentions from |
||
| 180 | * @return array An array of arrays containing members who were mentioned (each has 'id_member' and 'real_name') |
||
| 181 | */ |
||
| 182 | public static function getMentionedMembers($body) |
||
| 183 | { |
||
| 184 | global $smcFunc; |
||
| 185 | |||
| 186 | if (empty($body)) |
||
| 187 | return array(); |
||
| 188 | |||
| 189 | $possible_names = self::getPossibleMentions($body); |
||
| 190 | $existing_mentions = self::getExistingMentions($body); |
||
| 191 | |||
| 192 | if ((empty($possible_names) && empty($existing_mentions)) || !allowedTo('mention')) |
||
| 193 | return array(); |
||
| 194 | |||
| 195 | // Make sure we don't pass empty arrays to the query. |
||
| 196 | if (empty($existing_mentions)) |
||
| 197 | $existing_mentions = array(0 => ''); |
||
| 198 | if (empty($possible_names)) |
||
| 199 | $possible_names = $existing_mentions; |
||
| 200 | |||
| 201 | $request = $smcFunc['db_query']('', ' |
||
| 202 | SELECT id_member, real_name |
||
| 203 | FROM {db_prefix}members |
||
| 204 | WHERE id_member IN ({array_int:ids}) |
||
| 205 | OR real_name IN ({array_string:names}) |
||
| 206 | ORDER BY LENGTH(real_name) DESC |
||
| 207 | LIMIT {int:count}', |
||
| 208 | array( |
||
| 209 | 'ids' => array_keys($existing_mentions), |
||
| 210 | 'names' => $possible_names, |
||
| 211 | 'count' => count($possible_names), |
||
| 212 | ) |
||
| 213 | ); |
||
| 214 | $members = array(); |
||
| 215 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 216 | { |
||
| 217 | if (!isset($existing_mentions[$row['id_member']]) && stripos($body, static::$char . $row['real_name']) === false) |
||
| 218 | continue; |
||
| 219 | |||
| 220 | $members[$row['id_member']] = array( |
||
| 221 | 'id' => $row['id_member'], |
||
| 222 | 'real_name' => $row['real_name'], |
||
| 223 | ); |
||
| 224 | } |
||
| 225 | $smcFunc['db_free_result']($request); |
||
| 226 | |||
| 227 | return $members; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Parses a body in order to see if there are any mentions, returns possible mention names |
||
| 232 | * |
||
| 233 | * Names are tagged by "@<username>" format in post, but they can contain |
||
| 234 | * any type of character up to 60 characters length. So we extract, starting from @ |
||
| 235 | * up to 60 characters in length (or if we encounter a line break) and make |
||
| 236 | * several combination of strings after splitting it by anything that's not a word and join |
||
| 237 | * by having the first word, first and second word, first, second and third word and so on and |
||
| 238 | * search every name. |
||
| 239 | * |
||
| 240 | * One potential problem with this is something like "@Admin Space" can match |
||
| 241 | * "Admin Space" as well as "Admin", so we sort by length in descending order. |
||
| 242 | * One disadvantage of this is that we can only match by one column, hence I've chosen |
||
| 243 | * real_name since it's the most obvious. |
||
| 244 | * |
||
| 245 | * If there's an @ symbol within the name, it is counted in the ongoing string and a new |
||
| 246 | * combination string is started from it as well in order to account for all the possibilities. |
||
| 247 | * This makes the @ symbol to not be required to be escaped |
||
| 248 | * |
||
| 249 | * @static |
||
| 250 | * @access protected |
||
| 251 | * @param string $body The text to look for mentions in |
||
| 252 | * @return array An array of names of members who have been mentioned |
||
| 253 | */ |
||
| 254 | protected static function getPossibleMentions($body) |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Like getPossibleMentions(), but for `[member=1]name[/member]` format. |
||
| 318 | * |
||
| 319 | * @static |
||
| 320 | * @access public |
||
| 321 | * @param string $body The text to look for mentions in. |
||
| 322 | * @param array $members An array of arrays containing info about members (each should have 'id' and 'member'). |
||
| 323 | * @return array An array of arrays containing info about members that are in fact mentioned in the body. |
||
| 324 | */ |
||
| 325 | public static function getExistingMentions($body) |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Verifies that members really are mentioned in the text. |
||
| 345 | * |
||
| 346 | * This function assumes the incoming text has already been processed by |
||
| 347 | * the Mentions::getBody() function. |
||
| 348 | * |
||
| 349 | * @static |
||
| 350 | * @access public |
||
| 351 | * @param string $body The text to look for mentions in. |
||
| 352 | * @param array $members An array of arrays containing info about members (each should have 'id' and 'member'). |
||
| 353 | * @return array An array of arrays containing info about members that are in fact mentioned in the body. |
||
| 354 | */ |
||
| 355 | public static function verifyMentionedMembers($body, array $members) |
||
| 356 | { |
||
| 357 | if (empty($body)) |
||
| 358 | return array(); |
||
| 359 | |||
| 360 | if (empty(self::$excluded_bbc_regex)) |
||
| 361 | self::setExcludedBbcRegex(); |
||
| 362 | |||
| 363 | // Don't include mentions inside quotations, etc. |
||
| 364 | $body = preg_replace('~\[(' . self::$excluded_bbc_regex . ')[^\]]*\](?' . '>(?' . '>[^\[]|\[(?!/?\1[^\]]*\]))|(?0))*\[/\1\]~', '', $body); |
||
| 365 | |||
| 366 | foreach ($members as $member) |
||
| 367 | { |
||
| 368 | if (strpos($body, '[member=' . $member['id'] . ']' . $member['real_name'] . '[/member]') === false) |
||
| 369 | unset($members[$member['id']]); |
||
| 370 | } |
||
| 371 | |||
| 372 | return $members; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Retrieves info about the authors of posts quoted in a block of text. |
||
| 377 | * |
||
| 378 | * @static |
||
| 379 | * @access public |
||
| 380 | * @param string $body A block of text, such as the body of a post. |
||
| 381 | * @param int $poster_id The member ID of the author of the text. |
||
| 382 | * @return array Info about any members who were quoted. |
||
| 383 | */ |
||
| 384 | public static function getQuotedMembers($body, $poster_id) |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Builds a regular expression matching BBC that can't contain mentions. |
||
| 454 | * |
||
| 455 | * @static |
||
| 456 | * @access protected |
||
| 457 | */ |
||
| 458 | protected static function setExcludedBbcRegex() |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | ?> |