Stacktrace::getFrames()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php declare(strict_types=1);
2
3
namespace Logfile\Inspection;
4
5
use Logfile\Traits;
6
use Throwable;
7
8
class Stacktrace
9
{
10
    use Traits\Path;
11
12
    /**
13
     * @var Throwable
14
     */
15
    protected $exception;
16
17
    /**
18
     * @param Throwable
19
     */
20
    public function __construct(Throwable $exception)
21
    {
22
        $this->exception = $exception;
23
    }
24
25
    protected function inTrace(array $frames): bool
26
    {
27
        foreach ($frames as $frame) {
28
            if (array_key_exists('file', $frame) && $this->exception->getFile() == $frame['file'] &&
29
                    array_key_exists('line', $frame) && $this->exception->getLine() == $frame['line']) {
30
                return true;
31
            }
32
        }
33
34
        return false;
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function getTrace(): array
41
    {
42
        $frames = $this->exception->getTrace();
43
44
        if (!$this->inTrace($frames)) {
45
            array_unshift($frames, [
46
                'file' => $this->exception->getFile(),
47
                'line' => $this->exception->getLine(),
48
            ]);
49
        }
50
51
        return $frames;
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getFrames(): array
58
    {
59
        $frames = [];
60
61
        foreach ($this->getTrace() as $params) {
62
            $frame = Frame::create($params);
63
            $frame->setPath($this->getPath());
64
            $frames[] = $frame->toArray();
65
        }
66
67
        return $frames;
68
    }
69
}
70