Complex classes like Conversation 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Conversation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Conversation extends UrlModel implements NamedModel |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The subject of the conversation |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $subject; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The time of the last message to the conversation |
||
| 23 | * @var TimeDate |
||
| 24 | */ |
||
| 25 | protected $last_activity; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The id of the creator of the conversation |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | protected $creator; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The status of the conversation |
||
| 35 | * |
||
| 36 | * Can be 'active', 'disabled', 'deleted' or 'reported' |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $status; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The name of the database table used for queries |
||
| 43 | */ |
||
| 44 | const TABLE = "conversations"; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | 1 | protected function assignResult($conversation) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Get the subject of the discussion |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | **/ |
||
| 62 | 1 | public function getSubject() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Get the creator of the discussion |
||
| 69 | * |
||
| 70 | * @return Player |
||
| 71 | */ |
||
| 72 | public function getCreator() |
||
| 73 | { |
||
| 74 | return Player::get($this->creator); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Determine whether a player is the one who created the message conversation |
||
| 79 | * |
||
| 80 | * @param int $id The ID of the player to test for |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public function isCreator($id) |
||
| 84 | { |
||
| 85 | return $this->creator == $id; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | public function isEditor($player) |
||
| 92 | { |
||
| 93 | return $this->isCreator($player->getId()); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get the time when the conversation was most recently active |
||
| 98 | * |
||
| 99 | * @return TimeDate |
||
| 100 | */ |
||
| 101 | public function getLastActivity() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Update the conversation's last activity timestamp |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | 1 | public function updateLastActivity() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Update the conversation's subject |
||
| 119 | * |
||
| 120 | * @param string $subject The new subject |
||
| 121 | * @return self |
||
| 122 | */ |
||
| 123 | public function setSubject($subject) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the last message of the conversation |
||
| 130 | * |
||
| 131 | * @return Message |
||
| 132 | */ |
||
| 133 | public function getLastMessage() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Find whether the last message in the conversation has been read by a player |
||
| 146 | * |
||
| 147 | * @param int $playerId The ID of the player |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isReadBy($playerId) |
||
| 151 | { |
||
| 152 | $query = $this->db->query("SELECT `read` FROM `player_conversations` WHERE `player` = ? AND `conversation` = ?", |
||
| 153 | array($playerId, $this->id)); |
||
| 154 | |||
| 155 | return $query[0]['read'] == 1; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Mark the last message in the conversation as having been read by a player |
||
| 160 | * |
||
| 161 | * @param int $playerId The ID of the player |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | 1 | public function markReadBy($playerId) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Mark the last message in the conversation as unread by the conversation's members |
||
| 174 | * |
||
| 175 | * @param int $except The ID of a player to exclude |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | public function markUnread($except) |
||
| 179 | { |
||
| 180 | $this->db->execute( |
||
| 181 | "UPDATE `player_conversations` SET `read` = 0 WHERE `conversation` = ? AND `player` != ?", |
||
| 182 | array($this->id, $except) |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | 1 | public static function getRouteName($action = 'show') |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | 1 | public static function getActiveStatuses() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public function getName() |
||
| 206 | { |
||
| 207 | return $this->getSubject(); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get a list containing each member of the conversation |
||
| 212 | * @param int|null $hide The ID of a player to ignore |
||
| 213 | * @return Model[] An array of players and teams |
||
| 214 | */ |
||
| 215 | public function getMembers($hide = null) |
||
| 216 | { |
||
| 217 | $members = Player::arrayIdToModel($this->getPlayerIds($hide, true)); |
||
| 218 | usort($members, Player::getAlphabeticalSort()); |
||
| 219 | |||
| 220 | $teams = Team::arrayIdToModel($this->getTeamIds()); |
||
| 221 | usort($teams, Team::getAlphabeticalSort()); |
||
| 222 | |||
| 223 | return array_merge($members, $teams); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get the members of one of the conversation's teams that don't belong in |
||
| 228 | * the conversation |
||
| 229 | * |
||
| 230 | * @todo Use Model::createFromDatabaseResults() |
||
| 231 | * @param Team $team The team to check |
||
| 232 | * @return Player[] |
||
| 233 | */ |
||
| 234 | public function getMissingTeamMembers(Team $team) |
||
| 235 | { |
||
| 236 | $query = "SELECT players.id AS id FROM players |
||
| 237 | WHERE players.team = ? |
||
| 238 | AND players.id NOT IN ( |
||
| 239 | SELECT player_conversations.player FROM player_conversations |
||
| 240 | WHERE player_conversations.conversation = ? |
||
| 241 | )"; |
||
| 242 | |||
| 243 | $results = $this->db->query($query, array($team->getId(), $this->id)); |
||
| 244 | |||
| 245 | return Player::arrayIdToModel(array_column($results, 'id')); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get a list containing the IDs of each member player of the conversation |
||
| 250 | * @param int|null $hide The ID of a player to ignore |
||
| 251 | * @param bool $distinct Whether to only return players who were |
||
| 252 | * specifically invited to the conversation, and |
||
| 253 | * are not participating only as members of a team |
||
| 254 | * @return int[] An array of player IDs |
||
| 255 | */ |
||
| 256 | public function getPlayerIds($hide = null, $distinct = false) |
||
| 257 | { |
||
| 258 | $additional_query = "WHERE `conversation` = ?"; |
||
| 259 | $params = array($this->id); |
||
| 260 | |||
| 261 | if ($hide) { |
||
|
|
|||
| 262 | $additional_query .= " AND `player` != ?"; |
||
| 263 | $params[] = $hide; |
||
| 264 | } |
||
| 265 | |||
| 266 | if ($distinct) { |
||
| 267 | $additional_query .= " AND `distinct` = 1"; |
||
| 268 | } |
||
| 269 | |||
| 270 | return self::fetchIds($additional_query, $params, "player_conversations", "player"); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get a list containing the IDs of each member team of the conversation |
||
| 275 | * |
||
| 276 | * @return int[] An array of team IDs |
||
| 277 | */ |
||
| 278 | public function getTeamIds() |
||
| 279 | { |
||
| 280 | return self::fetchIds("WHERE `conversation` = ?", $this->id, "team_conversations", "team"); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Create a new message conversation |
||
| 285 | ** |
||
| 286 | * @param string $subject The subject of the conversation |
||
| 287 | * @param int $creatorId The ID of the player who created the conversation |
||
| 288 | * @param array $members A list of Models representing the conversation's members |
||
| 289 | * @return Conversation An object that represents the created conversation |
||
| 290 | */ |
||
| 291 | 1 | public static function createConversation($subject, $creatorId, $members = array()) |
|
| 292 | { |
||
| 293 | 1 | $conversation = self::create(array( |
|
| 294 | 1 | 'subject' => $subject, |
|
| 295 | 1 | 'creator' => $creatorId, |
|
| 296 | 1 | 'status' => "active", |
|
| 297 | 1 | ), 'last_activity'); |
|
| 298 | |||
| 299 | 1 | Database::getInstance()->startTransaction(); |
|
| 300 | 1 | foreach ($members as $member) { |
|
| 301 | 1 | $conversation->addMember($member); |
|
| 302 | } |
||
| 303 | 1 | Database::getInstance()->finishTransaction(); |
|
| 304 | |||
| 305 | 1 | return $conversation; |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Send a new message to the conversation's members |
||
| 310 | * @param Player $from The sender |
||
| 311 | * @param string $message The body of the message |
||
| 312 | * @param string $status The status of the message - can be 'visible', 'hidden', 'deleted' or 'reported' |
||
| 313 | * @return Message An object that represents the sent message |
||
| 314 | */ |
||
| 315 | 1 | public function sendMessage($from, $message, $status = 'visible') |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Checks if a player or team belongs in the conversation |
||
| 326 | * @param Player|Team $member The player or team to check |
||
| 327 | * @param bool $distinct Whether to only return true if a player is |
||
| 328 | * specifically a member of the conversation, not |
||
| 329 | * just a member of one of the conversation's teams (ignored if $member is a Team) |
||
| 330 | * @return bool True if the given object belongs in the conversation, false if they don't |
||
| 331 | */ |
||
| 332 | 1 | public function isMember($member, $distinct = false) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Add a member to the discussion |
||
| 352 | * |
||
| 353 | * @param Player|Team $member The member to add |
||
| 354 | * @param bool $distinct Whether to add the member as a distinct |
||
| 355 | * player (ignored for teams) |
||
| 356 | * @return void |
||
| 357 | */ |
||
| 358 | 1 | public function addMember($member, $distinct = true) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Find out if a player belongs to any of the conversation's teams |
||
| 391 | * |
||
| 392 | * This does not take into account whether the player is a distinct member |
||
| 393 | * of the conversation (i.e. they have been invited separately) |
||
| 394 | * |
||
| 395 | * @param Player $member The player to check |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | public function isTeamMember($member) |
||
| 399 | { |
||
| 400 | $query = $this->db->query( |
||
| 401 | "SELECT COUNT(*) as c FROM players |
||
| 402 | INNER JOIN teams ON teams.id = players.team |
||
| 403 | INNER JOIN team_conversations ON team_conversations.team = teams.id |
||
| 404 | WHERE team_conversations.conversation = ? |
||
| 405 | AND players.id = ? |
||
| 406 | LIMIT 1", array($this->getId(), $member->getId()) |
||
| 407 | ); |
||
| 408 | |||
| 409 | return $query[0]['c'] > 0; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Remove a member from the discussion |
||
| 414 | * |
||
| 415 | * @param Player|Team $member The member to remove |
||
| 416 | * @return void |
||
| 417 | */ |
||
| 418 | public function removeMember($member) |
||
| 419 | { |
||
| 420 | if ($member instanceof Player) { |
||
| 421 | if ($this->isTeamMember($member) && $member->getTeam()->getLeader()->isSameAs($member)) { |
||
| 422 | // The player is the leader of a team in the conversation, don't |
||
| 423 | // remove them entirely |
||
| 424 | $this->db->execute( |
||
| 425 | "UPDATE `player_conversations` SET `distinct` = 0 WHERE `conversation` = ? AND `player` = ?", array($this->getId(), $member->getId()) |
||
| 426 | ); |
||
| 427 | } else { |
||
| 428 | $this->db->execute( |
||
| 429 | "DELETE FROM `player_conversations` WHERE `conversation` = ? AND `player` = ?", array($this->getId(), $member->getId()) |
||
| 430 | ); |
||
| 431 | } |
||
| 432 | } else { |
||
| 433 | $this->db->execute( |
||
| 434 | "DELETE `player_conversations` FROM `player_conversations` |
||
| 435 | LEFT JOIN `players` ON players.id = player_conversations.player |
||
| 436 | WHERE player_conversations.conversation = ? |
||
| 437 | AND players.team = ? |
||
| 438 | AND player_conversations.distinct = 0", array($this->getId(), $member->getId()) |
||
| 439 | ); |
||
| 440 | |||
| 441 | $this->db->execute( |
||
| 442 | "DELETE FROM `team_conversations` |
||
| 443 | WHERE conversation = ? |
||
| 444 | AND team = ?", array($this->getId(), $member->getId()) |
||
| 445 | ); |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Find out which members of the conversation should receive an e-mail after a new |
||
| 451 | * message has been sent |
||
| 452 | * |
||
| 453 | * @param int $except The ID of a player who won't receive an e-mail |
||
| 454 | * (e.g. message author) |
||
| 455 | * @param bool $read Whether to only send e-mails to players who have |
||
| 456 | * read all the previous messages in the conversation |
||
| 457 | * @return int[] A player ID list |
||
| 458 | */ |
||
| 459 | public function getWaitingForEmailIDs($except, $read = true) |
||
| 460 | { |
||
| 461 | $readQuery = ($read) ? 'AND pg.read = 1' : ''; |
||
| 462 | |||
| 463 | return $this->fetchIds( |
||
| 464 | "LEFT JOIN players ON pg.player = players.id |
||
| 465 | WHERE pg.conversation = ? |
||
| 466 | $readQuery |
||
| 467 | AND pg.player != ? |
||
| 468 | AND players.verified = 1 |
||
| 469 | AND players.receives != \"nothing\"", |
||
| 470 | array($this->id, $except), |
||
| 471 | 'player_conversations AS pg', |
||
| 472 | 'pg.player'); |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Get a query builder for conversations |
||
| 477 | * @return ConversationQueryBuilder |
||
| 478 | */ |
||
| 479 | 1 | public static function getQueryBuilder() |
|
| 489 | } |
||
| 490 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: