Completed
Push — master ( 417ea8...dd48e2 )
by Maik
01:38
created

ExceptionErrorHandler::setErrorHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Util;
9
10
use Generics\DeprecatedException;
11
use Generics\NoticeException;
12
use Generics\RecoverableErrorException;
13
use Generics\UserDeprecatedException;
14
use Generics\UserErrorException;
15
use Generics\UserNoticeException;
16
use Generics\UserWarningException;
17
use Generics\WarningException;
18
use ErrorException;
19
20
/**
21
 * This class provides a custom error handler for exception handling rather than internal error handling
22
 *
23
 * @author Maik Greubel <[email protected]>
24
 *        
25
 */
26
class ExceptionErrorHandler
27
{
28
29 8
    public function __construct()
30
    {
31 8
        $this->setErrorHandler();
32 8
    }
33
34 8
    private function setErrorHandler()
35
    {
36 8
        set_error_handler(array(
37 8
            $this,
38 8
            'errorHandler'
39 8
        ), E_ALL | E_STRICT);
40 8
    }
41
42 7
    public function errorHandler(int $err_severity, string $err_msg, string $err_file, int $err_line)
43
    {
44 7
        if (0 === error_reporting()) {
45
            return false;
46
        }
47
        
48
        switch ($err_severity) {
49 7
            case E_WARNING:
50 1
                throw new WarningException($err_msg, 0, $err_severity, $err_file, $err_line);
51 6
            case E_NOTICE:
52 1
                throw new NoticeException($err_msg, 0, $err_severity, $err_file, $err_line);
53 5
            case E_USER_ERROR:
54 1
                throw new UserErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
55 4
            case E_USER_WARNING:
56 1
                throw new UserWarningException($err_msg, 0, $err_severity, $err_file, $err_line);
57 3
            case E_USER_NOTICE:
58 1
                throw new UserNoticeException($err_msg, 0, $err_severity, $err_file, $err_line);
59 2
            case E_RECOVERABLE_ERROR:
60 1
                throw new RecoverableErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
61 1
            case E_DEPRECATED:
62
                throw new DeprecatedException($err_msg, 0, $err_severity, $err_file, $err_line);
63 1
            case E_USER_DEPRECATED:
64 1
                throw new UserDeprecatedException($err_msg, 0, $err_severity, $err_file, $err_line);
65
            default:
66
                throw new ErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
67
        }
68
    }
69
}