Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ErrorHandler
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
4 / 4
30
100.00% covered (success)
100.00%
1 / 1
 friendlyErrorType
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
17
 friendlyErrorSeverity
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
 getErrorSeverity
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
6
 handle
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\err;
10
11use pvc\err\stock\ErrorException;
12
13/**
14 * Class ErrorHandler
15 */
16class ErrorHandler
17{
18    protected int $errorExceptionCode = 0;
19
20    public const UNDEFINED = 0;
21    public const  NOTICE = 1;
22    public const  WARNING = 2;
23    public const  PARSE = 3;
24    public const  FATAL = 4;
25
26    public static function friendlyErrorType(int $type): string
27    {
28        return match ($type) {
29            E_ERROR => 'E_ERROR',
30            E_WARNING => 'E_WARNING',
31            E_PARSE => 'E_PARSE',
32            E_NOTICE => 'E_NOTICE',
33            E_CORE_ERROR => 'E_CORE_ERROR',
34            E_CORE_WARNING => 'E_CORE_WARNING',
35            E_COMPILE_ERROR => 'E_COMPILE_ERROR',
36            E_COMPILE_WARNING => 'E_COMPILE_WARNING',
37            E_USER_ERROR => 'E_USER_ERROR',
38            E_USER_WARNING => 'E_USER_WARNING',
39            E_USER_NOTICE => 'E_USER_NOTICE',
40            E_STRICT => 'E_STRICT',
41            E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
42            E_DEPRECATED => 'E_DEPRECATED',
43            E_USER_DEPRECATED => 'E_USER_DEPRECATED',
44            default => '(undefined error type)',
45        };
46    }
47
48    public static function friendlyErrorSeverity(int $severity): string
49    {
50        return match ($severity) {
51            self::NOTICE => 'NOTICE',
52            self::WARNING => 'WARNING',
53            self::PARSE => 'PARSE',
54            self::FATAL => 'FATAL',
55            default => '(undefined error severity)',
56        };
57    }
58
59    public static function getErrorSeverity(int $errno): int
60    {
61        return match ($errno) {
62            E_NOTICE, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED => self::NOTICE,
63            E_WARNING, E_CORE_WARNING, E_COMPILE_WARNING, E_USER_WARNING => self::WARNING,
64            E_PARSE => self::PARSE,
65            E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR => self::FATAL,
66            default => self::UNDEFINED,
67        };
68    }
69
70    public function handle(
71        int $errno,
72        string $errstr,
73        ?string $errfile = null,
74        ?int $errline = null,
75    ): void {
76        throw new ErrorException(
77            $errstr,
78            $this->errorExceptionCode,
79            $errno,
80            $errfile,
81            $errline
82        );
83    }
84}