Completed
Push — master ( 4604a2...31c42c )
by Radu
02:05
created

ErrorHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 3
eloc 11
c 6
b 0
f 0
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A throwErrorException() 0 22 1
A restore() 0 3 1
A set() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework;
6
7
final class ErrorHandler
8
{
9
    /**
10
     * Restores default error handler.
11
     */
12
    public static function restore(): bool
13
    {
14
        return \restore_error_handler();
15
    }
16
17
    /**
18
     * Registers as the error handler.
19
     */
20
    public static function set(): bool
21
    {
22
        \ini_set('display_errors', '0');
23
        \set_error_handler(['\WebServCo\Framework\ErrorHandler', 'throwErrorException']);
24
        return true;
25
    }
26
27
    /**
28
     * Throw ErrorException.
29
     *
30
     * @param int $errno Error level
31
     * @param string $errstr Error message
32
     * @param string $errfile Filename the error was raised in
33
     * @param int $errline Line number the error was raised at
34
     * @throws \ErrorException
35
     */
36
    public static function throwErrorException(int $errno, string $errstr, string $errfile, int $errline): bool
37
    {
38
        /* Handle error reporting disabled or supressed *
39
        // CURRENT SITUATION: ignore error reporting disabled or supressed, handle all errors
40
        // Custom error handler is called even if errors disable or supressed (@)
41
        // Code below handles this.
42
        // https://www.php.net/manual/en/language.operators.errorcontrol.php
43
        // https://www.php.net/manual/en/function.set-error-handler.php
44
        if (!(\error_reporting() & $errno)) { // bitwise operator, not a typo
45
            // This error code is not included in error_reporting, so let it fall
46
            // through to the standard PHP error handler
47
            return false;
48
        }
49
        /* Handle error reporting disabled or supressed */
50
51
        throw new \ErrorException(
52
            \sprintf('%s: %s.', \WebServCo\Framework\Helpers\ErrorTypeHelper::getString($errno), $errstr), // message
53
            0, // code
54
            $errno, // severity
55
            $errfile, // filename
56
            $errline, // lineno
57
            null, // previous
58
        );
59
    }
60
}
61