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
Push — master ( f8a760...de8a52 )
by TJ
02:27
created

BacktraceFactory::parseArgs()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 10
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
        $backtrace[0] = array_merge($backtrace[0] ?? [], [
71
            'line' => $this->exception->getLine(),
72
            'file' => $this->exception->getFile(),
73
        ]);
74
75
        return $backtrace;
76
    }
77
78
    /**
79
     * @param  array  $backtrace
80
     * @return array
81
     */
82
    private function formatBacktrace(array $backtrace) : array
83
    {
84
        return array_map(function ($frame) {
85
            if (! array_key_exists('file', $frame)) {
86
                $context = $this->contextWithoutFile($frame);
87
            } else {
88
                $context = $this->contextWithFile($frame);
89
            }
90
91
            return array_merge($context, [
92
                'method' => $frame['function'] ?? null,
93
                'args' => $this->parseArgs($frame['args']),
94
            ]);
95
        }, $backtrace);
96
    }
97
98
    /**
99
     * Parse method arguments and make any transformations.
100
     *
101
     * @param array $args
102
     * @return array
103
     */
104
    private function parseArgs(array $args) : array
105
    {
106
        return array_map(function ($arg) {
107
            if (is_object($arg)) {
108
                return get_class($arg);
109
            }
110
111
            return $arg;
112
        }, $args);
113
    }
114
115
    /**
116
     * @param  array  $frame
117
     * @return array
118
     */
119
    private function contextWithoutFile(array $frame) : array
120
    {
121
        if (! empty($frame['class'])) {
122
            $filename = sprintf('%s%s%s', $frame['class'], $frame['type'], $frame['function']);
123
124
            try {
125
                $reflect = new ReflectionClass($frame['class']);
126
                $filename = $reflect->getFileName();
127
            } catch (ReflectionException $e) {
128
                // Forget it if we run into errors, it's not worth it.
129
            }
130
        } elseif (! empty($frame['function'])) {
131
            $filename = sprintf('%s(anonymous)', $frame['function']);
132
        } else {
133
            $filename = sprintf('(anonymous)');
134
        }
135
136
        if (empty($filename)) {
137
            $filename = '[Anonymous function]';
138
        }
139
140
        return [
141
            'source' => null,
142
            'file' => $filename,
143
            'number' => '0',
144
        ];
145
    }
146
147
    /**
148
     * @param  array  $frame
149
     * @return array
150
     */
151
    private function contextWithFile(array $frame) : array
152
    {
153
        return [
154
            'source' => (new FileSource($frame['file'], $frame['line']))->getSource(),
155
            'file' => $frame['file'],
156
            'number' => (string) $frame['line'],
157
        ];
158
    }
159
}
160