| Conditions | 11 |
| Paths | 96 |
| Total Lines | 142 |
| Code Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 57 | #[Route(path: '/resources/chat/conversations/', name: 'chamilo_core_chat_ajax', options: ['expose' => true])] |
||
| 58 | public function ajax(Request $request, ManagerRegistry $doctrine): Response |
||
| 59 | { |
||
| 60 | $debug = false; |
||
| 61 | $log = function (string $msg, array $ctx = []) use ($debug): void { |
||
| 62 | if (!$debug) { |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | error_log('[ChatController] '.$msg.' | '.json_encode($ctx, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); |
||
| 66 | }; |
||
| 67 | |||
| 68 | if (!api_protect_course_script()) { |
||
| 69 | $log('protect.failed'); |
||
| 70 | |||
| 71 | return new JsonResponse(['status' => false, 'error' => 'forbidden'], 403); |
||
| 72 | } |
||
| 73 | |||
| 74 | $courseId = api_get_course_int_id(); |
||
| 75 | $userId = api_get_user_id(); |
||
| 76 | $sessionId = api_get_session_id(); |
||
| 77 | $groupId = api_get_group_id(); |
||
| 78 | |||
| 79 | $log('request.start', [ |
||
| 80 | 'cid' => $courseId, |
||
| 81 | 'uid' => $userId, |
||
| 82 | 'sid' => $sessionId, |
||
| 83 | 'gid' => $groupId, |
||
| 84 | 'query' => $request->query->all(), |
||
| 85 | 'post' => $request->request->all(), |
||
| 86 | ]); |
||
| 87 | |||
| 88 | $parentResourceNode = $this->getParentResourceNode($request); |
||
| 89 | $log('parent.node', ['id' => $parentResourceNode?->getId()]); |
||
| 90 | |||
| 91 | if (!$parentResourceNode) { |
||
| 92 | return new JsonResponse(['status' => false, 'error' => 'parent_node_not_found'], 404); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** @var CChatConversationRepository $conversationRepository */ |
||
| 96 | $conversationRepository = $doctrine->getRepository(CChatConversation::class); |
||
| 97 | |||
| 98 | // Helper: single-file-per-day behavior, append in chronological order |
||
| 99 | $chat = new CourseChatUtils( |
||
| 100 | $courseId, |
||
| 101 | $userId, |
||
| 102 | $sessionId, |
||
| 103 | $groupId, |
||
| 104 | $parentResourceNode, |
||
| 105 | $conversationRepository |
||
| 106 | ); |
||
| 107 | |||
| 108 | $action = (string) $request->get('action', 'track'); |
||
| 109 | $log('action', ['name' => $action]); |
||
| 110 | |||
| 111 | $json = ['status' => false]; |
||
| 112 | |||
| 113 | try { |
||
| 114 | switch ($action) { |
||
| 115 | case 'chat_logout': |
||
| 116 | Event::registerLog([ |
||
| 117 | 'tool' => TOOL_CHAT, |
||
| 118 | 'action' => 'exit', |
||
| 119 | 'action_details' => 'exit-chat', |
||
| 120 | ]); |
||
| 121 | $json = ['status' => true]; |
||
| 122 | $log('logout.ok'); |
||
| 123 | |||
| 124 | break; |
||
| 125 | |||
| 126 | case 'track': |
||
| 127 | $chat->keepUserAsConnected(); |
||
| 128 | $chat->disconnectInactiveUsers(); |
||
| 129 | |||
| 130 | $friend = (int) $request->get('friend', 0); |
||
| 131 | $newUsersOnline = $chat->countUsersOnline(); |
||
| 132 | $oldUsersOnline = (int) $request->get('users_online', 0); |
||
| 133 | |||
| 134 | $json = [ |
||
| 135 | 'status' => true, |
||
| 136 | 'data' => [ |
||
| 137 | 'oldFileSize' => false, |
||
| 138 | 'history' => $chat->readMessages(false, $friend), |
||
| 139 | 'usersOnline' => $newUsersOnline, |
||
| 140 | 'userList' => $newUsersOnline !== $oldUsersOnline ? $chat->listUsersOnline() : null, |
||
| 141 | 'currentFriend' => $friend, |
||
| 142 | ], |
||
| 143 | ]; |
||
| 144 | $log('track.ok', [ |
||
| 145 | 'friend' => $friend, |
||
| 146 | 'usersOnline' => $newUsersOnline, |
||
| 147 | 'listChanged' => ($newUsersOnline !== $oldUsersOnline), |
||
| 148 | ]); |
||
| 149 | |||
| 150 | break; |
||
| 151 | |||
| 152 | case 'preview': |
||
| 153 | $msg = (string) $request->get('message', ''); |
||
| 154 | $json = [ |
||
| 155 | 'status' => true, |
||
| 156 | 'data' => ['message' => $chat->prepareMessage($msg)], |
||
| 157 | ]; |
||
| 158 | $log('preview.ok', ['len' => \strlen($msg)]); |
||
| 159 | |||
| 160 | break; |
||
| 161 | |||
| 162 | case 'reset': |
||
| 163 | $friend = (int) $request->get('friend', 0); |
||
| 164 | $json = [ |
||
| 165 | 'status' => true, |
||
| 166 | 'data' => $chat->readMessages(true, $friend), |
||
| 167 | ]; |
||
| 168 | $log('reset.ok', ['friend' => $friend]); |
||
| 169 | |||
| 170 | break; |
||
| 171 | |||
| 172 | case 'write': |
||
| 173 | $friend = (int) $request->get('friend', 0); |
||
| 174 | $msg = (string) $request->get('message', ''); |
||
| 175 | $ok = $chat->saveMessage($msg, $friend); |
||
| 176 | |||
| 177 | $json = [ |
||
| 178 | 'status' => $ok, |
||
| 179 | 'data' => ['writed' => $ok], |
||
| 180 | ]; |
||
| 181 | $log('write.done', ['friend' => $friend, 'len' => \strlen($msg), 'ok' => $ok]); |
||
| 182 | |||
| 183 | break; |
||
| 184 | |||
| 185 | default: |
||
| 186 | $log('action.unknown', ['name' => $action]); |
||
| 187 | $json = ['status' => false, 'error' => 'unknown_action']; |
||
| 188 | |||
| 189 | break; |
||
| 190 | } |
||
| 191 | } catch (Throwable $e) { |
||
| 192 | $log('error', ['action' => $action, 'err' => $e->getMessage()]); |
||
| 193 | $json = ['status' => false, 'error' => $e->getMessage()]; |
||
| 194 | } finally { |
||
| 195 | $log('response', ['status' => $json['status'] ?? null]); |
||
| 196 | } |
||
| 197 | |||
| 198 | return new JsonResponse($json); |
||
| 199 | } |
||
| 421 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.