Completed
Push — master ( 0815a4...7ef1c7 )
by Freek
04:45 queued 11s
created

Report   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 261
rs 10
c 0
b 0
f 0
wmc 30
lcom 2
cbo 6

27 Methods

Rating   Name   Duplication   Size   Complexity  
A createForThrowable() 0 11 1
A getClassForThrowable() 0 9 3
A createForMessage() 0 12 1
A exceptionClass() 0 6 1
A getExceptionClass() 0 4 1
A throwable() 0 6 1
A getThrowable() 0 4 1
A message() 0 6 1
A getMessage() 0 4 1
A stacktrace() 0 6 1
A getStacktrace() 0 4 1
A notifierName() 0 6 1
A languageVersion() 0 6 1
A frameworkVersion() 0 6 1
A useContext() 0 6 1
A openFrameIndex() 0 6 1
A setApplicationPath() 0 6 1
A getApplicationPath() 0 4 1
A view() 0 6 1
A addGlow() 0 6 1
A addSolution() 0 6 1
A userProvidedContext() 0 6 1
A groupByTopFrame() 0 6 1
A groupByException() 0 6 1
A allContext() 0 8 1
A exceptionContext() 0 8 2
A toArray() 0 21 1
1
<?php
2
3
namespace Facade\FlareClient;
4
5
use Facade\FlareClient\Concerns\HasContext;
6
use Facade\FlareClient\Concerns\UsesTime;
7
use Facade\FlareClient\Context\ContextInterface;
8
use Facade\FlareClient\Contracts\ProvidesFlareContext;
9
use Facade\FlareClient\Enums\GroupingTypes;
10
use Facade\FlareClient\Glows\Glow;
11
use Facade\FlareClient\Solutions\ReportSolution;
12
use Facade\FlareClient\Stacktrace\Stacktrace;
13
use Facade\Ignition\Exceptions\ViewException;
14
use Facade\IgnitionContracts\Solution;
15
use Throwable;
16
17
class Report
18
{
19
    use UsesTime, HasContext;
20
21
    /** @var \Facade\FlareClient\Stacktrace\Stacktrace */
22
    private $stacktrace;
23
24
    /** @var string */
25
    private $exceptionClass;
26
27
    /** @var string */
28
    private $message;
29
30
    /** @var array */
31
    private $glows = [];
32
33
    /** @var array */
34
    private $solutions = [];
35
36
    /** @var ContextInterface */
37
    private $context;
38
39
    /** @var string */
40
    private $applicationPath;
41
42
    /** @var array */
43
    private $userProvidedContext = [];
44
45
    /** @var array */
46
    private $exceptionContext = [];
47
48
    /** @var Throwable */
49
    private $throwable;
50
51
    /** @var string */
52
    private $notifierName;
53
54
    /** @var string */
55
    private $languageVersion;
56
57
    /** @var string */
58
    private $frameworkVersion;
59
60
    /** @var int */
61
    private $openFrameIndex;
62
63
    /** @var string */
64
    private $groupBy;
65
66
    public static function createForThrowable(Throwable $throwable, ContextInterface $context, ?string $applicationPath = null): self
67
    {
68
        return (new static())
69
            ->setApplicationPath($applicationPath)
70
            ->throwable($throwable)
71
            ->useContext($context)
72
            ->exceptionClass(self::getClassForThrowable($throwable))
73
            ->message($throwable->getMessage())
74
            ->stackTrace(Stacktrace::createForThrowable($throwable, $applicationPath))
75
            ->exceptionContext($throwable);
76
    }
77
78
    protected static function getClassForThrowable(Throwable $throwable): string
79
    {
80
        if ($throwable instanceof \Facade\Ignition\Exceptions\ViewException) {
0 ignored issues
show
Bug introduced by
The class Facade\Ignition\Exceptions\ViewException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
81
            if ($previous = $throwable->getPrevious())
82
                return get_class($previous);
83
        }
84
85
        return get_class($throwable);
86
    }
87
88
    public static function createForMessage(string $message, string $logLevel, ContextInterface $context, ?string $applicationPath = null): self
89
    {
90
        $stacktrace = Stacktrace::create($applicationPath);
91
92
        return (new static())
93
            ->setApplicationPath($applicationPath)
94
            ->message($message)
95
            ->useContext($context)
96
            ->exceptionClass($logLevel)
97
            ->stacktrace($stacktrace)
98
            ->openFrameIndex($stacktrace->firstApplicationFrameIndex());
99
    }
100
101
    public function exceptionClass(string $exceptionClass)
102
    {
103
        $this->exceptionClass = $exceptionClass;
104
105
        return $this;
106
    }
107
108
    public function getExceptionClass(): string
109
    {
110
        return $this->exceptionClass;
111
    }
112
113
    public function throwable(Throwable $throwable)
114
    {
115
        $this->throwable = $throwable;
116
117
        return $this;
118
    }
119
120
    public function getThrowable(): ?Throwable
121
    {
122
        return $this->throwable;
123
    }
124
125
    public function message(string $message)
126
    {
127
        $this->message = $message;
128
129
        return $this;
130
    }
131
132
    public function getMessage(): string
133
    {
134
        return $this->message;
135
    }
136
137
    public function stacktrace(Stacktrace $stacktrace)
138
    {
139
        $this->stacktrace = $stacktrace;
140
141
        return $this;
142
    }
143
144
    public function getStacktrace(): Stacktrace
145
    {
146
        return $this->stacktrace;
147
    }
148
149
    public function notifierName(string $notifierName)
150
    {
151
        $this->notifierName = $notifierName;
152
153
        return $this;
154
    }
155
156
    public function languageVersion(string $languageVersion)
157
    {
158
        $this->languageVersion = $languageVersion;
159
160
        return $this;
161
    }
162
163
    public function frameworkVersion(string $frameworkVersion)
164
    {
165
        $this->frameworkVersion = $frameworkVersion;
166
167
        return $this;
168
    }
169
170
    public function useContext(ContextInterface $request)
171
    {
172
        $this->context = $request;
173
174
        return $this;
175
    }
176
177
    public function openFrameIndex(?int $index)
178
    {
179
        $this->openFrameIndex = $index;
180
181
        return $this;
182
    }
183
184
    public function setApplicationPath(?string $applicationPath)
185
    {
186
        $this->applicationPath = $applicationPath;
187
188
        return $this;
189
    }
190
191
    public function getApplicationPath(): ?string
192
    {
193
        return $this->applicationPath;
194
    }
195
196
    public function view(?View $view)
197
    {
198
        $this->view = $view;
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
199
200
        return $this;
201
    }
202
203
    public function addGlow(Glow $glow)
204
    {
205
        $this->glows[] = $glow->toArray();
206
207
        return $this;
208
    }
209
210
    public function addSolution(Solution $solution)
211
    {
212
        $this->solutions[] = ReportSolution::fromSolution($solution)->toArray();
213
214
        return $this;
215
    }
216
217
    public function userProvidedContext(array $userProvidedContext)
218
    {
219
        $this->userProvidedContext = $userProvidedContext;
220
221
        return $this;
222
    }
223
224
    public function groupByTopFrame()
225
    {
226
        $this->groupBy = GroupingTypes::TOP_FRAME;
227
228
        return $this;
229
    }
230
231
    public function groupByException()
232
    {
233
        $this->groupBy = GroupingTypes::EXCEPTION;
234
235
        return $this;
236
    }
237
238
    public function allContext(): array
239
    {
240
        $context = $this->context->toArray();
241
242
        $context = array_merge_recursive_distinct($context, $this->exceptionContext);
243
244
        return array_merge_recursive_distinct($context, $this->userProvidedContext);
245
    }
246
247
    private function exceptionContext(Throwable $throwable)
248
    {
249
        if ($throwable instanceof ProvidesFlareContext) {
250
            $this->exceptionContext = $throwable->context();
251
        }
252
253
        return $this;
254
    }
255
256
    public function toArray()
257
    {
258
        return [
259
            'notifier' => $this->notifierName ?? 'Flare Client',
260
            'language' => 'PHP',
261
            'framework_version' => $this->frameworkVersion,
262
            'language_version' => $this->languageVersion ?? phpversion(),
263
            'exception_class' => $this->exceptionClass,
264
            'seen_at' => $this->getCurrentTime(),
265
            'message' => $this->message,
266
            'glows' => $this->glows,
267
            'solutions' => $this->solutions,
268
            'stacktrace' => $this->stacktrace->toArray(),
269
            'context' => $this->allContext(),
270
            'stage' => $this->stage,
271
            'message_level' => $this->messageLevel,
272
            'open_frame_index' => $this->openFrameIndex,
273
            'group_by' => $this->groupBy ?? GroupingTypes::TOP_FRAME,
274
            'application_path' => $this->applicationPath,
275
        ];
276
    }
277
}
278