LogHandler::translateError()   C
last analyzed

Complexity

Conditions 16
Paths 16

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 16

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 22
cts 22
cp 1
rs 5.0151
c 0
b 0
f 0
cc 16
eloc 23
nc 16
nop 1
crap 16

How to fix   Complexity   

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
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