Passed
Push — master ( 755731...71ac2a )
by Dmitriy
02:36
created

ExceptionCollector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollected() 0 14 3
A reset() 0 3 1
A collect() 0 8 2
A __construct() 0 2 1
A serializeException() 0 10 1
A getSummary() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use Throwable;
8
use Yiisoft\ErrorHandler\Event\ApplicationError;
9
10
final class ExceptionCollector implements SummaryCollectorInterface
11
{
12
    use CollectorTrait;
13
14
    private ?Throwable $exception = null;
15
16
    public function __construct(private TimelineCollector $timelineCollector)
17
    {
18
    }
19
20
    public function getCollected(): array
21
    {
22
        if ($this->exception === null) {
23
            return [];
24
        }
25
        $throwable = $this->exception;
26
        $exceptions = [
27
            $throwable,
28
        ];
29
        while (($throwable = $throwable->getPrevious()) !== null) {
30
            $exceptions[] = $throwable;
31
        }
32
33
        return array_map([$this, 'serializeException'], $exceptions);
34
    }
35
36
    public function collect(ApplicationError $error): void
37
    {
38
        if (!$this->isActive()) {
39
            return;
40
        }
41
42
        $this->exception = $error->getThrowable();
43
        $this->timelineCollector->collect($this, $error::class);
44
    }
45
46
    public function getSummary(): array
47
    {
48
        return [
49
            'exception' => $this->exception === null ? [] : [
50
                'class' => $this->exception::class,
51
                'message' => $this->exception->getMessage(),
52
                'file' => $this->exception->getFile(),
53
                'line' => $this->exception->getLine(),
54
                'code' => $this->exception->getCode(),
55
            ],
56
        ];
57
    }
58
59
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
60
    {
61
        $this->exception = null;
62
    }
63
64
    private function serializeException(Throwable $throwable): array
65
    {
66
        return [
67
            'class' => $throwable::class,
68
            'message' => $throwable->getMessage(),
69
            'file' => $throwable->getFile(),
70
            'line' => $throwable->getLine(),
71
            'code' => $throwable->getCode(),
72
            'trace' => $throwable->getTrace(),
73
            'traceAsString' => $throwable->getTraceAsString(),
74
        ];
75
    }
76
}
77