ErrorHandler   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80.38%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 146
ccs 41
cts 51
cp 0.8038
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handleError() 0 16 4
B handleShutdown() 0 22 4
C getSystemError() 0 53 16
1
<?php
2
3
namespace Dazzle\Throwable;
4
5
use Dazzle\Throwable\Error\FatalError;
6
use Dazzle\Throwable\Error\NoticeError;
7
use Dazzle\Throwable\Error\WarningError;
8
9
abstract class ErrorHandler
10
{
11
    /**
12
     * @var int
13
     */
14
    const E_UNSUPPORTED = 8;
15
16
    /**
17
     * @var int
18
     */
19
    const E_ERROR = 4;
20
21
    /**
22
     * @var int
23
     */
24
    const E_WARNING = 2;
25
26
    /**
27
     * @var int
28
     */
29
    const E_NOTICE = 1;
30
31
    /**
32
     * @var string
33
     */
34
    protected static $errHandler = '\Dazzle\Throwable\Error::toString';
35
36
    /**
37
     * @var string
38
     */
39
    protected static $excHandler = '\Dazzle\Throwable\Exception::toString';
40
41
    /**
42
     * Invoke default Error Handler.
43
     *
44
     * @param int $code
45
     * @param string $message
46
     * @param string $file
47
     * @param int $line
48
     * @throws FatalError
49
     * @throws NoticeError
50
     * @throws WarningError
51
     */
52 16
    public static function handleError($code, $message, $file, $line)
53
    {
54 16
        $list = static::getSystemError($code);
0 ignored issues
show
Bug introduced by
Since getSystemError() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getSystemError() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
55 16
        $name = $list[0];
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56 16
        $type = $list[1];
57
58 16
        $message = "\"$message\" in $file:$line";
59
60
        switch ($type)
61
        {
62 16
            case static::E_NOTICE:  throw new NoticeError($message);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
63 12
            case static::E_WARNING: throw new WarningError($message);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
64 7
            case static::E_ERROR:   throw new FatalError($message);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
65 1
            default:                return;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
66
        }
67
    }
68
69
    /**
70
     * Invoke default Shutdown Handler.
71
     *
72
     * @param bool $forceKill
73
     */
74
    public static function handleShutdown($forceKill = false)
75
    {
76
        $err = error_get_last();
77
78
        try
79
        {
80
            static::handleError($err['type'], $err['message'], $err['file'], $err['line']);
81
        }
82
        catch (\Error $ex)
0 ignored issues
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
83
        {
84
            echo call_user_func(static::$errHandler, $ex) . PHP_EOL;
85
        }
86
        catch (\Exception $ex)
87
        {
88
            echo call_user_func(static::$excHandler, $ex) . PHP_EOL;
89
        }
90
91
        if ($forceKill)
92
        {
93
            posix_kill(posix_getpid(), 9);
94
        }
95
    }
96
97
    /**
98
     * @param int $type
99
     * @return array
100
     */
101 16
    private static function getSystemError($type)
102
    {
103
        switch($type)
104
        {
105 16
            case E_ERROR: // 1 //
106 1
                return [ 'E_ERROR',             static::E_ERROR ];
107
108 15
            case E_WARNING: // 2 //
109 1
                return [ 'E_WARNING',           static::E_WARNING ];
110
111 14
            case E_PARSE: // 4 //
112 1
                return [ 'E_PARSE',             static::E_ERROR ];
113
114 13
            case E_NOTICE: // 8 //
115 1
                return [ 'E_NOTICE',            static::E_NOTICE ];
116
117 12
            case E_CORE_ERROR: // 16 //
118 1
                return [ 'E_CORE_ERROR',        static::E_ERROR ];
119
120 11
            case E_CORE_WARNING: // 32 //
121 1
                return [ 'E_CORE_WARNING',      static::E_WARNING ];
122
123 10
            case E_COMPILE_ERROR: // 64 //
124 1
                return [ 'E_COMPILE_ERROR',     static::E_ERROR ];
125
126 9
            case E_COMPILE_WARNING: // 128 //
127 1
                return [ 'E_COMPILE_WARNING',   static::E_WARNING ];
128
129 8
            case E_USER_ERROR: // 256 //
130 1
                return [ 'E_USER_ERROR',        static::E_ERROR ];
131
132 7
            case E_USER_WARNING: // 512 //
133 1
                return [ 'E_USER_WARNING',      static::E_WARNING ];
134
135 6
            case E_USER_NOTICE: // 1024 //
136 1
                return [ 'E_USER_NOTICE',       static::E_NOTICE ];
137
138 5
            case E_STRICT: // 2048 //
139 1
                return [ 'E_STRICT',            static::E_ERROR ];
140
141 4
            case E_RECOVERABLE_ERROR: // 4096 //
142 1
                return [ 'E_RECOVERABLE_ERROR', static::E_WARNING ];
143
144 3
            case E_DEPRECATED: // 8192 //
145 1
                return [ 'E_DEPRECATED',        static::E_NOTICE ];
146
147 2
            case E_USER_DEPRECATED: // 16384 //
148 1
                return [ 'E_USER_DEPRECATED',   static::E_NOTICE ];
149
150
            default:
151 1
                return [ 'E_UNKNOWN',           static::E_UNSUPPORTED ];
152
        }
153
    }
154
}
155