Completed
Push — 2.1 ( c952e8...98ed49 )
by Carsten
10:00
created

ErrorException::isFatalError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 2
eloc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\base;
9
10
use Yii;
11
12
/**
13
 * ErrorException represents a PHP error.
14
 *
15
 * @author Alexander Makarov <[email protected]>
16
 * @since 2.0
17
 */
18
class ErrorException extends \ErrorException
19
{
20
    /**
21
     * This constant represents a fatal error in the HHVM engine.
22
     *
23
     * PHP Zend runtime won't call the error handler on fatals, HHVM will, with an error code of 16777217
24
     * We will handle fatal error a bit different on HHVM.
25
     * @see https://github.com/facebook/hhvm/blob/master/hphp/runtime/base/runtime-error.h#L62
26
     * @since 2.0.6
27
     */
28
    const E_HHVM_FATAL_ERROR = 16777217; // E_ERROR | (1 << 24)
29
30
31
    /**
32
     * Constructs the exception.
33
     * @link http://php.net/manual/en/errorexception.construct.php
34
     * @param $message [optional]
35
     * @param $code [optional]
36
     * @param $severity [optional]
37
     * @param $filename [optional]
38
     * @param $lineno [optional]
39
     * @param $previous [optional]
40
     */
41
    public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, \Exception $previous = null)
42
    {
43
        parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
44
45
        if (function_exists('xdebug_get_function_stack')) {
46
            $trace = array_slice(array_reverse(xdebug_get_function_stack()), 3, -1);
47
            foreach ($trace as &$frame) {
48
                if (!isset($frame['function'])) {
49
                    $frame['function'] = 'unknown';
50
                }
51
52
                // XDebug < 2.1.1: http://bugs.xdebug.org/view.php?id=695
53
                if (!isset($frame['type']) || $frame['type'] === 'static') {
54
                    $frame['type'] = '::';
55
                } elseif ($frame['type'] === 'dynamic') {
56
                    $frame['type'] = '->';
57
                }
58
59
                // XDebug has a different key name
60
                if (isset($frame['params']) && !isset($frame['args'])) {
61
                    $frame['args'] = $frame['params'];
62
                }
63
            }
64
65
            $ref = new \ReflectionProperty('Exception', 'trace');
66
            $ref->setAccessible(true);
67
            $ref->setValue($this, $trace);
68
        }
69
    }
70
71
    /**
72
     * Returns if error is one of fatal type.
73
     *
74
     * @param array $error error got from error_get_last()
75
     * @return boolean if error is one of fatal type
76
     */
77
    public static function isFatalError($error)
78
    {
79
        return isset($error['type']) && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, self::E_HHVM_FATAL_ERROR]);
80
    }
81
82
    /**
83
     * @return string the user-friendly name of this exception
84
     */
85
    public function getName()
86
    {
87
        static $names = [
88
            E_COMPILE_ERROR => 'PHP Compile Error',
89
            E_COMPILE_WARNING => 'PHP Compile Warning',
90
            E_CORE_ERROR => 'PHP Core Error',
91
            E_CORE_WARNING => 'PHP Core Warning',
92
            E_DEPRECATED => 'PHP Deprecated Warning',
93
            E_ERROR => 'PHP Fatal Error',
94
            E_NOTICE => 'PHP Notice',
95
            E_PARSE => 'PHP Parse Error',
96
            E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
97
            E_STRICT => 'PHP Strict Warning',
98
            E_USER_DEPRECATED => 'PHP User Deprecated Warning',
99
            E_USER_ERROR => 'PHP User Error',
100
            E_USER_NOTICE => 'PHP User Notice',
101
            E_USER_WARNING => 'PHP User Warning',
102
            E_WARNING => 'PHP Warning',
103
            self::E_HHVM_FATAL_ERROR => 'HHVM Fatal Error',
104
        ];
105
106
        return isset($names[$this->getCode()]) ? $names[$this->getCode()] : 'Error';
107
    }
108
}
109