Completed
Push — master ( 184560...9805df )
by ARCANEDEV
21s queued 18s
created

VerifyJsonRequest::jsonErrorResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 10
ccs 5
cts 5
cp 1
c 0
b 0
f 0
cc 1
nop 0
crap 1
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Support\Middleware;
6
7
use Closure;
8
use Illuminate\Http\{Request, Response};
9
10
/**
11
 * Class     VerifyJsonRequest
12
 *
13
 * @package  Arcanedev\Support\Middleware
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class VerifyJsonRequest
17
{
18
    /* -----------------------------------------------------------------
19
     |  Properties
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     *
25
     * @var array
26
     */
27
    protected $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
28
29
    /* -----------------------------------------------------------------
30
     |  Main Methods
31
     | -----------------------------------------------------------------
32
     */
33
34
    /**
35
     * Handle an incoming request.
36
     *
37
     * @param  \Illuminate\Http\Request  $request
38
     * @param  \Closure                  $next
39
     * @param  string|array|null         $methods
40
     *
41
     * @return mixed
42
     */
43 18
    public function handle(Request $request, Closure $next, $methods = null)
44
    {
45 18
        if ($this->isJsonRequestValid($request, $methods)) {
46 12
            return $next($request);
47
        }
48
49 6
        return $this->jsonErrorResponse();
50
    }
51
52
    /* -----------------------------------------------------------------
53
     |  Check Methods
54
     | -----------------------------------------------------------------
55
     */
56
57
    /**
58
     * Validate json Request.
59
     *
60
     * @param  Request            $request
61
     * @param  string|array|null  $methods
62
     *
63
     * @return bool
64
     */
65 18
    protected function isJsonRequestValid(Request $request, $methods)
66
    {
67 18
        $methods = $this->getMethods($methods);
68
69 18
        if ( ! in_array($request->method(), $methods)) {
70
            return false;
71
        }
72
73 18
        return $request->isJson();
74
    }
75
76
    /* -----------------------------------------------------------------
77
     |  Other Methods
78
     | -----------------------------------------------------------------
79
     */
80
81
    /**
82
     * Get the error as json response.
83
     *
84
     * @return \Illuminate\Http\JsonResponse
85
     */
86 6
    protected function jsonErrorResponse()
87
    {
88
        $data = [
89 6
            'status'  => 'error',
90 6
            'code'    => $statusCode = Response::HTTP_BAD_REQUEST,
91 6
            'message' => 'Request must be JSON',
92
        ];
93
94 6
        return response()->json($data, $statusCode);
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...
95
    }
96
97
    /**
98
     * Get request methods.
99
     *
100
     * @param  string|array|null  $methods
101
     *
102
     * @return array
103
     */
104 18
    protected function getMethods($methods): array
105
    {
106 18
        $methods = $methods ?? $this->methods;
107
108 18
        if (is_string($methods)) {
109 12
            $methods = (array) $methods;
110
        }
111
112 18
        return is_array($methods) ? array_map('strtoupper', $methods) : [];
113
    }
114
}
115