SystemFacade   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 159
rs 10
wmc 19

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setErrorHandler() 0 7 2
A registerShutdownFunction() 0 3 1
A flushOutputBuffer() 0 3 1
A cleanOutputBuffer() 0 3 1
A stopExecution() 0 3 1
A getErrorReportingLevel() 0 3 1
A restoreExceptionHandler() 0 3 1
A setHttpResponseCode() 0 10 2
A startOutputBuffering() 0 3 1
A sendHeaders() 0 11 3
A setExceptionHandler() 0 3 1
A getLastError() 0 3 1
A restoreErrorHandler() 0 3 1
A getOutputBufferLevel() 0 3 1
A endOutputBuffering() 0 3 1
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;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CodeCoverageIgnore was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
179
    }
180
}
181