| Conditions | 18 |
| Paths | > 20000 |
| Total Lines | 72 |
| Code Lines | 52 |
| 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 |
||
| 19 | public function index(Request $request) |
||
| 20 | { |
||
| 21 | $this->setAdminPrefs(); |
||
| 22 | |||
| 23 | $meta_title = $title = 'User List'; |
||
| 24 | |||
| 25 | $roles = []; |
||
| 26 | $userRoles = Role::cursor()->remember(); |
||
| 27 | foreach ($userRoles as $userRole) { |
||
| 28 | $roles[$userRole->id] = $userRole->name; |
||
| 29 | } |
||
| 30 | |||
| 31 | $ordering = getUserBrowseOrdering(); |
||
| 32 | $orderBy = $request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : ''; |
||
| 33 | $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1; |
||
| 34 | $offset = ($page - 1) * config('nntmux.items_per_page'); |
||
| 35 | |||
| 36 | $variables = [ |
||
| 37 | 'username' => $request->has('username') ? $request->input('username') : '', |
||
| 38 | 'email' => $request->has('email') ? $request->input('email') : '', |
||
| 39 | 'host' => $request->has('host') ? $request->input('host') : '', |
||
| 40 | 'role' => $request->has('role') ? $request->input('role') : '', |
||
| 41 | 'created_from' => $request->has('created_from') ? $request->input('created_from') : '', |
||
| 42 | 'created_to' => $request->has('created_to') ? $request->input('created_to') : '', |
||
| 43 | ]; |
||
| 44 | |||
| 45 | $result = User::getRange( |
||
| 46 | $offset, |
||
| 47 | config('nntmux.items_per_page'), |
||
| 48 | $orderBy, |
||
| 49 | $variables['username'], |
||
| 50 | $variables['email'], |
||
| 51 | $variables['host'], |
||
| 52 | $variables['role'], |
||
| 53 | true, |
||
| 54 | $variables['created_from'], |
||
| 55 | $variables['created_to'] |
||
| 56 | ); |
||
| 57 | |||
| 58 | $results = $this->paginate($result ?? [], User::getCount($variables['role'], $variables['username'], $variables['host'], $variables['email'], $variables['created_from'], $variables['created_to']) ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query()); |
||
| 59 | |||
| 60 | // Add country data to each user based on their host IP |
||
| 61 | foreach ($results as $user) { |
||
| 62 | $position = null; |
||
| 63 | if (! empty($user->host) && filter_var($user->host, FILTER_VALIDATE_IP)) { |
||
| 64 | $position = Location::get($user->host); |
||
| 65 | } |
||
| 66 | $user->country_name = $position ? $position->countryName : null; |
||
| 67 | $user->country_code = $position ? $position->countryCode : null; |
||
| 68 | } |
||
| 69 | |||
| 70 | // Build order by URLs |
||
| 71 | $orderByUrls = []; |
||
| 72 | foreach ($ordering as $orderType) { |
||
| 73 | $orderByUrls['orderby'.$orderType] = url('admin/user-list?ob='.$orderType); |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->viewData = array_merge($this->viewData, [ |
||
| 77 | 'username' => $variables['username'], |
||
| 78 | 'email' => $variables['email'], |
||
| 79 | 'host' => $variables['host'], |
||
| 80 | 'role' => $variables['role'], |
||
| 81 | 'created_from' => $variables['created_from'], |
||
| 82 | 'created_to' => $variables['created_to'], |
||
| 83 | 'role_ids' => array_keys($roles), |
||
| 84 | 'role_names' => $roles, |
||
| 85 | 'userlist' => $results, |
||
| 86 | 'title' => $title, |
||
| 87 | 'meta_title' => $meta_title, |
||
| 88 | ], $orderByUrls); |
||
| 89 | |||
| 90 | return view('admin.users.index', $this->viewData); |
||
| 91 | } |
||
| 278 |