Passed
Push — 5.2 ( c6dbca...184417 )
by
unknown
02:35
created

Error::appShutdown()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 11
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\initializer;
14
15
use think\App;
16
use think\console\Output as ConsoleOutput;
17
use think\exception\ErrorException;
18
use think\exception\Handle;
19
use Throwable;
20
21
class Error
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
22
{
23
    /** @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...
24
    protected $app;
25
26
    /**
27
     * 注册异常处理
28
     * @access public
29
     * @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...
30
     * @return void
31
     */
32
    public function init(App $app)
33
    {
34
        $this->app = $app;
35
        error_reporting(E_ALL);
36
        set_error_handler([$this, 'appError']);
37
        set_exception_handler([$this, 'appException']);
38
        register_shutdown_function([$this, 'appShutdown']);
39
    }
40
41
    /**
42
     * Exception Handler
43
     * @access public
44
     * @param  \Throwable $e
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
45
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
46
    public function appException(Throwable $e): void
47
    {
48
        $handler = $this->getExceptionHandler();
49
50
        $handler->report($e);
51
52
        if ($this->app->runningInConsole()) {
53
            $handler->renderForConsole(new ConsoleOutput, $e);
54
        } else {
55
            $handler->render($e)->send();
56
        }
57
    }
58
59
    /**
60
     * Error Handler
61
     * @access public
62
     * @param  integer $errno   错误编号
63
     * @param  string  $errstr  详细错误信息
64
     * @param  string  $errfile 出错的文件
65
     * @param  integer $errline 出错行号
66
     * @throws ErrorException
67
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
68
    public function appError(int $errno, string $errstr, string $errfile = '', int $errline = 0): void
69
    {
70
        $exception = new ErrorException($errno, $errstr, $errfile, $errline);
71
72
        if (error_reporting() & $errno) {
73
            // 将错误信息托管至 think\exception\ErrorException
74
            throw $exception;
75
        }
76
    }
77
78
    /**
79
     * Shutdown Handler
80
     * @access public
81
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
82
    public function appShutdown(): void
83
    {
84
        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...
85
            // 将错误信息托管至think\ErrorException
86
            $exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']);
87
88
            $this->appException($exception);
89
        }
90
91
        // 写入日志
92
        $this->app->log->save();
93
    }
94
95
    /**
96
     * 确定错误类型是否致命
97
     *
98
     * @access protected
99
     * @param  int $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
100
     * @return bool
101
     */
102
    protected function isFatal(int $type): bool
103
    {
104
        return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
105
    }
106
107
    /**
108
     * Get an instance of the exception handler.
109
     *
110
     * @access protected
111
     * @return Handle
112
     */
113
    protected function getExceptionHandler()
114
    {
115
        return $this->app->make(Handle::class);
116
    }
117
}
118