Completed
Branch 2.0 (63d421)
by Anton
05:39
created

ExceptionHandler::handleError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Framework;
10
11
use Spiral\Exceptions\AbstractHandler;
12
use Spiral\Exceptions\ConsoleHandler;
13
use Spiral\Exceptions\HtmlHandler;
14
use Spiral\Framework\Exceptions\FatalException;
15
16
/**
17
 * ExceptionHandler is responsible for global error handling (outside of dispatchers). Handler usually used in case
18
 * of bootload errors.
19
 */
20
final class ExceptionHandler
21
{
22
    /**
23
     * Enable global exception handling.
24
     */
25
    public static function register()
26
    {
27
        register_shutdown_function([self::class, 'handleShutdown']);
28
        set_error_handler([self::class, 'handleError']);
29
        set_exception_handler([self::class, 'handleException']);
30
    }
31
32
33
    /**
34
     * Handle php shutdown and search for fatal errors.
35
     */
36
    public static function handleShutdown()
37
    {
38
        if (!empty($error = error_get_last())) {
39
            self::handleException(
40
                new FatalException($error['message'], $error['type'], 0, $error['file'], $error['line'])
41
            );
42
        }
43
    }
44
45
    /**
46
     * Convert application error into exception.
47
     *
48
     * @param int    $code
49
     * @param string $message
50
     * @param string $filename
51
     * @param int    $line
52
     *
53
     * @throws \ErrorException
54
     */
55
    public static function handleError($code, $message, $filename = '', $line = 0)
56
    {
57
        throw new \ErrorException($message, $code, 0, $filename, $line);
58
    }
59
60
    /**
61
     * Handle exception and output error to the user.
62
     *
63
     * @param \Throwable $e
64
     */
65
    public static function handleException(\Throwable $e)
66
    {
67
        if (php_sapi_name() == 'cli') {
68
            $handler = new ConsoleHandler(STDERR);
0 ignored issues
show
Documentation introduced by
STDERR is of type string, but the function expects a boolean|resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
        } else {
70
            $handler = new HtmlHandler(HtmlHandler::INVERTED);
71
        }
72
73
        // we are safe to handle global exceptions (system level) with maximum verbosity
74
        fwrite(STDERR, $handler->renderException($e, AbstractHandler::VERBOSITY_VERBOSE));
75
    }
76
}