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

VerifyJsonRequest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 18
cts 19
cp 0.9474
c 0
b 0
f 0
rs 10
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 2
A isJsonRequestValid() 0 10 2
A jsonErrorResponse() 0 10 1
A getMethods() 0 10 3
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