1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of error-handler |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\ErrorHandler\Util; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\Attributes\CodeCoverageIgnore; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SystemFacade |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class SystemFacade |
21
|
|
|
{ |
22
|
|
|
public const ERROR_LEVELS_PHP_DEFAULTS = E_ALL; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sets the error handler for the application. |
27
|
|
|
* |
28
|
|
|
* @param callable $handler The function or method to handle the errors. |
29
|
|
|
* @param int $types The types of errors to handle (self::ERROR_LEVELS_PHP_DEFAULTS by default). |
30
|
|
|
* @return callable|null The previous error handler function, |
31
|
|
|
* or null if no previous handler was set. |
32
|
|
|
*/ |
33
|
|
|
public function setErrorHandler(callable $handler, int $types = self::ERROR_LEVELS_PHP_DEFAULTS): ?callable |
34
|
|
|
{ |
35
|
|
|
// Since PHP 5.4 the constant E_ALL contains all errors (even E_STRICT) |
36
|
|
|
if ($types === self::ERROR_LEVELS_PHP_DEFAULTS) { |
37
|
|
|
$types = E_ALL; |
38
|
|
|
} |
39
|
|
|
return set_error_handler($handler, $types); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sets the exception handler function. |
44
|
|
|
* |
45
|
|
|
* @param callable $handler The exception handler function to set. |
46
|
|
|
* @return callable|null The previous exception handler function, |
47
|
|
|
* or null if no previous handler was set. |
48
|
|
|
*/ |
49
|
|
|
public function setExceptionHandler(callable $handler): ?callable |
50
|
|
|
{ |
51
|
|
|
return set_exception_handler($handler); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function restoreExceptionHandler(): bool |
58
|
|
|
{ |
59
|
|
|
return restore_exception_handler(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function restoreErrorHandler(): bool |
66
|
|
|
{ |
67
|
|
|
return restore_error_handler(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param callable $function |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function registerShutdownFunction(callable $function): void |
76
|
|
|
{ |
77
|
|
|
register_shutdown_function($function); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Turns on output buffering. |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function startOutputBuffering(): bool |
86
|
|
|
{ |
87
|
|
|
return ob_start(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string|false |
92
|
|
|
*/ |
93
|
|
|
public function cleanOutputBuffer() |
94
|
|
|
{ |
95
|
|
|
return ob_get_clean(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function getOutputBufferLevel() |
102
|
|
|
{ |
103
|
|
|
return ob_get_level(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function endOutputBuffering() |
110
|
|
|
{ |
111
|
|
|
return ob_end_clean(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function flushOutputBuffer() |
118
|
|
|
{ |
119
|
|
|
flush(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
|
|
public function getErrorReportingLevel() |
126
|
|
|
{ |
127
|
|
|
return error_reporting(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array{type: int, message: string, file: string, line: int}|null |
132
|
|
|
*/ |
133
|
|
|
public function getLastError(): ?array |
134
|
|
|
{ |
135
|
|
|
return error_get_last(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param int $httpCode |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function setHttpResponseCode($httpCode): bool |
144
|
|
|
{ |
145
|
|
|
if (!headers_sent()) { |
146
|
|
|
// Ensure that no 'location' header is present as otherwise this |
147
|
|
|
// will override the HTTP code being set here, and mask the |
148
|
|
|
// expected error page. |
149
|
|
|
header_remove('location'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return (bool) http_response_code($httpCode); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array<string, string> $headers |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function sendHeaders(array $headers): bool |
160
|
|
|
{ |
161
|
|
|
$done = false; |
162
|
|
|
if (!headers_sent()) { |
163
|
|
|
foreach ($headers as $name => $value) { |
164
|
|
|
header("$name: $value"); |
165
|
|
|
} |
166
|
|
|
$done = true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $done; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param int $exitStatus |
174
|
|
|
* @SuppressWarnings("PHPMD.ExitExpression") |
175
|
|
|
*/ |
176
|
|
|
public function stopExecution($exitStatus): void |
177
|
|
|
{ |
178
|
|
|
exit($exitStatus); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths