LogHandler   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 2
dl 0
loc 57
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 13 2
C translateError() 0 30 16
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SKM\Whoops\Handler;
6
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LogLevel;
9
use Whoops\Handler\Handler;
10
11
/**
12
 * @author Márk Sági-Kazár <[email protected]>
13
 */
14
final class LogHandler extends Handler
15
{
16
    /**
17
     * @var LoggerInterface
18
     */
19
    private $logger;
20
21 18
    public function __construct(LoggerInterface $logger)
22
    {
23 18
        $this->logger = $logger;
24 18
    }
25
26 17
    public function handle(): int
27
    {
28 17
        $exception = $this->getException();
29 17
        $level = LogLevel::CRITICAL;
30
31 17
        if ($exception instanceof \ErrorException) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
32 16
            $level = $this->translateError($exception);
33
        }
34
35 17
        $this->logger->log($level, $exception->getMessage().$exception->getTraceAsString());
36
37 17
        return Handler::DONE;
38
    }
39
40 16
    private function translateError(\ErrorException $exception): string
41
    {
42 16
        switch ($exception->getSeverity()) {
43 16
            case E_ERROR:
44 15
            case E_RECOVERABLE_ERROR:
45 14
            case E_CORE_ERROR:
46 13
            case E_COMPILE_ERROR:
47 12
            case E_USER_ERROR:
48 11
            case E_PARSE:
49 6
                return LogLevel::ERROR;
50
51 10
            case E_WARNING:
52 9
            case E_USER_WARNING:
53 8
            case E_CORE_WARNING:
54 7
            case E_COMPILE_WARNING:
55 4
                return LogLevel::WARNING;
56
57 6
            case E_NOTICE:
58 5
            case E_USER_NOTICE:
59 2
                return LogLevel::NOTICE;
60
61 4
            case E_STRICT:
62 3
            case E_DEPRECATED:
63 2
            case E_USER_DEPRECATED:
64 3
                return LogLevel::INFO;
65
66
            default:
67 1
                return LogLevel::CRITICAL;
68
        }
69
    }
70
}
71