Conditions | 25 |
Paths | 9216 |
Total Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
21 | public function index(Request $request) |
||
22 | { |
||
23 | ini_set('memory_limit', '-1'); |
||
24 | ini_set('max_execution_time', '0'); |
||
25 | |||
26 | $query = ApiAccessLogs::query(); |
||
27 | |||
28 | $uid = $request->input('uid'); |
||
29 | if (isset($uid) && $uid) { |
||
30 | $query = $query->where('user.id', $uid); |
||
31 | } |
||
32 | |||
33 | $method = $request->input('method'); |
||
34 | if (isset($method) && $method) { |
||
35 | $query = $query->where('request_method', $method); |
||
36 | } |
||
37 | |||
38 | $project_key = $request->input('project_key'); |
||
39 | if (isset($project_key) && $project_key) { |
||
40 | $query = $query->where('project_key', $project_key); |
||
41 | } |
||
42 | |||
43 | $module = $request->input('module'); |
||
44 | if (isset($module) && $module) { |
||
45 | $query = $query->where('module', $module); |
||
46 | } |
||
47 | |||
48 | $request_time = $request->input('request_time'); |
||
49 | if (isset($request_time) && $request_time) { |
||
50 | if (strpos($request_time, '~') !== false) { |
||
51 | $sections = explode('~', $request_time); |
||
52 | if ($sections[0]) { |
||
53 | $query->where('requested_start_at', '>=', strtotime($sections[0]) * 1000); |
||
54 | } |
||
55 | if ($sections[1]) { |
||
56 | $query->where('requested_start_at', '<=', strtotime($sections[1] . ' 23:59:59') * 1000); |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | $url = $request->input('request_url'); |
||
62 | if (isset($url) && $url) { |
||
63 | $query->where('request_url', 'like', '%' . $url . '%'); |
||
64 | } |
||
65 | |||
66 | $request_source_ip = $request->input('request_source_ip'); |
||
67 | if (isset($request_source_ip) && $request_source_ip) { |
||
68 | $query->where('request_source_ip', $request_source_ip); |
||
69 | } |
||
70 | |||
71 | $exec_time = $request->input('exec_time'); |
||
72 | if (isset($exec_time) && $exec_time) { |
||
73 | $flag = substr($exec_time, 0, 1); |
||
74 | if ($flag == '-') { |
||
75 | $query->where('exec_time', '<=', abs(floatval($exec_time)) * 1000); |
||
76 | } |
||
77 | else |
||
78 | { |
||
79 | $query->where('exec_time', '>=', abs(floatval($exec_time)) * 1000); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | // get total |
||
84 | $total = $query->count(); |
||
85 | $query->orderBy('_id', 'desc'); |
||
86 | $page_size = $request->input('limit') ? intval($request->input('limit')) : 100; |
||
87 | $page = $request->input('page') ?: 1; |
||
88 | $query = $query->skip($page_size * ($page - 1))->take($page_size); |
||
89 | |||
90 | $logs = $query->get(); |
||
91 | |||
92 | $from = $request->input('from'); |
||
93 | if (isset($from) && $from == 'export') { |
||
94 | $this->export($logs); |
||
95 | exit(); |
||
96 | } |
||
97 | |||
98 | return response()->json([ 'ecode' => 0, 'data' => $logs, 'options' => [ 'total' => $total, 'sizePerPage' => $page_size ] ]); |
||
|
|||
99 | } |
||
100 | |||
151 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: