Completed
Push — master ( 0235df...db4a8b )
by Bjørn
12:10 queued 01:07
created

WarningLoggerTrait::warningHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 57
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 4
dl 0
loc 57
ccs 0
cts 21
cp 0
crap 6
rs 9.7
c 0
b 0
f 0

How to fix   Long Method   

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
namespace WebPConvert\Convert\BaseConverters\BaseTraits;
4
5
trait WarningLoggerTrait
6
{
7
    abstract protected function logLn($msg, $style = '');
8
9
    public $previousErrorHandler;
10
    /**
11
     *  Handle errors during conversion.
12
     *  The function is a callback used with "set_error_handler". It logs
13
     */
14
    protected function warningHandler($errno, $errstr, $errfile, $errline)
15
    {
16
        /*
17
        We do NOT do the following (even though it is generally recommended):
18
19
        if (!(error_reporting() & $errno)) {
20
            // This error code is not included in error_reporting, so let it fall
21
            // through to the standard PHP error handler
22
            return false;
23
        }
24
25
        - Because we want to log all warnings and errors (also the ones that was suppressed with @)
26
        https://secure.php.net/manual/en/language.operators.errorcontrol.php
27
        */
28
29
        $errorTypes = [
30
            E_WARNING =>             "Warning",
31
            E_NOTICE =>              "Notice",
32
            E_USER_ERROR =>          "User Error",
33
            E_USER_WARNING =>        "User Warning",
34
            E_USER_NOTICE =>         "User Notice",
35
            E_STRICT =>              "Strict Notice",
36
            E_DEPRECATED =>          "Deprecated",
37
            E_USER_DEPRECATED =>     "User Deprecated",
38
39
            /*
40
            The following can never be catched by a custom error handler:
41
            E_PARSE, E_ERROR, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
42
43
            We do do not currently trigger the following:
44
            E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
45
46
            But we may want to do that at some point, like this:
47
            trigger_error('Your version of Gd is very old', E_USER_WARNING);
48
            */
49
        ];
50
51
        if (isset($errorTypes[$errno])) {
52
            $errType = $errorTypes[$errno];
53
        } else {
54
            $errType = "Unknown error ($errno)";
55
        }
56
57
        $msg = $errType . ': ' . $errstr . ' in ' . $errfile . ', line ' . $errline . ', PHP ' . PHP_VERSION .
58
            ' (' . PHP_OS . ')';
59
        $this->logLn($msg);
60
61
        /*
62
        if ($errno == E_USER_ERROR) {
63
            // trigger error.
64
            // unfortunately, we can only catch user errors
65
            throw new ConversionFailedException('Uncaught error in converter', $msg);
66
        }*/
67
68
        //echo 'previously defined handler:' . print_r($this->previousErrorHandler, true);
69
70
        return call_user_func($this->previousErrorHandler, $errno, $errstr, $errfile, $errline);
71
        //return $this->previousErrorHandler;
72
    }
73
74
    /*
75
    public function get_error_handler(){
76
        $handler = set_error_handler(function(){});
77
        restore_error_handler();
78
        echo 'handler:' . $handler;
79
        return $handler;
80
    }*/
81
82
    protected function activateWarningLogger()
83
    {
84
        $this->previousErrorHandler = set_error_handler(
85
            array($this, "warningHandler"),
86
            E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE | E_USER_ERROR
87
        );
88
    }
89
90
    protected function deactivateWarningLogger()
91
    {
92
        restore_error_handler();
93
    }
94
}
95