Completed
Push — master ( 4caca7...1176eb )
by Milroy
01:50
created

ApiException::getResponseData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Exceptions;
6
7
use Exception;
8
use Illuminate\Http\Request;
9
use Illuminate\Http\JsonResponse;
10
use Illuminate\Http\Response;
11
12
abstract class ApiException extends Exception implements JsonApiExceptionContract
13
{
14
    /**
15
     * Get unique identifier for this particular occurrence
16
     * of the problem.
17
     */
18
    public function id(): string
19
    {
20
        return '';
21
    }
22
23
    /**
24
     * Get application-specific error code.
25
     */
26
    public function code(): string
27
    {
28
        return '';
29
    }
30
31
    /**
32
     * Get human-readable explanation specific to this
33
     * occurrence of the problem.
34
     */
35
    public function detail(): string
36
    {
37
        return '';
38
    }
39
40
    /**
41
     * Get the URI that yield further details about this
42
     * particular occurrence of the problem.
43
     */
44
    public function href(): string
45
    {
46
        return '';
47
    }
48
49
    /**
50
     * Get associated resources, which can be dereferenced
51
     * from the request document.
52
     */
53
    public function links(): array
54
    {
55
        return [];
56
    }
57
58
    /**
59
     * Get relative path to the relevant attribute within
60
     * the associated resource(s).
61
     */
62
    public function path(): string
63
    {
64
        return '';
65
    }
66
67
    /**
68
     * Get non-standard meta-information about the error.
69
     */
70
    public function meta(): array
71
    {
72
        return [];
73
    }
74
75
    protected function getResponseData(): array
76
    {
77
        $error = array_merge([
78
            'status' => (string) $this->status(),
79
            'title' => (string) $this->title(),
80
        ], $this->getAvailableData());
81
82
        return [
83
            'errors' => [$error]
84
        ];
85
    }
86
87
    protected function getApiResponse(): JsonResponse
88
    {
89
        return response()
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
90
            ->json($this->getResponseData(), $this->status());
91
    }
92
93
    public function render(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        return $this->getApiResponse();
96
    }
97
98
    private function getAvailableData(): array
99
    {
100
        $data = [];
101
102
        collect(['links', 'meta', 'id', 'href', 'code', 'detail', 'path'])->each(function ($key) use (&$data) {
103
            if (! empty($this->{$key}())) {
104
                $data[$key] = $this->{$key}();
105
            }
106
        });
107
108
        return $data;
109
    }
110
}
111