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