Completed
Push — master ( ecd7bc...039dcf )
by Maik
01:58
created

ExceptionErrorHandler::errorHandler()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 26
cp 0
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 22
nc 10
nop 4
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function __construct()
30
    {
31
        $this->setErrorHandler();
32
    }
33
34
    private function setErrorHandler()
35
    {
36
        set_error_handler(array(
37
            $this,
38
            'errorHandler'
39
        ), E_ALL | E_STRICT);
40
    }
41
42
    public function errorHandler(int $err_severity, string $err_msg, string $err_file, int $err_line)
43
    {
44
        if (0 === error_reporting()) {
45
            return false;
46
        }
47
        
48
        switch ($err_severity) {
49
            case E_WARNING:
50
                throw new WarningException($err_msg, 0, $err_severity, $err_file, $err_line);
51
            case E_NOTICE:
52
                throw new NoticeException($err_msg, 0, $err_severity, $err_file, $err_line);
53
            case E_USER_ERROR:
54
                throw new UserErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
55
            case E_USER_WARNING:
56
                throw new UserWarningException($err_msg, 0, $err_severity, $err_file, $err_line);
57
            case E_USER_NOTICE:
58
                throw new UserNoticeException($err_msg, 0, $err_severity, $err_file, $err_line);
59
            case E_RECOVERABLE_ERROR:
60
                throw new RecoverableErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
61
            case E_DEPRECATED:
62
                throw new DeprecatedException($err_msg, 0, $err_severity, $err_file, $err_line);
63
            case E_USER_DEPRECATED:
64
                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
}