Passed
Push — fix-tests ( 6dd538...727178 )
by Alexander
195:41 queued 192:18
created

ErrorException   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 93
ccs 18
cts 24
cp 0.75
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 31 9
A isFatalError() 0 3 2
A getName() 0 22 2
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
 * For more details and usage information on ErrorException, see the [guide article on handling errors](guide:runtime-handling-errors).
16
 *
17
 * @author Alexander Makarov <[email protected]>
18
 * @since 2.0
19
 */
20
class ErrorException extends \ErrorException
21
{
22
    /**
23
     * This constant represents a fatal error in the HHVM engine.
24
     *
25
     * PHP Zend runtime won't call the error handler on fatals, HHVM will, with an error code of 16777217
26
     * We will handle fatal error a bit different on HHVM.
27
     * @see https://github.com/facebook/hhvm/blob/master/hphp/runtime/base/runtime-error.h#L62
28
     * @since 2.0.6
29
     */
30
    const E_HHVM_FATAL_ERROR = 16777217; // E_ERROR | (1 << 24)
31
32
33
    /**
34
     * Constructs the exception.
35
     * @link https://secure.php.net/manual/en/errorexception.construct.php
36
     * @param $message [optional]
37
     * @param $code [optional]
38
     * @param $severity [optional]
39
     * @param $filename [optional]
40
     * @param $lineno [optional]
41
     * @param $previous [optional]
42
     */
43 1
    public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, \Exception $previous = null)
44
    {
45 1
        parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
46
47 1
        if (function_exists('xdebug_get_function_stack')) {
48
            // XDebug trace can't be modified and used directly with PHP 7
49
            // @see https://github.com/yiisoft/yii2/pull/11723
50 1
            $xDebugTrace = array_slice(array_reverse(xdebug_get_function_stack()), 1, -1);
51 1
            $trace = [];
52 1
            foreach ($xDebugTrace as $frame) {
53 1
                if (!isset($frame['function'])) {
54
                    $frame['function'] = 'unknown';
55
                }
56
57
                // XDebug < 2.1.1: http://bugs.xdebug.org/view.php?id=695
58 1
                if (!isset($frame['type']) || $frame['type'] === 'static') {
59 1
                    $frame['type'] = '::';
60 1
                } elseif ($frame['type'] === 'dynamic') {
61 1
                    $frame['type'] = '->';
62
                }
63
64
                // XDebug has a different key name
65 1
                if (isset($frame['params']) && !isset($frame['args'])) {
66 1
                    $frame['args'] = $frame['params'];
67
                }
68 1
                $trace[] = $frame;
69
            }
70
71 1
            $ref = new \ReflectionProperty('Exception', 'trace');
72 1
            $ref->setAccessible(true);
73 1
            $ref->setValue($this, $trace);
74
        }
75 1
    }
76
77
    /**
78
     * Returns if error is one of fatal type.
79
     *
80
     * @param array $error error got from error_get_last()
81
     * @return bool if error is one of fatal type
82
     */
83
    public static function isFatalError($error)
84
    {
85
        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]);
86
    }
87
88
    /**
89
     * @return string the user-friendly name of this exception
90
     */
91
    public function getName()
92
    {
93
        static $names = [
94
            E_COMPILE_ERROR => 'PHP Compile Error',
95
            E_COMPILE_WARNING => 'PHP Compile Warning',
96
            E_CORE_ERROR => 'PHP Core Error',
97
            E_CORE_WARNING => 'PHP Core Warning',
98
            E_DEPRECATED => 'PHP Deprecated Warning',
99
            E_ERROR => 'PHP Fatal Error',
100
            E_NOTICE => 'PHP Notice',
101
            E_PARSE => 'PHP Parse Error',
102
            E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
103
            E_STRICT => 'PHP Strict Warning',
104
            E_USER_DEPRECATED => 'PHP User Deprecated Warning',
105
            E_USER_ERROR => 'PHP User Error',
106
            E_USER_NOTICE => 'PHP User Notice',
107
            E_USER_WARNING => 'PHP User Warning',
108
            E_WARNING => 'PHP Warning',
109
            self::E_HHVM_FATAL_ERROR => 'HHVM Fatal Error',
110
        ];
111
112
        return isset($names[$this->getCode()]) ? $names[$this->getCode()] : 'Error';
113
    }
114
}
115