| Conditions | 9 |
| Paths | 24 |
| Total Lines | 53 |
| Code Lines | 34 |
| Lines | 5 |
| Ratio | 9.43 % |
| 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 |
||
| 13 | public function eventsAction($chatId = 0) { |
||
|
1 ignored issue
–
show
|
|||
| 14 | $chatId = (int) $chatId; |
||
| 15 | $result = new Server\Result(); |
||
| 16 | View Code Duplication | if (!$chatId || !($chat = \Chats\Chat::get($chatId))) { |
|
| 17 | $result->success = false; |
||
| 18 | $result->content = 'Такого чата не существует'; |
||
| 19 | $result->send(); |
||
| 20 | } |
||
| 21 | $where = [ |
||
| 22 | ['chat_id', $chatId], |
||
| 23 | ['delete', 0] |
||
| 24 | ]; |
||
| 25 | if (!empty($_GET['lastEventDate'])) { |
||
| 26 | $where[] = ['date_create', $_GET['lastEventDate'], '>']; |
||
| 27 | } |
||
| 28 | $result->content = [ |
||
| 29 | 'members' => [], |
||
| 30 | 'messages' => [] |
||
| 31 | ]; |
||
| 32 | if (Users\User::$cur->id) { |
||
| 33 | $member = \Chats\Chat\Member::get([['chat_id', $chatId], ['user_id', Users\User::$cur->id]]); |
||
| 34 | if (!$member) { |
||
| 35 | $member = new \Chats\Chat\Member(); |
||
| 36 | $member->user_id = Users\User::$cur->id; |
||
| 37 | $member->chat_id = $chatId; |
||
| 38 | } |
||
| 39 | $member->date_last_active = date('Y-m-d H:i:s'); |
||
| 40 | $member->save(); |
||
| 41 | } |
||
| 42 | |||
| 43 | $messages = \Chats\Chat\Message::getList(['where' => $where, 'limit' => 20, 'order' => ['date_create', 'DESC']]); |
||
| 44 | if ($messages) { |
||
| 45 | $messages = array_reverse($messages); |
||
| 46 | foreach ($messages as $message) { |
||
| 47 | $msg = [ |
||
| 48 | 'message' => $message->_params, |
||
| 49 | 'fullUserName' => $message->user->name(), |
||
| 50 | 'userFirstName' => $message->user->info->first_name, |
||
| 51 | 'userPhoto' => $message->user->info->photo ? $message->user->info->photo->path : '/static/system/images/no-image.png' |
||
| 52 | ]; |
||
| 53 | $result->content['messages'][] = $msg; |
||
| 54 | } |
||
| 55 | /* $members = $this->module->getMembers($chatId); |
||
| 56 | foreach ($members as $member) { |
||
| 57 | $result->content['members'][$member->user_id] = [ |
||
| 58 | 'fullUserName' => $member->user->name(), |
||
| 59 | 'userFirstName' => $member->user->info->first_name, |
||
| 60 | 'userPhoto' => $member->user->info->photo ? $member->user->info->photo->path : '/static/system/images/no-image.png' |
||
| 61 | ]; |
||
| 62 | } */ |
||
| 63 | } |
||
| 64 | $result->send(); |
||
| 65 | } |
||
| 66 | |||
| 147 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: