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