HumanReadableExceptionFormatter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 7.32 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 6
loc 82
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 8 2
B format() 6 59 8
A indentLines() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PolderKnowledge\LogModule\Formatter;
4
5
use Monolog\Formatter\NormalizerFormatter;
6
use WShafer\PSR11MonoLog\FactoryInterface;
7
8
/**
9
 * Format an Exception in a similar way PHP does by default when an exception bubbles to the top
10
 */
11
class HumanReadableExceptionFormatter extends NormalizerFormatter implements FactoryInterface
12
{
13
    public function __invoke(array $options)
14
    {
15
        if (array_key_exists('dateFormat', $options)) {
16
            return new self($options['dateFormat']);
17
        }
18
19
        return new self();
20
    }
21
22
    public function format(array $record): string
23
    {
24
        $throwable = $record['context']['exception'] ?? null;
25
26
        $record = parent::format($record);
27
28
        $result = sprintf(
29
            "[%s] %s.%s: %s\n\n",
30
            $record['datetime'],
31
            $record['channel'],
32
            $record['level_name'],
33
            $record['message']
34
        );
35
36
        if (isset($record['context']['file'])) {
37
            $result .= "[Context]\n\n";
38
39 View Code Duplication
            if (isset($record['context']['message'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
                $result .= sprintf("  Message: %s\n", $record['context']['message']);
41
            }
42
43
            $result .= sprintf("  File: %s\n", $record['context']['file']);
44
            $result .= sprintf("  Line: %d\n", $record['context']['line']);
45
46 View Code Duplication
            if (isset($record['context']['code'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
                $result .= sprintf("  Code: %s\n\n", $record['context']['code']);
48
            }
49
        }
50
51
        $exceptionCounter = 1;
52
53
        while ($throwable !== null) {
54
            $result .= sprintf("[Exception #%d]\n", $exceptionCounter++);
55
            $result .= sprintf("  Type: %s\n", get_class($throwable));
56
            $result .= sprintf("  Message: %s\n", $throwable->getMessage());
57
            $result .= sprintf("  Code: %d\n", $throwable->getCode());
58
            $result .= sprintf("  File: %s\n", $throwable->getFile());
59
            $result .= sprintf("  Line: %d\n\n", $throwable->getLine());
60
61
            $result .= "[Trace]\n";
62
            $result .= $this->indentLines($throwable->getTraceAsString());
63
            $result .= "\n\n";
64
65
            $throwable = $throwable->getPrevious();
66
        }
67
68
        foreach ($record['extra'] as $key => $params) {
69
            if (is_array($params) || is_object($params)) {
70
                $lines = json_encode($params, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
71
            } else {
72
                $lines = $params;
73
            }
74
75
            $result .= "[Extra - " . $key . "]\n";
76
            $result .= $this->indentLines($lines) . "\n\n";
77
        }
78
79
        return $result;
80
    }
81
82
    private function indentLines(string $input, int $spaces = 2)
83
    {
84
        $lines = explode("\n", $input);
85
86
        $indented = array_map(function ($item) use ($spaces) {
87
            return str_repeat(' ', $spaces) . $item;
88
        }, $lines);
89
90
        return implode("\n", $indented);
91
    }
92
}
93