Test Failed
Push — master ( 5f8db2...b3d8d2 )
by Waaaaaaaaaa
02:26
created

Stacktrace   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
eloc 21
dl 0
loc 60
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A inTrace() 0 10 6
A __construct() 0 3 1
A getFrames() 0 11 2
A getTrace() 0 12 2
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