Completed
Push — master ( cf731b...184560 )
by ARCANEDEV
17s queued 11s
created

VerifyJsonRequest::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Support\Middleware;
6
7
use Arcanedev\Support\Http\Middleware;
8
use Closure;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
12
/**
13
 * Class     VerifyJsonRequest
14
 *
15
 * @package  Arcanedev\Support\Middleware
16
 * @author   ARCANEDEV <[email protected]>
17
 */
18
class VerifyJsonRequest extends Middleware
19
{
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Properties
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    /**
25
     *
26
     * @var array
27
     */
28
    protected $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
29
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Main Functions
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 response()->json([
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...
50 6
            'status'  => 'error',
51
            'code'    => Response::HTTP_BAD_REQUEST,
52
            'message' => 'Request must be JSON',
53 6
        ], Response::HTTP_BAD_REQUEST);
54
    }
55
56
    /* ------------------------------------------------------------------------------------------------
57
     |  Check Functions
58
     | ------------------------------------------------------------------------------------------------
59
     */
60
    /**
61
     * Validate json Request.
62
     *
63
     * @param  Request            $request
64
     * @param  string|array|null  $methods
65
     *
66
     * @return bool
67
     */
68 18
    private function isJsonRequestValid(Request $request, $methods)
69
    {
70 18
        $methods = $this->getMethods($methods);
71
72 18
        if ( ! in_array($request->method(), $methods)) {
73
            return false;
74
        }
75
76 18
        return $request->isJson();
77
    }
78
79
    /**
80
     * Get request methods.
81
     *
82
     * @param  string|array|null  $methods
83
     *
84
     * @return array
85
     */
86 18
    private function getMethods($methods)
87
    {
88 18
        $methods = $methods ?? $this->methods;
89
90 18
        if (is_string($methods))
91 12
            $methods = (array) $methods;
92
93 18
        return is_array($methods) ? array_map('strtoupper', $methods) : [];
94
    }
95
}
96