Completed
Push — master ( 5f5fca...2fa1e0 )
by Bjørn
12:28 queued 02:24
created

WarningLoggerTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 76
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

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