GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#57)
by TJ
03:15
created

BacktraceFactory::formatBacktrace()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Honeybadger;
4
5
use Throwable;
6
use ReflectionClass;
7
use ReflectionException;
8
9
class BacktraceFactory
10
{
11
    /**
12
     * @var \Throwable
13
     */
14
    protected $exception;
15
16
    /**
17
     * @param   \Throwable  $exception
18
     */
19
    public function __construct(Throwable $exception)
20
    {
21
        $this->exception = $exception;
22
    }
23
24
    /**
25
     * @return array
26
     */
27
    public function trace() : array
28
    {
29
        $backtrace = $this->offsetForThrownException(
30
            $this->exception->getTrace()
31
        );
32
33
        return $this->formatBacktrace($backtrace);
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function previous() : array
40
    {
41
        return $this->formatPrevious($this->exception);
42
    }
43
44
    /**
45
     * @param  \Throwable  $e
46
     * @param  array  $previousCauses
47
     * @return array
48
     */
49
    private function formatPrevious(Throwable $e, array $previousCauses = []) : array
50
    {
51
        if ($e = $e->getPrevious()) {
52
            $previousCauses[] = [
53
                'class' => get_class($e),
54
                'message' => $e->getMessage(),
55
                'backtrace' => (new self($e))->trace(),
56
            ];
57
58
            return $this->formatPrevious($e, $previousCauses);
59
        }
60
61
        return $previousCauses;
62
    }
63
64
    /**
65
     * @param  array  $backtrace
66
     * @return array
67
     */
68
    private function offsetForThrownException(array $backtrace) : array
69
    {
70
        if (isset($backtrace[0])) {
71
            $backtrace[0] = array_merge($backtrace[0], [
72
                'line' => $this->exception->getLine(),
73
                'file' => $this->exception->getFile(),
74
            ]);
75
        }
76
77
        return $backtrace;
78
    }
79
80
    /**
81
     * @param  array  $backtrace
82
     * @return array
83
     */
84
    private function formatBacktrace(array $backtrace) : array
85
    {
86
        return array_map(function ($frame) {
87
            if (! array_key_exists('file', $frame)) {
88
                $context = $this->contextWithoutFile($frame);
89
            } else {
90
                $context = $this->contextWithFile($frame);
91
            }
92
93
            return array_merge($context, [
94
                'method' => $frame['function'] ?? null,
95
            ]);
96
        }, $backtrace);
97
    }
98
99
    /**
100
     * @param  array  $frame
101
     * @return array
102
     */
103
    private function contextWithoutFile(array $frame) : array
104
    {
105
        if (! empty($frame['class'])) {
106
            $filename = sprintf('%s%s%s', $frame['class'], $frame['type'], $frame['function']);
107
108
            try {
109
                $reflect = new ReflectionClass($frame['class']);
110
                $filename = $reflect->getFileName();
111
            } catch (ReflectionException $e) {
112
                // Forget it if we run into errors, it's not worth it.
113
            }
114
        } elseif (! empty($frame['function'])) {
115
            $filename = sprintf('%s(anonymous)', $frame['function']);
116
        } else {
117
            $filename = sprintf('(anonymous)');
118
        }
119
120
        if (empty($filename)) {
121
            $filename = '[Anonymous function]';
122
        }
123
124
        return [
125
            'source' => null,
126
            'file' => $filename,
127
            'number' => '0',
128
        ];
129
    }
130
131
    /**
132
     * @param  array  $frame
133
     * @return array
134
     */
135
    private function contextWithFile(array $frame) : array
136
    {
137
        return [
138
            'source' => (new FileSource($frame['file'], $frame['line']))->getSource(),
139
            'file' => $frame['file'],
140
            'number' => (string) $frame['line'],
141
        ];
142
    }
143
}
144