| Conditions | 10 |
| Paths | 48 |
| Total Lines | 38 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 17 | public function index(Request $request) |
||
| 18 | { |
||
| 19 | $query = Family::query()->with(['husband', 'wife']); |
||
| 20 | |||
| 21 | if ($request->has('searchTerm')) { |
||
| 22 | $columnsToSearch = ['description', 'is_active', 'husband_id', 'wife_id', 'type_id', 'chan', 'nchi', 'rin']; |
||
| 23 | $search_term = json_decode($request->searchTerm)->searchTerm; |
||
| 24 | if (! empty($search_term)) { |
||
| 25 | $searchQuery = '%'.$search_term.'%'; |
||
| 26 | foreach ($columnsToSearch as $column) { |
||
| 27 | $query->orWhere($column, 'LIKE', $searchQuery); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | if ($request->has('columnFilters')) { |
||
| 33 | $filters = get_object_vars(json_decode($request->columnFilters)); |
||
| 34 | |||
| 35 | foreach ($filters as $key => $value) { |
||
| 36 | if (! empty($value)) { |
||
| 37 | $query->orWhere($key, 'like', '%'.$value.'%'); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | if ($request->has('sort.0')) { |
||
| 43 | $sort = json_decode($request->sort[0]); |
||
| 44 | $query->orderBy($sort->field, $sort->type); |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($request->has('perPage')) { |
||
| 48 | $rows = $query->paginate($request->perPage); |
||
| 49 | } |
||
| 50 | if (! count($request->all())) { |
||
| 51 | $rows = $query->get(); |
||
| 52 | } |
||
| 53 | |||
| 54 | return $rows; |
||
|
|
|||
| 55 | } |
||
| 176 |