ExceptionErrorHandler::errorHandler()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10.1

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 18
cts 20
cp 0.9
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 4
crap 10.1

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 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 8
    public function errorHandler(int $err_severity, string $err_msg, string $err_file, int $err_line)
43
    {
44 8
        if (0 === error_reporting()) {
45
            return false;
46
        }
47
        
48
        switch ($err_severity) {
49 8
            case E_WARNING:
50 1
                throw new WarningException($err_msg, 0, $err_severity, $err_file, $err_line);
51 7
            case E_NOTICE:
52 1
                throw new NoticeException($err_msg, 0, $err_severity, $err_file, $err_line);
53 6
            case E_USER_ERROR:
54 1
                throw new UserErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
55 5
            case E_USER_WARNING:
56 1
                throw new UserWarningException($err_msg, 0, $err_severity, $err_file, $err_line);
57 4
            case E_USER_NOTICE:
58 1
                throw new UserNoticeException($err_msg, 0, $err_severity, $err_file, $err_line);
59 3
            case E_RECOVERABLE_ERROR:
60 1
                throw new RecoverableErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
61 2
            case E_DEPRECATED:
62 1
                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
}
70