| Conditions | 10 |
| Paths | 24 |
| Total Lines | 37 |
| 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 |
||
| 15 | public function index(Request $request) |
||
| 16 | { |
||
| 17 | $query = Repository::query()->with(['type', 'addr']); |
||
| 18 | |||
| 19 | if ($request->has('searchTerm')) { |
||
| 20 | $columnsToSearch = ['repo', 'name', 'addr_id', 'rin', 'email', 'phon', 'fax', 'www', 'description', 'type_id', 'is_active']; |
||
| 21 | $search_term = json_decode($request->searchTerm)->searchTerm; |
||
| 22 | if (! empty($search_term)) { |
||
| 23 | $searchQuery = '%'.$search_term.'%'; |
||
| 24 | foreach ($columnsToSearch as $column) { |
||
| 25 | $query->orWhere($column, 'LIKE', $searchQuery); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | if ($request->has('columnFilters')) { |
||
| 31 | $filters = get_object_vars(json_decode($request->columnFilters)); |
||
| 32 | $relationship_column = ['addr.adr1', 'type.name']; |
||
| 33 | foreach ($filters as $key => $value) { |
||
| 34 | if (! in_array($key, $relationship_column)) { |
||
| 35 | if (! empty($value)) { |
||
| 36 | $query->orWhere($key, 'like', '%'.$value.'%'); |
||
| 37 | } |
||
| 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 | |||
| 51 | return $rows; |
||
|
|
|||
| 52 | } |
||
| 178 |