Conditions | 12 |
Paths | 1 |
Total Lines | 39 |
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 |
||
120 | public function export($logs) |
||
121 | { |
||
122 | set_time_limit(0); |
||
123 | |||
124 | $headers = [ |
||
125 | '用户', |
||
126 | '方法', |
||
127 | 'Url', |
||
128 | '模块', |
||
129 | '请求开始时间', |
||
130 | '请求结束时间', |
||
131 | '请求时长(毫秒)', |
||
132 | '来源IP', |
||
133 | 'User-Agent', |
||
134 | 'Body' |
||
135 | ]; |
||
136 | |||
137 | $file_name = 'access-logs'; |
||
138 | Excel::create($file_name, function ($excel) use($headers, $logs) { |
||
139 | $excel->sheet('Sheetname', function ($sheet) use($headers, $logs) { |
||
140 | $sheet->appendRow($headers); |
||
141 | foreach ($logs as $log) |
||
142 | { |
||
143 | $tmp = []; |
||
144 | $tmp[] = $log->user ? $log->user['name'] : ''; |
||
145 | $tmp[] = $log->request_method ?: ''; |
||
146 | $tmp[] = $log->request_url ?: ''; |
||
147 | $tmp[] = $log->module ?: ''; |
||
148 | $tmp[] = $log->requested_start_at ? date('Y-m-d h:i:s', intval($log->requested_start_at / 1000)): ''; |
||
149 | $tmp[] = $log->requested_end_at ? date('Y-m-d h:i:s', intval($log->requested_end_at / 1000)) : ''; |
||
150 | $tmp[] = $log->exec_time ?: ''; |
||
151 | $tmp[] = $log->request_source_ip ?: ''; |
||
152 | $tmp[] = $log->request_user_agent ?: ''; |
||
153 | $tmp[] = $log->request_body ? json_encode($log->request_body) : ''; |
||
154 | $sheet->appendRow($tmp); |
||
155 | } |
||
156 | }); |
||
157 | })->download('xls'); |
||
158 | } |
||
159 | } |
||
160 |
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: