Passed
Push — fix-array-access ( 050ccc...ecf3a7 )
by Alexander
56:12 queued 49:24
created

ErrorException::isXdebugStackAvailable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.5923

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 0
loc 19
ccs 6
cts 9
cp 0.6667
crap 4.5923
rs 10
c 0
b 0
f 0
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 string $message [optional]
37
     * @param int $code [optional]
38
     * @param int $severity [optional]
39
     * @param string $filename [optional]
40
     * @param int $lineno [optional]
41
     * @param \Throwable|\Exception $previous [optional]
42
     */
43 1
    public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, $previous = null)
44
    {
45 1
        parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
46
47 1
        if ($this->isXdebugStackAvailable()) {
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
     * Ensures that Xdebug stack trace is available based on Xdebug version.
79
     * Idea taken from developer bishopb at https://github.com/rollbar/rollbar-php
80
     * @return bool
81
     */
82 1
    private function isXdebugStackAvailable()
83
    {
84 1
        if (!function_exists('xdebug_get_function_stack')) {
85
            return false;
86
        }
87
88
        // check for Xdebug being installed to ensure origin of xdebug_get_function_stack()
89 1
        $version = phpversion('xdebug');
90 1
        if ($version === false) {
91
            return false;
92
        }
93
94
        // Xdebug 2 and prior
95 1
        if (version_compare($version, '3.0.0', '<')) {
96 1
            return true;
97
        }
98
99
        // Xdebug 3 and later, proper mode is required
100
        return false !== strpos(ini_get('xdebug.mode'), 'develop');
101
    }
102
103
    /**
104
     * Returns if error is one of fatal type.
105
     *
106
     * @param array $error error got from error_get_last()
107
     * @return bool if error is one of fatal type
108
     */
109
    public static function isFatalError($error)
110
    {
111
        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]);
112
    }
113
114
    /**
115
     * @return string the user-friendly name of this exception
116
     */
117
    public function getName()
118
    {
119
        static $names = [
120
            E_COMPILE_ERROR => 'PHP Compile Error',
121
            E_COMPILE_WARNING => 'PHP Compile Warning',
122
            E_CORE_ERROR => 'PHP Core Error',
123
            E_CORE_WARNING => 'PHP Core Warning',
124
            E_DEPRECATED => 'PHP Deprecated Warning',
125
            E_ERROR => 'PHP Fatal Error',
126
            E_NOTICE => 'PHP Notice',
127
            E_PARSE => 'PHP Parse Error',
128
            E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
129
            E_STRICT => 'PHP Strict Warning',
130
            E_USER_DEPRECATED => 'PHP User Deprecated Warning',
131
            E_USER_ERROR => 'PHP User Error',
132
            E_USER_NOTICE => 'PHP User Notice',
133
            E_USER_WARNING => 'PHP User Warning',
134
            E_WARNING => 'PHP Warning',
135
            self::E_HHVM_FATAL_ERROR => 'HHVM Fatal Error',
136
        ];
137
138
        return isset($names[$this->getCode()]) ? $names[$this->getCode()] : 'Error';
139
    }
140
}
141