PhpLastErrorShutdownHandler::handleShutdown()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 10
cc 3
nc 2
nop 0
crap 12
1
<?php
2
3
/**
4
 * This file is part of coisa/error-handler.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\ErrorHandler\Handler;
15
16
/**
17
 * Class PhpLastErrorShutdownHandler
18
 *
19
 * @package CoiSA\ErrorHandler\Handler
20
 */
21
final class PhpLastErrorShutdownHandler implements ShutdownHandlerInterface
22
{
23
    /**
24
     * @var PhpErrorHandlerInterface
25
     */
26
    private $phpErrorHandler;
27
28
    /**
29
     * PhpLastErrorShutdownHandler constructor.
30
     *
31
     * @param PhpErrorHandlerInterface $phpErrorHandler
32
     */
33 17
    public function __construct(
34
        PhpErrorHandlerInterface $phpErrorHandler
35
    ) {
36 17
        $this->phpErrorHandler = $phpErrorHandler;
37 17
    }
38
39
    /**
40
     * Handle last php error
41
     */
42
    public function handleShutdown(): void
43
    {
44
        $error = \error_get_last();
45
46
        if (!\is_array($error) || !$this->isCatchableFatalError($error['type'])) {
47
            return;
48
        }
49
50
        $this->phpErrorHandler->handlePhpError(
51
            $error['type'],
52
            $error['message'],
53
            $error['file'],
54
            $error['line']
55
        );
56
    }
57
58
    /**
59
     * @param int $level
60
     *
61
     * @return bool
62
     */
63
    private function isCatchableFatalError(int $level)
64
    {
65
        $errors = E_ERROR;
66
        $errors |= E_PARSE;
67
        $errors |= E_CORE_ERROR;
68
        $errors |= E_CORE_WARNING;
69
        $errors |= E_COMPILE_ERROR;
70
        $errors |= E_COMPILE_WARNING;
71
72
        return ($level & $errors) > 0;
73
    }
74
}
75