| Conditions | 22 |
| Paths | 12289 |
| Total Lines | 121 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 7 | public function search() |
||
| 8 | { |
||
| 9 | $msg = null; |
||
| 10 | $gets = $this->request->gets(); |
||
| 11 | |||
| 12 | if (empty($gets)) { |
||
| 13 | $users = $this->load->model('User')->users(); |
||
| 14 | $usersformatted = $this->formatUsers($users); |
||
|
|
|||
| 15 | return json_encode($usersformatted); |
||
| 16 | } |
||
| 17 | |||
| 18 | $gender = $gets['gender'] ?? null; |
||
| 19 | $zip = $gets['zip'] ?? null; |
||
| 20 | $country = $gets['country'] ?? null; |
||
| 21 | $registration_from = $gets['registration_from'] ?? null; |
||
| 22 | $registration_to = $gets['registration_to'] ?? null; |
||
| 23 | $active = $gets['active'] ?? null; |
||
| 24 | $pending = $gets['pending'] ?? null; |
||
| 25 | $inactive = $gets['inactive'] ?? null; |
||
| 26 | $online = $gets['online'] ?? null; |
||
| 27 | $offline = $gets['offline'] ?? null; |
||
| 28 | |||
| 29 | $sql = ''; |
||
| 30 | $wheres = []; |
||
| 31 | |||
| 32 | if ($active && $active == '1') { |
||
| 33 | $sql .= 'status = ? AND '; |
||
| 34 | array_push($wheres, '2'); |
||
| 35 | } |
||
| 36 | if ($pending && $pending == '1') { |
||
| 37 | $sql .= 'status = ? AND '; |
||
| 38 | array_push($wheres, '1'); |
||
| 39 | } |
||
| 40 | if ($inactive && $inactive == '1') { |
||
| 41 | $sql .= 'status = ? AND '; |
||
| 42 | array_push($wheres, '0'); |
||
| 43 | } |
||
| 44 | |||
| 45 | $count_status = substr_count($sql, 'status = ?'); |
||
| 46 | |||
| 47 | if ($count_status > 1) { |
||
| 48 | $sql = str_replace('status = ? AND', 'status = ? OR', $sql); |
||
| 49 | $sql = rtrim($sql, 'OR '); |
||
| 50 | $sql = "( $sql )"; |
||
| 51 | $sql .= ' AND '; |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($online && $online == '1') { |
||
| 55 | $sql .= 'is_login = ? AND '; |
||
| 56 | array_push($wheres, '1'); |
||
| 57 | } |
||
| 58 | if ($offline && $offline == '1') { |
||
| 59 | $sql .= 'is_login = ? AND '; |
||
| 60 | array_push($wheres, '0'); |
||
| 61 | } |
||
| 62 | |||
| 63 | $count_is_login = substr_count($sql, 'is_login = ?'); |
||
| 64 | |||
| 65 | if ($count_is_login > 1) { |
||
| 66 | $sql = str_replace('is_login = ? AND', 'is_login = ? OR', $sql); |
||
| 67 | $sql = rtrim($sql, 'OR '); |
||
| 68 | $sql = "( $sql )"; |
||
| 69 | $sql .= ' AND '; |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($gender) { |
||
| 73 | $sql .= 'gender = ? AND '; |
||
| 74 | array_push($wheres, $gender); |
||
| 75 | } |
||
| 76 | if ($zip) { |
||
| 77 | $sql .= 'zip = ? AND '; |
||
| 78 | array_push($wheres, $zip); |
||
| 79 | } |
||
| 80 | if ($country) { |
||
| 81 | $sql .= 'country = ? AND '; |
||
| 82 | array_push($wheres, $country); |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($registration_from) { |
||
| 86 | $registration_from = date("Y-m-d", strtotime($registration_from)); |
||
| 87 | |||
| 88 | if (!$registration_to) { |
||
| 89 | $sql .= 'registration >= ? AND '; |
||
| 90 | array_push($wheres, $registration_from); |
||
| 91 | } else { |
||
| 92 | $registration_to = date("Y-m-d", strtotime($registration_to)); |
||
| 93 | |||
| 94 | $sql .= 'registration BETWEEN ? AND ? AND '; |
||
| 95 | array_push($wheres, $registration_from); |
||
| 96 | array_push($wheres, $registration_to); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($sql == '') { |
||
| 101 | $users = $this->load->model('User')->users(); |
||
| 102 | |||
| 103 | $usersformatted = $this->formatUsers($users); |
||
| 104 | |||
| 105 | return json_encode($usersformatted); |
||
| 106 | } |
||
| 107 | |||
| 108 | $sql = substr($sql, 0, -4); |
||
| 109 | |||
| 110 | $users = $this->load->model('User')->filter($sql, $wheres); |
||
| 111 | |||
| 112 | if (!$users) { |
||
| 113 | $msg = 'no users'; |
||
| 114 | return json_encode($msg); |
||
| 115 | } |
||
| 116 | |||
| 117 | $users_for_list = []; |
||
| 118 | |||
| 119 | foreach ($users as $user) { |
||
| 120 | $user->new = $this->isUserNew($user->registration); |
||
| 121 | $user->country_icon = $this->countries($user->country); |
||
| 122 | $user->registration = $this->changeFormatDate($user->registration); |
||
| 123 | $user->last_login = $this->changeFormatDate($user->last_login); |
||
| 124 | $users_for_list[] = $user; |
||
| 125 | } |
||
| 126 | $msg = $users_for_list; |
||
| 127 | return json_encode($msg); |
||
| 128 | } |
||
| 130 |