Passed
Push — master ( 683a5e...33d5ac )
by Dmitriy
05:47 queued 01:43
created

ExceptionCollector::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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