Conditions | 10 |
Paths | 48 |
Total Lines | 41 |
Code Lines | 24 |
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 | $user = auth()->user(); |
||
18 | |||
19 | $companies_id = $user->Company()->pluck('companies.id'); |
||
20 | $query = Company::query(); |
||
21 | |||
22 | if ($request->has('searchTerm')) { |
||
23 | $columnsToSearch = ['name', 'status']; |
||
24 | $search_term = json_decode($request->searchTerm)->searchTerm; |
||
25 | if (! empty($search_term)) { |
||
26 | $searchQuery = '%'.$search_term.'%'; |
||
27 | foreach ($columnsToSearch as $column) { |
||
28 | $query->orWhere($column, 'LIKE', $searchQuery); |
||
29 | } |
||
30 | } |
||
31 | } |
||
32 | |||
33 | if ($request->has('columnFilters')) { |
||
34 | $filters = get_object_vars(json_decode($request->columnFilters)); |
||
35 | |||
36 | foreach ($filters as $key => $value) { |
||
37 | if (! empty($value)) { |
||
38 | $query->orWhere($key, 'like', '%'.$value.'%'); |
||
39 | } |
||
40 | } |
||
41 | } |
||
42 | |||
43 | if ($request->has('sort.0')) { |
||
44 | $sort = json_decode($request->sort[0]); |
||
45 | $query->orderBy($sort->field, $sort->type); |
||
46 | } |
||
47 | $query->find($companies_id); |
||
48 | if ($request->has('perPage')) { |
||
49 | $rows = $query->paginate($request->perPage); |
||
50 | } |
||
51 | if (! count($request->all())) { |
||
52 | $rows = $query->get()->toArray(); |
||
53 | } |
||
54 | |||
55 | return $rows; |
||
|
|||
56 | } |
||
155 |