DebugRenderer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 109
ccs 0
cts 33
cp 0
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 27 2
A setStatusCode() 0 3 1
A setResponseCode() 0 3 1
A formatTrace() 0 7 2
A setStatusMessage() 0 3 1
A setException() 0 3 1
1
<?php
2
3
namespace Napp\Core\Api\Exceptions\Renderer;
4
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Support\Arr;
7
8
/**
9
 * Class DebugRenderer.
10
 */
11
class DebugRenderer implements RendererInterface
12
{
13
    /**
14
     * @var \Exception
15
     */
16
    protected $exception;
17
18
    /**
19
     * @var int
20
     */
21
    protected $statusCode;
22
23
    /**
24
     * @var string
25
     */
26
    protected $statusMessage;
27
28
    /**
29
     * @var int
30
     */
31
    protected $responseCode;
32
33
    /**
34
     * @return JsonResponse
35
     */
36
    public function render(): JsonResponse
37
    {
38
        if ($this->exception instanceof \JsonSerializable) {
39
            return response()->json(array_merge_recursive(
40
                $this->exception->jsonSerialize(),
41
                [
42
                    'error' => [
43
                        'type'  => \get_class($this->exception),
44
                        'file'  => $this->exception->getFile(),
45
                        'line'  => $this->exception->getLine(),
46
                        'trace' => $this->formatTrace($this->exception->getTrace()),
47
                    ],
48
                ]),
49
                $this->responseCode);
50
        }
51
52
        return response()->json(
53
            [
54
                'error' => [
55
                    'code'    => $this->statusCode,
56
                    'message' => $this->statusMessage,
57
                    'type'    => \get_class($this->exception),
58
                    'file'    => $this->exception->getFile(),
59
                    'line'    => $this->exception->getLine(),
60
                    'trace'   => $this->formatTrace($this->exception->getTrace()),
61
                ], ],
62
            $this->responseCode
63
        );
64
    }
65
66
    /**
67
     * @param \Throwable $e
68
     *
69
     * @return void
70
     */
71
    public function setException(\Throwable $e)
72
    {
73
        $this->exception = $e;
0 ignored issues
show
Documentation Bug introduced by
$e is of type Throwable, but the property $exception was declared to be of type Exception. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
74
    }
75
76
    /**
77
     * @param int $responseCode
78
     *
79
     * @return void
80
     */
81
    public function setResponseCode($responseCode)
82
    {
83
        $this->responseCode = $responseCode;
84
    }
85
86
    /**
87
     * @param int $statusCode
88
     *
89
     * @return void
90
     */
91
    public function setStatusCode($statusCode)
92
    {
93
        $this->statusCode = $statusCode;
94
    }
95
96
    /**
97
     * @param string $statusMessage
98
     *
99
     * @return void
100
     */
101
    public function setStatusMessage($statusMessage)
102
    {
103
        $this->statusMessage = $statusMessage;
104
    }
105
106
    /**
107
     * Remove the args property from the trace array objects.
108
     *
109
     * @param array $trace
110
     *
111
     * @return array
112
     */
113
    protected function formatTrace(array $trace)
114
    {
115
        foreach ($trace as &$t) {
116
            $t = Arr::except($t, ['args']);
117
        }
118
119
        return $trace;
120
    }
121
}
122