| Conditions | 6 |
| Paths | 5 |
| Total Lines | 181 |
| Code Lines | 114 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 3 | Features | 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 |
||
| 60 | public function setupListOperation() |
||
| 61 | { |
||
| 62 | |||
| 63 | // display lead status counts on page top |
||
| 64 | foreach (LeadType::all() as $leadType) { |
||
| 65 | if ($leadType->id === 4) { |
||
| 66 | $count = Student::where('lead_type_id', $leadType->id)->orWhereNull('lead_type_id')->count(); |
||
| 67 | } else { |
||
| 68 | $count = Student::where('lead_type_id', $leadType->id)->count(); |
||
| 69 | } |
||
| 70 | if ($count > 0) { |
||
| 71 | Widget::add([ |
||
| 72 | 'type' => 'view', |
||
| 73 | 'view' => 'students.lead-type-insights-widget', |
||
| 74 | 'studentCount' => $count, |
||
| 75 | 'name' => Str::plural($leadType->name), |
||
| 76 | 'icon' => $leadType->icon, |
||
| 77 | 'leadTypeId' => $leadType->id, |
||
| 78 | 'description' => $leadType->description, |
||
| 79 | ])->to('before_content'); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | // Columns. |
||
| 84 | CRUD::setColumns([ |
||
| 85 | [ |
||
| 86 | 'label' => __('ID number'), |
||
| 87 | 'type' => 'text', |
||
| 88 | 'name' => 'idnumber', |
||
| 89 | ], |
||
| 90 | [ |
||
| 91 | // 1-n relationship |
||
| 92 | 'label' => __('Last Name'), |
||
| 93 | 'type' => 'relationship', |
||
| 94 | 'key' => 'lastname', |
||
| 95 | 'name' => 'user', |
||
| 96 | 'attribute' => 'lastname', |
||
| 97 | 'model' => User::class, |
||
| 98 | 'orderable' => true, |
||
| 99 | 'orderLogic' => fn ($query, $column, $columnDirection) => $query->leftJoin('users', 'users.id', '=', 'students.id') |
||
| 100 | ->orderBy('users.lastname', $columnDirection)->select('students.*'), |
||
| 101 | 'searchLogic' => function ($query, $column, $searchTerm) { |
||
| 102 | $query->orWhereHas('user', function ($q) use ($searchTerm) { |
||
| 103 | $q->where('lastname', 'like', '%'.$searchTerm.'%'); |
||
| 104 | }); |
||
| 105 | }, |
||
| 106 | ], |
||
| 107 | |||
| 108 | [ |
||
| 109 | // 1-n relationship |
||
| 110 | 'label' => __('First Name'), |
||
| 111 | 'type' => 'relationship', |
||
| 112 | 'key' => 'firstname', |
||
| 113 | 'name' => 'user', |
||
| 114 | 'attribute' => 'firstname', |
||
| 115 | 'model' => User::class, |
||
| 116 | 'orderable' => true, |
||
| 117 | 'orderLogic' => fn ($query, $column, $columnDirection) => $query->leftJoin('users', 'users.id', '=', 'students.id') |
||
| 118 | ->orderBy('users.firstname', $columnDirection)->select('students.*'), |
||
| 119 | 'searchLogic' => function ($query, $column, $searchTerm) { |
||
| 120 | $query->orWhereHas('user', function ($q) use ($searchTerm) { |
||
| 121 | $q->where('firstname', 'like', '%'.$searchTerm.'%'); |
||
| 122 | }); |
||
| 123 | }, |
||
| 124 | ], |
||
| 125 | |||
| 126 | [ |
||
| 127 | // 1-n relationship |
||
| 128 | 'label' => __('Email'), |
||
| 129 | 'type' => 'relationship', |
||
| 130 | 'name' => 'user', |
||
| 131 | 'attribute' => 'email', |
||
| 132 | 'model' => User::class, |
||
| 133 | 'orderable' => true, |
||
| 134 | 'orderLogic' => fn ($query, $column, $columnDirection) => $query->leftJoin('users', 'users.id', '=', 'students.id') |
||
| 135 | ->orderBy('users.email', $columnDirection)->select('students.*'), |
||
| 136 | 'searchLogic' => function ($query, $column, $searchTerm) { |
||
| 137 | $query->orWhereHas('user', function ($q) use ($searchTerm) { |
||
| 138 | $q->where('email', 'like', '%'.$searchTerm.'%'); |
||
| 139 | }); |
||
| 140 | }, |
||
| 141 | ], |
||
| 142 | |||
| 143 | [ |
||
| 144 | 'label' => __('Username'), |
||
| 145 | 'type' => 'relationship', |
||
| 146 | 'key' => 'username', |
||
| 147 | 'name' => 'user', |
||
| 148 | 'attribute' => 'username', |
||
| 149 | 'model' => User::class, |
||
| 150 | 'orderable' => false, |
||
| 151 | 'searchLogic' => false, |
||
| 152 | ], |
||
| 153 | |||
| 154 | [ |
||
| 155 | 'label' => __('Age'), |
||
| 156 | 'name' => 'student_age', |
||
| 157 | ], |
||
| 158 | |||
| 159 | [ |
||
| 160 | 'label' => __('Birthdate'), |
||
| 161 | 'name' => 'student_birthdate', |
||
| 162 | ], |
||
| 163 | |||
| 164 | [ |
||
| 165 | // n-n relationship (with pivot table) |
||
| 166 | 'label' => __('Phone number'), |
||
| 167 | 'type' => 'select_multiple', |
||
| 168 | 'name' => 'phone', |
||
| 169 | 'attribute' => 'phone_number', |
||
| 170 | 'model' => PhoneNumber::class, |
||
| 171 | ], |
||
| 172 | |||
| 173 | [ |
||
| 174 | // 1-n relationship |
||
| 175 | 'label' => __('Status'), |
||
| 176 | 'type' => 'text', |
||
| 177 | 'name' => 'lead_status_name', |
||
| 178 | 'orderable' => false, |
||
| 179 | ], |
||
| 180 | |||
| 181 | ]); |
||
| 182 | |||
| 183 | CRUD::addFilter( |
||
| 184 | [ // select2 filter |
||
| 185 | 'name' => 'enrolled', |
||
| 186 | 'type' => 'select2', |
||
| 187 | 'label' => __('Is Enrolled in'), |
||
| 188 | ], |
||
| 189 | fn () => Period::all()->pluck('name', 'id')->toArray(), |
||
| 190 | function ($value) { |
||
| 191 | $this->crud->query = $this->crud->query->whereHas('enrollments', fn ($query) => $query->whereHas('course', function ($q) use ($value) { |
||
| 192 | $q->where('period_id', $value); |
||
| 193 | })); |
||
| 194 | }, |
||
| 195 | function () { // if the filter is NOT active (the GET parameter "checkbox" does not exit) |
||
| 196 | } |
||
| 197 | ); |
||
| 198 | |||
| 199 | CRUD::addFilter([ // select2_multiple filter |
||
| 200 | 'name' => 'notenrolled', |
||
| 201 | 'type' => 'select2_multiple', |
||
| 202 | 'label' => __('Is Not Enrolled in'), |
||
| 203 | ], fn () => Period::all()->pluck('name', 'id')->toArray(), function ($values) { |
||
| 204 | foreach (json_decode($values, null, 512, JSON_THROW_ON_ERROR) as $value) { |
||
| 205 | $this->crud->query = $this->crud->query->whereDoesntHave('enrollments', fn ($query) => $query->whereHas('course', function ($q) use ($value) { |
||
| 206 | $q->where('period_id', $value); |
||
| 207 | })); |
||
| 208 | } |
||
| 209 | }); |
||
| 210 | |||
| 211 | CRUD::addFilter( |
||
| 212 | [ |
||
| 213 | 'name' => 'new_students', |
||
| 214 | 'type' => 'select2', |
||
| 215 | 'label' => __('New In'), |
||
| 216 | ], |
||
| 217 | fn () => Period::all()->pluck('name', 'id')->toArray(), |
||
| 218 | function ($value) { |
||
| 219 | CRUD::addClause('newInPeriod', $value); |
||
| 220 | } |
||
| 221 | ); |
||
| 222 | |||
| 223 | // select2 filter |
||
| 224 | $this->crud->addFilter([ |
||
| 225 | 'name' => 'institution_id', |
||
| 226 | 'type' => 'select2', |
||
| 227 | 'label' => __('Institution'), |
||
| 228 | ], fn () => Institution::all()->pluck('name', 'id')->toArray(), function ($value) { |
||
| 229 | $this->crud->addClause('where', 'institution_id', $value); |
||
| 230 | }); |
||
| 231 | |||
| 232 | $this->crud->addFilter([ |
||
| 233 | 'name' => 'lead_type_id', |
||
| 234 | 'type' => 'select2', |
||
| 235 | 'label' => __('Lead Status'), |
||
| 236 | ], fn () => LeadType::all()->pluck('name', 'id')->toArray(), function ($value) { |
||
| 237 | if ($value === '4') { |
||
| 238 | $this->crud->query = $this->crud->query->where('lead_type_id', $value)->orWhere('lead_type_id', null); |
||
| 239 | } else { |
||
| 240 | $this->crud->addClause('where', 'lead_type_id', $value); |
||
| 241 | } |
||
| 501 |