| Conditions | 13 |
| Paths | 15 |
| Total Lines | 97 |
| Code Lines | 60 |
| 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 |
||
| 34 | public function messageList($corId): ?string |
||
| 35 | { |
||
| 36 | if (!App::$User->isAuth()) { |
||
| 37 | throw new ForbiddenException('Auth required'); |
||
| 38 | } |
||
| 39 | |||
| 40 | if (!Any::isInt($corId) || $corId < 1) { |
||
| 41 | throw new NotFoundException('Corresponded id is wrong'); |
||
| 42 | } |
||
| 43 | |||
| 44 | // get special types for this action |
||
| 45 | $queryType = $this->request->get('type'); |
||
| 46 | $queryId = (int)$this->request->get('id'); |
||
| 47 | // get current user object |
||
| 48 | $user = App::$User->identity(); |
||
| 49 | |||
| 50 | if (Arr::in($queryType, ['before', 'after']) && (!Any::isInt($queryId) || $queryId < 1)) { |
||
| 51 | throw new NativeException('Bad input data'); |
||
| 52 | } |
||
| 53 | |||
| 54 | $messages = null; |
||
|
|
|||
| 55 | // sounds like a Hindi code, but we need more closures to organize where conditions |
||
| 56 | // after raw: select * from `ffcms_messages` where `id` > ? and ((`target_id` = ? and `sender_id` = ?) or (`target_id` = ? and `sender_id` = ?)) order by `created_at` desc |
||
| 57 | // before raw: select * from `ffcms_messages` where (`target_id` = ? and `sender_id` = ?) or (`target_id` = ? and `sender_id` = ?) order by `created_at` desc |
||
| 58 | // default raw: select * from `ffcms_messages` where `id` < ? and ((`target_id` = ? and `sender_id` = ?) or (`target_id` = ? and `sender_id` = ?)) order by `created_at` desc |
||
| 59 | switch ($queryType) { |
||
| 60 | case 'after': |
||
| 61 | $messages = Message::where('id', '>', $queryId) |
||
| 62 | ->where(function ($query) use ($corId, $user) { |
||
| 63 | $query->where(function ($q) use ($cor_id, $user) { |
||
| 64 | $q->where('target_id', '=', $user->getId()) |
||
| 65 | ->where('sender_id', '=', $cor_id); |
||
| 66 | })->orWhere(function ($q) use ($cor_id, $user) { |
||
| 67 | $q->where('target_id', '=', $cor_id) |
||
| 68 | ->where('sender_id', '=', $user->getId()); |
||
| 69 | }); |
||
| 70 | }); |
||
| 71 | break; |
||
| 72 | case 'before': |
||
| 73 | $messages = Message::where('id', '<', $queryId) |
||
| 74 | ->where(function ($query) use ($corId, $user) { |
||
| 75 | $query->where(function ($q) use ($cor_id, $user) { |
||
| 76 | $q->where('target_id', '=', $user->getId()) |
||
| 77 | ->where('sender_id', '=', $cor_id); |
||
| 78 | })->orWhere(function ($q) use ($cor_id, $user) { |
||
| 79 | $q->where('target_id', '=', $cor_id) |
||
| 80 | ->where('sender_id', '=', $user->getId()); |
||
| 81 | }); |
||
| 82 | }); |
||
| 83 | break; |
||
| 84 | default: |
||
| 85 | $messages = Message::where(function ($query) use ($corId, $user) { |
||
| 86 | $query->where('target_id', '=', $user->getId()) |
||
| 87 | ->where('sender_id', '=', $corId); |
||
| 88 | })->orWhere(function ($query) use ($corId, $user) { |
||
| 89 | $query->where('target_id', '=', $corId) |
||
| 90 | ->where('sender_id', '=', $user->getId()); |
||
| 91 | }); |
||
| 92 | break; |
||
| 93 | } |
||
| 94 | |||
| 95 | // set response header |
||
| 96 | $this->setJsonHeader(); |
||
| 97 | |||
| 98 | $messages->orderBy('created_at', 'DESC') |
||
| 99 | ->take(self::MSG_TEXT_LIST); |
||
| 100 | |||
| 101 | // check if messages exist |
||
| 102 | if ($messages->count() < 1) { |
||
| 103 | return json_encode(['status' => 0, 'text' => 'No messages']); |
||
| 104 | } |
||
| 105 | |||
| 106 | // build response |
||
| 107 | $response = null; |
||
| 108 | foreach ($messages->get() as $msg) { |
||
| 109 | /** @var Message $msg */ |
||
| 110 | $response[] = [ |
||
| 111 | 'id' => $msg->id, |
||
| 112 | 'my' => $msg->sender_id === $user->id, |
||
| 113 | 'message' => $msg->message, |
||
| 114 | 'date' => Date::convertToDatetime($msg->created_at, Date::FORMAT_TO_SECONDS), |
||
| 115 | 'readed' => $msg->readed |
||
| 116 | ]; |
||
| 117 | |||
| 118 | // update status to readed |
||
| 119 | if ((bool)$msg->readed !== true && $msg->sender_id !== $user->id) { |
||
| 120 | $msg->readed = true; |
||
| 121 | $msg->save(); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | return json_encode([ |
||
| 126 | 'status' => 1, |
||
| 127 | 'data' => array_reverse($response), |
||
| 128 | 'blocked' => !Blacklist::check($user->id, $corId) |
||
| 129 | ]); |
||
| 130 | } |
||
| 131 | } |
||
| 132 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.