Conditions | 14 |
Paths | 50 |
Total Lines | 66 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 210 |
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 |
||
41 | public function output(App $app, Response $response, array $log = []) |
||
42 | { |
||
43 | $request = $app->request; |
||
44 | |||
45 | $contentType = $response->getHeader('Content-Type'); |
||
46 | $accept = $request->header('accept'); |
||
47 | if (strpos($accept, 'application/json') === 0 || $request->isAjax()) { |
||
48 | return false; |
||
49 | } elseif (!empty($contentType) && strpos($contentType, 'html') === false) { |
||
50 | return false; |
||
51 | } |
||
52 | |||
53 | // 获取基本信息 |
||
54 | $runtime = number_format(microtime(true) - $app->getBeginTime(), 10, '.', ''); |
||
55 | $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞'; |
||
56 | $mem = number_format((memory_get_usage() - $app->getBeginMem()) / 1024, 2); |
||
57 | |||
58 | // 页面Trace信息 |
||
59 | if ($request->host()) { |
||
60 | $uri = $request->protocol() . ' ' . $request->method() . ' : ' . $request->url(true); |
||
61 | } else { |
||
62 | $uri = 'cmd:' . implode(' ', $_SERVER['argv']); |
||
63 | } |
||
64 | |||
65 | $base = [ |
||
66 | '请求信息' => date('Y-m-d H:i:s', $request->time()) . ' ' . $uri, |
||
67 | '运行时间' => number_format((float) $runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()), |
||
68 | '查询信息' => $app->db->getQueryTimes() . ' queries', |
||
69 | '缓存信息' => $app->cache->getReadTimes() . ' reads,' . $app->cache->getWriteTimes() . ' writes', |
||
70 | ]; |
||
71 | |||
72 | if ($app->session->getId(false)) { |
||
73 | $base['会话信息'] = 'SESSION_ID=' . $app->session->getId(); |
||
74 | } |
||
75 | |||
76 | $info = $this->getFileInfo(); |
||
77 | |||
78 | // 页面Trace信息 |
||
79 | $trace = []; |
||
80 | foreach ($this->config['tabs'] as $name => $title) { |
||
81 | $name = strtolower($name); |
||
82 | switch ($name) { |
||
83 | case 'base': // 基本信息 |
||
1 ignored issue
–
show
|
|||
84 | $trace[$title] = $base; |
||
85 | break; |
||
86 | case 'file': // 文件信息 |
||
1 ignored issue
–
show
|
|||
87 | $trace[$title] = $info; |
||
88 | break; |
||
89 | default: // 调试信息 |
||
1 ignored issue
–
show
|
|||
90 | if (strpos($name, '|')) { |
||
1 ignored issue
–
show
|
|||
91 | // 多组信息 |
||
92 | $names = explode('|', $name); |
||
93 | $result = []; |
||
94 | foreach ($names as $item) { |
||
1 ignored issue
–
show
|
|||
95 | $result = array_merge($result, $log[$item] ?? []); |
||
96 | } |
||
1 ignored issue
–
show
|
|||
97 | $trace[$title] = $result; |
||
98 | } else { |
||
1 ignored issue
–
show
|
|||
99 | $trace[$title] = $log[$name] ?? ''; |
||
100 | } |
||
1 ignored issue
–
show
|
|||
101 | } |
||
102 | } |
||
103 | // 调用Trace页面模板 |
||
104 | ob_start(); |
||
105 | include $this->config['file'] ?: __DIR__ . '/../../tpl/page_trace.tpl'; |
||
106 | return ob_get_clean(); |
||
107 | } |
||
126 |