Completed
Push — master ( 6be619...d7df4f )
by Alexander
05:23
created

ErrorException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
eloc 2
nc 1
nop 6
1
<?php
2
3
namespace Yiisoft\Yii\Web\ErrorHandler;
4
5
/**
6
 * ErrorException represents a PHP error.
7
 */
8
class ErrorException extends \ErrorException implements FriendlyExceptionInterface
9
{
10
    private const ERROR_NAMES = [
11
        E_ERROR => 'PHP Fatal Error',
12
        E_WARNING => 'PHP Warning',
13
        E_PARSE => 'PHP Parse Error',
14
        E_NOTICE => 'PHP Notice',
15
        E_CORE_ERROR => 'PHP Core Error',
16
        E_CORE_WARNING => 'PHP Core Warning',
17
        E_COMPILE_ERROR => 'PHP Compile Error',
18
        E_COMPILE_WARNING => 'PHP Compile Warning',
19
        E_USER_ERROR => 'PHP User Error',
20
        E_USER_WARNING => 'PHP User Warning',
21
        E_USER_NOTICE => 'PHP User Notice',
22
        E_STRICT => 'PHP Strict Warning',
23
        E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
24
        E_DEPRECATED => 'PHP Deprecated Warning',
25
        E_USER_DEPRECATED => 'PHP User Deprecated Warning',
26
    ];
27
28
    public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, \Exception $previous = null)
29
    {
30
        parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
31
        $this->addXDebugTraceToFatalIfAvailable();
32
    }
33
34
    /**
35
     * Returns if error is one of fatal type.
36
     *
37
     * @param array $error error got from error_get_last()
38
     * @return bool if error is one of fatal type
39
     */
40
    public static function isFatalError(array $error): bool
41
    {
42
        return isset($error['type']) && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING], true);
43
    }
44
45
    /**
46
     * @return string the user-friendly name of this exception
47
     */
48
    public function getName(): string
49
    {
50
        return self::ERROR_NAMES[$this->getCode()] ?? 'Error';
51
    }
52
53
    /**
54
     * Fatal errors normally do not provide any trace making it harder to debug. In case XDebug is installed, we
55
     * can get a trace using xdebug_get_function_stack().
56
     */
57
    private function addXDebugTraceToFatalIfAvailable(): void
58
    {
59
        if (function_exists('xdebug_get_function_stack')) {
60
            // XDebug trace can't be modified and used directly with PHP 7
61
            // @see https://github.com/yiisoft/yii2/pull/11723
62
            $xDebugTrace = array_slice(array_reverse(xdebug_get_function_stack()), 1, -1);
63
            $trace = [];
64
            foreach ($xDebugTrace as $frame) {
65
                if (!isset($frame['function'])) {
66
                    $frame['function'] = 'unknown';
67
                }
68
69
                // XDebug < 2.1.1: http://bugs.xdebug.org/view.php?id=695
70
                if (!isset($frame['type']) || $frame['type'] === 'static') {
71
                    $frame['type'] = '::';
72
                } elseif ($frame['type'] === 'dynamic') {
73
                    $frame['type'] = '->';
74
                }
75
76
                // XDebug has a different key name
77
                if (isset($frame['params']) && !isset($frame['args'])) {
78
                    $frame['args'] = $frame['params'];
79
                }
80
                $trace[] = $frame;
81
            }
82
83
            $ref = new \ReflectionProperty('Exception', 'trace');
84
            $ref->setAccessible(true);
85
            $ref->setValue($this, $trace);
86
        }
87
    }
88
89
    public function getSolution(): ?string
90
    {
91
        return null;
92
    }
93
}
94