Passed
Push — master ( cd4678...c3f0e5 )
by Radu
03:07
created

ErrorHandler   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 27
eloc 70
c 7
b 0
f 0
dl 0
loc 125
rs 10

6 Methods

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