Passed
Push — master ( 564eb2...184cb3 )
by Waaaaaaaaaa
06:07
created

Stacktrace   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A inTrace() 0 10 6
A getFrames() 0 11 2
A getTrace() 0 12 2
1
<?php declare(strict_types=1);
2
3
namespace Logfile;
4
5
use Throwable;
6
7
class Stacktrace
8
{
9
    use PathTrait;
10
11
    /**
12
     * @var Throwable
13
     */
14
    protected $exception;
15
16
    /**
17
     * @param Throwable
18
     */
19
    public function __construct(Throwable $exception)
20
    {
21
        $this->exception = $exception;
22
    }
23
24
    protected function inTrace(array $frames): bool
25
    {
26
        foreach ($frames as $frame) {
27
            if (array_key_exists($frame, 'file') && $this->exception->getFile() == $frame['file'] &&
0 ignored issues
show
Bug introduced by
'file' of type string is incompatible with the type array expected by parameter $search of array_key_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
            if (array_key_exists($frame, /** @scrutinizer ignore-type */ 'file') && $this->exception->getFile() == $frame['file'] &&
Loading history...
28
                    array_key_exists($frame, 'line') && $this->exception->getLine() == $frame['line']) {
29
                return true;
30
            }
31
        }
32
33
        return false;
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getTrace(): array
40
    {
41
        $frames = $this->exception->getTrace();
42
43
        if (!$this->inTrace($frames)) {
44
            array_unshift($frames, [
45
                'file' => $this->exception->getFile(),
46
                'line' => $this->exception->getLine(),
47
            ]);
48
        }
49
50
        return $frames;
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    public function getFrames(): array
57
    {
58
        $frames = [];
59
60
        foreach ($this->getTrace() as $params) {
61
            $frame = Frame::create($params);
62
            $frame->setPath($this->getPath());
63
            $frames[] = $frame->toArray();
64
        }
65
66
        return $frames;
67
    }
68
}
69