Passed
Push — master ( 9eeeb5...ff15f0 )
by Radu
02:55
created

ErrorHandler::restore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
namespace WebServCo\Framework;
3
4
final class ErrorHandler
5
{
6
    public static function getErrorInfo($exception = null)
7
    {
8
        $errorInfo = [
9
            'code' => 0,
10
            'message' => null,
11
            'file' => null,
12
            'line' => null,
13
            'trace' => null,
14
            'exception' => null,
15
        ];
16
        if ($exception instanceof \Throwable ||
17
            $exception instanceof \Exception
18
        ) {
19
            $errorInfo['code'] = $exception->getCode();
20
            $errorInfo['message'] = $exception->getMessage();
21
            $errorInfo['file'] = $exception->getFile();
22
            $errorInfo['line'] = $exception->getLine();
23
            $errorInfo['trace'] = $exception->getTrace();
24
            $errorInfo['exception'] = $exception;
25
        } else {
26
            $last_error = error_get_last();
27
            if (!empty($last_error['message'])) {
28
                $errorInfo['message'] = $last_error['message'];
29
            }
30
            if (!empty($last_error['file'])) {
31
                $errorInfo['file'] = $last_error['file'];
32
            }
33
            if (!empty($last_error['line'])) {
34
                $errorInfo['line'] = $last_error['line'];
35
            }
36
        }
37
        return $errorInfo;
38
    }
39
40
    public static function getErrorTypeString($type)
41
    {
42
        switch ($type) {
43
            case E_ERROR: // 1
44
                return 'E_ERROR';
45
            case E_WARNING: // 2
46
                return 'Warning';
47
            case E_PARSE: // 4
48
                return 'E_PARSE';
49
            case E_NOTICE: // 8
50
                return 'Notice';
51
            case E_CORE_ERROR: // 16
52
                return 'E_CORE_ERROR';
53
            case E_CORE_WARNING: // 32
54
                return 'E_CORE_WARNING';
55
            case E_COMPILE_ERROR: // 64
56
                return 'E_COMPILE_ERROR';
57
            case E_COMPILE_WARNING: // 128
58
                return 'E_COMPILE_WARNING';
59
            case E_USER_ERROR: // 256
60
                return 'E_USER_ERROR';
61
            case E_USER_WARNING: // 512
62
                return 'E_USER_WARNING';
63
            case E_USER_NOTICE: // 1024
64
                return 'E_USER_NOTICE';
65
            case E_STRICT: // 2048
66
                return 'E_STRICT';
67
            case E_RECOVERABLE_ERROR: // 4096
68
                return 'E_RECOVERABLE_ERROR';
69
            case E_DEPRECATED: // 8192
70
                return 'Deprecated';
71
            case E_USER_DEPRECATED: // 16384
72
                return 'E_USER_DEPRECATED';
73
            case E_ALL: // 32767
74
                return 'E_ALL';
75
            default:
76
                return 'Unknown';
77
        }
78
    }
79
80
    /**
81
     * Restores default error handler.
82
     *
83
     * @return bool
84
     */
85
    public static function restore()
86
    {
87
        return restore_error_handler();
88
    }
89
90
    /**
91
     * Registers as the error handler.
92
     *
93
     * @return bool
94
     */
95
    public static function set()
96
    {
97
        self::disableErrorDisplay();
98
        set_error_handler(['\WebServCo\Framework\ErrorHandler', 'throwErrorException']);
99
        return true;
100
    }
101
102
    /**
103
     * Throws ErrorException.
104
     *
105
     * @param int $errno Error level
106
     * @param string $errstr Error message
107
     * @param string $errfile Filename the error was raised in
108
     * @param int $errline Line number the error was raised at
109
     *
110
     * @throws \ErrorException
111
     */
112
    public static function throwErrorException($errno, $errstr, $errfile, $errline)
113
    {
114
        if (error_reporting() & $errno) { //check if error level is set
115
            throw new \ErrorException(
116
                sprintf('%s: %s', self::getErrorTypeString($errno), $errstr),
117
                0,
118
                $errno,
119
                $errfile,
120
                $errline
121
            );
122
        }
123
    }
124
125
    /**
126
     * Disable error display.
127
     */
128
    protected static function disableErrorDisplay()
129
    {
130
        ini_set('display_errors', 0);
131
    }
132
}
133