Skip to content

Commit bd06ba3

Browse files
add 5.5 compatibility
1 parent 97a72aa commit bd06ba3

1 file changed

Lines changed: 118 additions & 88 deletions

File tree

src/ErrorTypes.php

Lines changed: 118 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -9,85 +9,106 @@ class ErrorTypes
99
*
1010
* @var array[]
1111
*/
12-
protected static $ERROR_TYPES = [
13-
E_ERROR => [
14-
'name' => 'PHP Fatal Error',
15-
'severity' => 'error',
16-
],
17-
18-
E_WARNING => [
19-
'name' => 'PHP Warning',
20-
'severity' => 'warning',
21-
],
22-
23-
E_PARSE => [
24-
'name' => 'PHP Parse Error',
25-
'severity' => 'error',
26-
],
27-
28-
E_NOTICE => [
29-
'name' => 'PHP Notice',
30-
'severity' => 'info',
31-
],
32-
33-
E_CORE_ERROR => [
34-
'name' => 'PHP Core Error',
35-
'severity' => 'error',
36-
],
37-
38-
E_CORE_WARNING => [
39-
'name' => 'PHP Core Warning',
40-
'severity' => 'warning',
41-
],
42-
43-
E_COMPILE_ERROR => [
44-
'name' => 'PHP Compile Error',
45-
'severity' => 'error',
46-
],
47-
48-
E_COMPILE_WARNING => [
49-
'name' => 'PHP Compile Warning',
50-
'severity' => 'warning',
51-
],
52-
53-
E_USER_ERROR => [
54-
'name' => 'User Error',
55-
'severity' => 'error',
56-
],
57-
58-
E_USER_WARNING => [
59-
'name' => 'User Warning',
60-
'severity' => 'warning',
61-
],
62-
63-
E_USER_NOTICE => [
64-
'name' => 'User Notice',
65-
'severity' => 'info',
66-
],
67-
68-
// E_STRICT has been depreciated in PHP 8.4.
69-
// https://php.watch/versions/8.4/E_STRICT-deprecated
70-
// Adding the property conditionally, to silence the deprecation warning.
71-
(PHP_VERSION_ID < 80400 ? E_STRICT : null) => PHP_VERSION_ID < 80400 ? [
72-
'name' => 'PHP Strict',
73-
'severity' => 'info',
74-
] : null,
75-
76-
E_RECOVERABLE_ERROR => [
77-
'name' => 'PHP Recoverable Error',
78-
'severity' => 'error',
79-
],
80-
81-
E_DEPRECATED => [
82-
'name' => 'PHP Deprecated',
83-
'severity' => 'info',
84-
],
85-
86-
E_USER_DEPRECATED => [
87-
'name' => 'User Deprecated',
88-
'severity' => 'info',
89-
],
90-
];
12+
protected static $ERROR_TYPES;
13+
14+
/**
15+
* Static initializer to conditionally populate $ERROR_TYPES.
16+
*/
17+
protected static function initializeErrorTypes()
18+
{
19+
static::$ERROR_TYPES = [
20+
E_ERROR => [
21+
'name' => 'PHP Fatal Error',
22+
'severity' => 'error',
23+
],
24+
25+
E_WARNING => [
26+
'name' => 'PHP Warning',
27+
'severity' => 'warning',
28+
],
29+
30+
E_PARSE => [
31+
'name' => 'PHP Parse Error',
32+
'severity' => 'error',
33+
],
34+
35+
E_NOTICE => [
36+
'name' => 'PHP Notice',
37+
'severity' => 'info',
38+
],
39+
40+
E_CORE_ERROR => [
41+
'name' => 'PHP Core Error',
42+
'severity' => 'error',
43+
],
44+
45+
E_CORE_WARNING => [
46+
'name' => 'PHP Core Warning',
47+
'severity' => 'warning',
48+
],
49+
50+
E_COMPILE_ERROR => [
51+
'name' => 'PHP Compile Error',
52+
'severity' => 'error',
53+
],
54+
55+
E_COMPILE_WARNING => [
56+
'name' => 'PHP Compile Warning',
57+
'severity' => 'warning',
58+
],
59+
60+
E_USER_ERROR => [
61+
'name' => 'User Error',
62+
'severity' => 'error',
63+
],
64+
65+
E_USER_WARNING => [
66+
'name' => 'User Warning',
67+
'severity' => 'warning',
68+
],
69+
70+
E_USER_NOTICE => [
71+
'name' => 'User Notice',
72+
'severity' => 'info',
73+
],
74+
75+
E_RECOVERABLE_ERROR => [
76+
'name' => 'PHP Recoverable Error',
77+
'severity' => 'error',
78+
],
79+
80+
E_DEPRECATED => [
81+
'name' => 'PHP Deprecated',
82+
'severity' => 'info',
83+
],
84+
85+
E_USER_DEPRECATED => [
86+
'name' => 'User Deprecated',
87+
'severity' => 'info',
88+
],
89+
];
90+
91+
// Conditionally add E_STRICT if PHP version is below 8.4
92+
// https://php.watch/versions/8.4/E_STRICT-deprecated
93+
if (PHP_VERSION_ID < 80400) {
94+
static::$ERROR_TYPES[E_STRICT] = [
95+
'name' => 'PHP Strict',
96+
'severity' => 'info',
97+
];
98+
}
99+
}
100+
101+
/**
102+
* Get the error types map.
103+
*
104+
* @return array[]
105+
*/
106+
protected static function getErrorTypes()
107+
{
108+
if (static::$ERROR_TYPES === null)
109+
static::initializeErrorTypes();
110+
return static::$ERROR_TYPES;
111+
}
91112

92113
/**
93114
* Is the given error code fatal?
@@ -110,8 +131,10 @@ public static function isFatal($code)
110131
*/
111132
public static function getName($code)
112133
{
113-
if (array_key_exists($code, static::$ERROR_TYPES)) {
114-
return static::$ERROR_TYPES[$code]['name'];
134+
$errorTypes = static::getErrorTypes();
135+
136+
if (array_key_exists($code, $errorTypes)) {
137+
return $errorTypes[$code]['name'];
115138
}
116139

117140
return 'Unknown';
@@ -126,15 +149,17 @@ public static function getName($code)
126149
*/
127150
public static function getSeverity($code)
128151
{
129-
if (array_key_exists($code, static::$ERROR_TYPES)) {
130-
return static::$ERROR_TYPES[$code]['severity'];
152+
$errorTypes = static::getErrorTypes();
153+
154+
if (array_key_exists($code, $errorTypes)) {
155+
return $errorTypes[$code]['severity'];
131156
}
132157

133158
return 'error';
134159
}
135160

136161
/**
137-
* Get the the levels for the given severity.
162+
* Get the levels for the given severity.
138163
*
139164
* @param string $severity the given severity
140165
*
@@ -143,8 +168,9 @@ public static function getSeverity($code)
143168
public static function getLevelsForSeverity($severity)
144169
{
145170
$levels = 0;
171+
$errorTypes = static::getErrorTypes();
146172

147-
foreach (static::$ERROR_TYPES as $level => $info) {
173+
foreach ($errorTypes as $level => $info) {
148174
if ($info['severity'] == $severity) {
149175
$levels |= $level;
150176
}
@@ -160,7 +186,7 @@ public static function getLevelsForSeverity($severity)
160186
*/
161187
public static function getAllCodes()
162188
{
163-
return array_keys(self::$ERROR_TYPES);
189+
return array_keys(static::getErrorTypes());
164190
}
165191

166192
/**
@@ -189,10 +215,14 @@ public static function codeToString($code)
189215
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
190216
E_DEPRECATED => 'E_DEPRECATED',
191217
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
192-
(PHP_VERSION_ID < 80400 ? E_STRICT : null) => PHP_VERSION_ID < 80400 ? 'E_STRICT' : null,
193218
];
194219

220+
// Conditionally add E_STRICT if PHP version is below 8.4
221+
// https://php.watch/versions/8.4/E_STRICT-deprecated
222+
if (PHP_VERSION_ID < 80400) {
223+
$map[E_STRICT] = 'E_STRICT';
224+
}
225+
195226
return isset($map[$code]) ? $map[$code] : 'Unknown';
196227
}
197-
198228
}

0 commit comments

Comments
 (0)