Passed
Push — 5.2 ( 5de8a0...f2908c )
by liu
02:29
created

Error::getExceptionHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: 麦当苗儿 <[email protected]> <http://zjzit.cn>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use think\console\Output as ConsoleOutput;
16
use think\exception\ErrorException;
17
use think\exception\Handle;
18
use Throwable;
19
20
class Error
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
21
{
22
    /** @var App */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
23
    protected $app;
24
25
    /**
26
     * 注册异常处理
27
     * @access public
28
     * @param App $app
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
29
     * @return void
30
     */
31
    public function register(App $app)
32
    {
33
        $this->app = $app;
34
        error_reporting(E_ALL);
35
        set_error_handler([$this, 'appError']);
36
        set_exception_handler([$this, 'appException']);
37
        register_shutdown_function([$this, 'appShutdown']);
38
    }
39
40
    /**
41
     * Exception Handler
42
     * @access public
43
     * @param  \Throwable $e
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public function appException(Throwable $e): void
46
    {
47
        $handler = $this->getExceptionHandler();
48
49
        $handler->report($e);
50
51
        if (PHP_SAPI == 'cli') {
52
            $handler->renderForConsole(new ConsoleOutput, $e);
53
        } else {
54
            $handler->render($e)->send();
55
        }
56
    }
57
58
    /**
59
     * Error Handler
60
     * @access public
61
     * @param  integer $errno   错误编号
62
     * @param  string  $errstr  详细错误信息
63
     * @param  string  $errfile 出错的文件
64
     * @param  integer $errline 出错行号
65
     * @throws ErrorException
66
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
67
    public function appError(int $errno, string $errstr, string $errfile = '', int $errline = 0): void
68
    {
69
        $exception = new ErrorException($errno, $errstr, $errfile, $errline);
70
71
        if (error_reporting() & $errno) {
72
            // 将错误信息托管至 think\exception\ErrorException
73
            throw $exception;
74
        }
75
    }
76
77
    /**
78
     * Shutdown Handler
79
     * @access public
80
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
81
    public function appShutdown(): void
82
    {
83
        if (!is_null($error = error_get_last()) && $this->isFatal($error['type'])) {
0 ignored issues
show
introduced by
The condition is_null($error = error_get_last()) is always false.
Loading history...
84
            // 将错误信息托管至think\ErrorException
85
            $exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']);
86
87
            $this->appException($exception);
88
        }
89
90
        // 写入日志
91
        $this->app->make(Log::class)->save();
92
    }
93
94
    /**
95
     * 确定错误类型是否致命
96
     *
97
     * @access protected
98
     * @param  int $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
99
     * @return bool
100
     */
101
    protected function isFatal(int $type): bool
102
    {
103
        return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
104
    }
105
106
    /**
107
     * Get an instance of the exception handler.
108
     *
109
     * @access protected
110
     * @return Handle
111
     */
112
    protected function getExceptionHandler()
113
    {
114
        return $this->app->make(Handle::class);
115
    }
116
}
117