Completed
Push — master ( c4aa45...6dfe1b )
by Bjørn
03:12
created

WarningLoggerTrait::warningHandler()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 51
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0213

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 4
dl 0
loc 51
ccs 13
cts 15
cp 0.8667
crap 3.0213
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
    /** @var string|array|null  Previous error handler (stored in order to be able pass warnings on) */
10
    private $previousErrorHandler;
11
12
    /**
13
     *  Handle warnings and notices during conversion by logging them and passing them on.
14
     *
15
     *  The function is a callback used with "set_error_handler".
16
     *  It is declared public because it needs to be accessible from the point where the warning happened.
17
     *
18
     *  @param  integer  $errno
19
     *  @param  string   $errstr
20
     *  @param  string   $errfile
21
     *  @param  integer  $errline
22
     *
23
     *  @return false|null
24
     */
25 1
    public function warningHandler($errno, $errstr, $errfile, $errline)
26
    {
27
        /*
28
        We do NOT do the following (even though it is generally recommended):
29
30
        if (!(error_reporting() & $errno)) {
31
            // This error code is not included in error_reporting, so let it fall
32
            // through to the standard PHP error handler
33
            return false;
34
        }
35
36
        - Because we want to log all warnings and errors (also the ones that was suppressed with @)
37
        https://secure.php.net/manual/en/language.operators.errorcontrol.php
38
        */
39
40
        $errorTypes = [
41 1
            E_WARNING =>             "Warning",
42 1
            E_NOTICE =>              "Notice",
43 1
            E_STRICT =>              "Strict Notice",
44 1
            E_DEPRECATED =>          "Deprecated",
45 1
            E_USER_DEPRECATED =>     "User Deprecated",
46
47
            /*
48
            The following can never be catched by a custom error handler:
49
            E_PARSE, E_ERROR, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
50
51
            We do do not currently trigger the following:
52
            E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
53
54
            But we may want to do that at some point, like this:
55
            trigger_error('Your version of Gd is very old', E_USER_WARNING);
56
            in that case, remember to add them to this array
57
            */
58
        ];
59
60 1
        if (isset($errorTypes[$errno])) {
61 1
            $errType = $errorTypes[$errno];
62
        } else {
63
            $errType = "Unknown error/warning/notice ($errno)";
64
        }
65
66 1
        $msg = $errType . ': ' . $errstr . ' in ' . $errfile . ', line ' . $errline . ', PHP ' . PHP_VERSION .
67 1
            ' (' . PHP_OS . ')';
68 1
        $this->logLn($msg);
69
70
        //echo 'previously defined handler:' . print_r($this->previousErrorHandler, true);
71
72 1
        if (!is_null($this->previousErrorHandler)) {
73 1
            return call_user_func($this->previousErrorHandler, $errno, $errstr, $errfile, $errline);
74
        } else {
75
            return false;
76
        }
77
    }
78
79
    /**
80
     *  Activate warning logger.
81
     *
82
     *  Sets the error handler and stores the previous to our error handler can bubble up warnings
83
     *
84
     *  @return  void
85
     */
86 13
    protected function activateWarningLogger()
87
    {
88 13
        $this->previousErrorHandler = set_error_handler(
89 13
            array($this, "warningHandler"),
90 13
            E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE | E_USER_ERROR
91
        );
92 13
    }
93
94
    /**
95
     *  Deactivate warning logger.
96
     *
97
     *  Restores the previous error handler.
98
     *
99
     *  @return  void
100
     */
101 13
    protected function deactivateWarningLogger()
102
    {
103 13
        restore_error_handler();
104 13
    }
105
}
106