| Conditions | 12 |
| Paths | 13 |
| Total Lines | 68 |
| Code Lines | 39 |
| 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 |
||
| 31 | public function listMessageDialog($offset = 0, $new = 0): ?string |
||
| 32 | { |
||
| 33 | // check is user auth |
||
| 34 | if (!App::$User->isAuth()) { |
||
| 35 | throw new ForbiddenException('Auth required'); |
||
| 36 | } |
||
| 37 | $this->setJsonHeader(); |
||
| 38 | |||
| 39 | // check is offset is int |
||
| 40 | if ($offset !== 0 && !Any::isInt($offset)) { |
||
| 41 | $offset = 0; |
||
| 42 | } |
||
| 43 | ++$offset; |
||
| 44 | |||
| 45 | // get user person |
||
| 46 | $user = App::$User->identity(); |
||
| 47 | |||
| 48 | $records = Message::select('readed', 'target_id', 'sender_id', Capsule::raw('max(created_at) as cmax')) |
||
|
|
|||
| 49 | ->where('target_id', '=', $user->id) |
||
| 50 | ->orWhere('sender_id', '=', $user->id) |
||
| 51 | ->orderBy('readed', 'ASC') //- error happens, cuz readed is boolean in pgsql |
||
| 52 | ->orderBy('cmax', 'DESC') |
||
| 53 | ->groupBy(['sender_id', 'target_id', 'readed']) // multiple order's can throw exception on some kind of database engines |
||
| 54 | ->take($offset * self::MSG_USER_LIST) |
||
| 55 | ->get(); |
||
| 56 | |||
| 57 | $userList = []; |
||
| 58 | $unreadList = []; |
||
| 59 | |||
| 60 | if (Any::isInt($new) && $new > 0 && App::$User->isExist($new)) { |
||
| 61 | $userList[] = $new; |
||
| 62 | } |
||
| 63 | |||
| 64 | $records->each(function($row) use (&$userList, $user){ |
||
| 65 | // target is not myself? then i'm - sender (remote user is target: my->to_user) |
||
| 66 | if ($row->target_id !== $user->id) { |
||
| 67 | $userList[] = $row->target_id; |
||
| 68 | } |
||
| 69 | |||
| 70 | // sender is not myself? then i'm - target (remote user is sender user->to_me) |
||
| 71 | if ($row->sender_id !== $user->id) { |
||
| 72 | $userList[] = $row->sender_id; |
||
| 73 | if ((bool)$row->readed !== true) { |
||
| 74 | $unreadList[] = $row->sender_id; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | }); |
||
| 78 | |||
| 79 | // store only unique users in dialog |
||
| 80 | $userList = array_unique($userList, SORT_NUMERIC); |
||
| 81 | // generate json response based on userList and unreadList |
||
| 82 | $response = []; |
||
| 83 | foreach ($userList as $user_id) { |
||
| 84 | $identity = App::$User->identity($user_id); |
||
| 85 | if (!$identity) { |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | |||
| 89 | $response[] = [ |
||
| 90 | 'user_id' => $user_id, |
||
| 91 | 'user_nick' => $identity->profile->getNickname(), |
||
| 92 | 'user_avatar' => $identity->profile->getAvatarUrl('small'), |
||
| 93 | 'message_new' => Arr::in($user_id, $unreadList), |
||
| 94 | 'user_block' => !Blacklist::check($user->id, $identity->id) |
||
| 95 | ]; |
||
| 96 | } |
||
| 97 | |||
| 98 | return json_encode(['status' => 1, 'data' => $response]); |
||
| 99 | } |
||
| 101 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.