ExceptionCollector::serializeException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
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(
17
        private readonly TimelineCollector $timelineCollector
18
    ) {
19
    }
20
21
    public function getCollected(): array
22
    {
23
        if (!$this->isActive()) {
24
            return [];
25
        }
26
        if ($this->exception === null) {
27
            return [];
28
        }
29
        $throwable = $this->exception;
30
        $exceptions = [
31
            $throwable,
32
        ];
33
        while (($throwable = $throwable->getPrevious()) !== null) {
34
            $exceptions[] = $throwable;
35
        }
36
37
        return array_map([$this, 'serializeException'], $exceptions);
38
    }
39
40
    public function collect(ApplicationError $error): void
41
    {
42
        if (!$this->isActive()) {
43
            return;
44
        }
45
46
        $this->exception = $error->getThrowable();
47
        $this->timelineCollector->collect($this, $error::class);
48
    }
49
50
    public function getSummary(): array
51
    {
52
        if (!$this->isActive()) {
53
            return [];
54
        }
55
        return [
56
            'exception' => $this->exception === null ? [] : [
57
                'class' => $this->exception::class,
58
                'message' => $this->exception->getMessage(),
59
                'file' => $this->exception->getFile(),
60
                'line' => $this->exception->getLine(),
61
                'code' => $this->exception->getCode(),
62
            ],
63
        ];
64
    }
65
66
    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...
67
    {
68
        $this->exception = null;
69
    }
70
71
    private function serializeException(Throwable $throwable): array
72
    {
73
        return [
74
            'class' => $throwable::class,
75
            'message' => $throwable->getMessage(),
76
            'file' => $throwable->getFile(),
77
            'line' => $throwable->getLine(),
78
            'code' => $throwable->getCode(),
79
            'trace' => $throwable->getTrace(),
80
            'traceAsString' => $throwable->getTraceAsString(),
81
        ];
82
    }
83
}
84