Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 13 | class Chat implements MessageComponentInterface |
||
| 14 | { |
||
| 15 | /** @var ConnectionInterface[] */ |
||
| 16 | private $clients = []; |
||
| 17 | /** @var \jones\wschat\components\ChatManager */ |
||
| 18 | private $cm = null; |
||
| 19 | /** |
||
| 20 | * @var array list of available requests |
||
| 21 | */ |
||
| 22 | private $requests = [ |
||
| 23 | 'auth', 'message' |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param \jones\wschat\components\ChatManager $cm |
||
| 28 | */ |
||
| 29 | public function __construct(ChatManager $cm) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param ConnectionInterface $conn |
||
| 36 | */ |
||
| 37 | public function onOpen(ConnectionInterface $conn) |
||
| 38 | { |
||
| 39 | $rid = $this->getResourceId($conn); |
||
| 40 | $this->clients[$rid] = $conn; |
||
| 41 | Yii::info('Connection is established: '.$rid, 'chat'); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param ConnectionInterface $from |
||
| 46 | * @param string $msg |
||
| 47 | */ |
||
| 48 | public function onMessage(ConnectionInterface $from, $msg) |
||
| 49 | { |
||
| 50 | $data = Json::decode($msg, true); |
||
| 51 | $rid = array_search($from, $this->clients); |
||
| 52 | if (in_array($data['type'], $this->requests)) { |
||
| 53 | call_user_func_array([$this, $data['type'].'Request'], [$rid, $data['data']]); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param ConnectionInterface $conn |
||
| 59 | */ |
||
| 60 | public function onClose(ConnectionInterface $conn) |
||
| 61 | { |
||
| 62 | $rid = array_search($conn, $this->clients); |
||
| 63 | if ($this->cm->getUserByRid($rid)) { |
||
| 64 | $this->closeRequest($rid); |
||
| 65 | } |
||
| 66 | unset($this->clients[$rid]); |
||
| 67 | Yii::info('Connection is closed: '.$rid, 'chat'); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param ConnectionInterface $conn |
||
| 72 | * @param \Exception $e |
||
| 73 | */ |
||
| 74 | public function onError(ConnectionInterface $conn, \Exception $e) |
||
| 75 | { |
||
| 76 | Yii::error($e->getMessage()); |
||
| 77 | $conn->send(Json::encode(['type' => 'error', 'data' => [ |
||
| 78 | 'message' => Yii::t('app', 'Something wrong. Connection will be closed') |
||
| 79 | ]])); |
||
| 80 | $conn->close(); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get connection resource id |
||
| 85 | * |
||
| 86 | * @access private |
||
| 87 | * @param ConnectionInterface $conn |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | private function getResourceId(ConnectionInterface $conn) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Process auth request. Find user chat(if not exists - create it) |
||
| 97 | * and send message to all other clients |
||
| 98 | * |
||
| 99 | * @access private |
||
| 100 | * @param $rid |
||
| 101 | * @param $data |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | private function authRequest($rid, array $data) |
||
| 105 | { |
||
| 106 | $chatId = $data['cid']; |
||
| 107 | Yii::info('Auth request from rid: '.$rid.' and chat: '.$chatId, 'chat'); |
||
| 108 | $userId = !empty($data['user']['id']) ? $data['user']['id'] : ''; |
||
| 109 | //the same user already connected to current chat, need to close old connect |
||
| 110 | if ($oldRid = $this->cm->isUserExistsInChat($userId, $chatId)) { |
||
| 111 | $this->closeRequest($oldRid); |
||
| 112 | } |
||
| 113 | $this->cm->addUser($rid, $userId, $data['user']); |
||
| 114 | $chat = $this->cm->findChat($chatId, $rid); |
||
| 115 | $users = $chat->getUsers(); |
||
| 116 | $joinedUser = $this->cm->getUserByRid($rid); |
||
| 117 | $response = [ |
||
| 118 | 'user' => $joinedUser, |
||
| 119 | 'join' => true, |
||
| 120 | ]; |
||
| 121 | View Code Duplication | foreach ($users as $user) { |
|
| 122 | //send message for other users of this chat |
||
| 123 | if ($userId != $user->getId()) { |
||
| 124 | $conn = $this->clients[$user->getRid()]; |
||
| 125 | $conn->send(Json::encode(['type' => 'auth', 'data' => $response])); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | //send auth response for joined user |
||
| 129 | $response = [ |
||
| 130 | 'user' => $joinedUser, |
||
| 131 | 'users' => $users, |
||
| 132 | 'history' => $this->cm->getHistory($chat->getUid()) |
||
| 133 | ]; |
||
| 134 | $conn = $this->clients[$rid]; |
||
| 135 | $conn->send(Json::encode(['type' => 'auth', 'data' => $response])); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Process message request. Find user chat room and send message to other users |
||
| 140 | * in this chat room |
||
| 141 | * |
||
| 142 | * @access private |
||
| 143 | * @param $rid |
||
| 144 | * @param array $data |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | private function messageRequest($rid, array $data) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Process close request. Find user chat, remove user from chat and send message |
||
| 169 | * to other users in this chat |
||
| 170 | * |
||
| 171 | * @access public |
||
| 172 | * @param $rid |
||
| 173 | */ |
||
| 174 | private function closeRequest($rid) |
||
| 192 | } |
||
| 193 | |||
| 195 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: