Completed
Push — 6.0 ( bcbbbf...49864a )
by liu
03:05
created

Html   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 104
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C output() 0 66 14
A getFileInfo() 0 10 2
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
namespace think\debug;
13
14
use think\App;
15
use think\Response;
16
17
/**
18
 * 页面Trace调试
19
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
20
class Html
21
{
22
    protected $config = [
23
        'file' => '',
24
        'tabs' => ['base' => '基本', 'file' => '文件', 'info' => '流程', 'notice|error' => '错误', 'sql' => 'SQL', 'debug|log' => '调试'],
25
    ];
26
27
    // 实例化并传入参数
28
    public function __construct(array $config = [])
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
29
    {
30
        $this->config = array_merge($this->config, $config);
31
    }
32
33
    /**
34
     * 调试输出接口
35
     * @access public
36
     * @param  App      $app 应用实例
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
37
     * @param  Response $response Response对象
38
     * @param  array    $log 日志信息
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
39
     * @return bool|string
40
     */
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()) {
0 ignored issues
show
Bug introduced by
It seems like $accept can also be of type array; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        if (strpos(/** @scrutinizer ignore-type */ $accept, 'application/json') === 0 || $request->isAjax()) {
Loading history...
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,
0 ignored issues
show
Bug introduced by
It seems like $request->time() can also be of type double; however, parameter $timestamp of date() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            '请求信息' => date('Y-m-d H:i:s', /** @scrutinizer ignore-type */ $request->time()) . ' ' . $uri,
Loading history...
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',
0 ignored issues
show
Bug introduced by
The method getWriteTimes() does not exist on think\Cache. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            '缓存信息' => $app->cache->getReadTimes() . ' reads,' . $app->cache->/** @scrutinizer ignore-call */ getWriteTimes() . ' writes',
Loading history...
Bug introduced by
The method getReadTimes() does not exist on think\Cache. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            '缓存信息' => $app->cache->/** @scrutinizer ignore-call */ getReadTimes() . ' reads,' . $app->cache->getWriteTimes() . ' writes',
Loading history...
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
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
84
                    $trace[$title] = $base;
85
                    break;
86
                case 'file': // 文件信息
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
87
                    $trace[$title] = $info;
88
                    break;
89
                default: // 调试信息
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
90
                    if (strpos($name, '|')) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
91
                        // 多组信息
92
                        $names  = explode('|', $name);
93
                        $result = [];
94
                        foreach ($names as $item) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
95
                            $result = array_merge($result, $log[$item] ?? []);
96
                        }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
97
                        $trace[$title] = $result;
98
                    } else {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
99
                        $trace[$title] = $log[$name] ?? '';
100
                    }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
101
            }
102
        }
103
        // 调用Trace页面模板
104
        ob_start();
105
        include $this->config['file'] ?: __DIR__ . '/../../tpl/page_trace.tpl';
106
        return ob_get_clean();
107
    }
108
109
    /**
110
     * 获取文件加载信息
111
     * @access protected
112
     * @return integer|array
113
     */
114
    protected function getFileInfo()
115
    {
116
        $files = get_included_files();
117
        $info  = [];
118
119
        foreach ($files as $key => $file) {
120
            $info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )';
121
        }
122
123
        return $info;
124
    }
125
}
126