Completed
Pull Request — master (#31)
by ARCANEDEV
01:30
created

VerifyJsonRequest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
ccs 15
cts 16
cp 0.9375
wmc 7
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 2
A isJsonRequestValid() 0 10 2
A getMethods() 0 9 3
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
11
/**
12
 * Class     VerifyJsonRequest
13
 *
14
 * @package  Arcanedev\Support\Middleware
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class VerifyJsonRequest extends Middleware
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /**
24
     *
25
     * @var array
26
     */
27
    protected $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
28
29
    /* ------------------------------------------------------------------------------------------------
30
     |  Main Functions
31
     | ------------------------------------------------------------------------------------------------
32
     */
33
    /**
34
     * Handle an incoming request.
35
     *
36
     * @param  \Illuminate\Http\Request  $request
37
     * @param  \Closure                  $next
38
     * @param  string|array|null         $methods
39
     *
40
     * @return mixed
41
     */
42 18
    public function handle(Request $request, Closure $next, $methods = null)
43
    {
44 18
        if ($this->isJsonRequestValid($request, $methods)) {
45 12
            return $next($request);
46
        }
47
48 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...
49 6
            'status'  => 'error',
50
            'code'    => 400,
51
            'message' => 'Request must be json',
52 6
        ], 400);
53
    }
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Check Functions
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Validate json Request.
61
     *
62
     * @param  Request            $request
63
     * @param  string|array|null  $methods
64
     *
65
     * @return bool
66
     */
67 18
    private function isJsonRequestValid(Request $request, $methods)
68
    {
69 18
        $methods = $this->getMethods($methods);
70
71 18
        if ( ! in_array($request->method(), $methods)) {
72
            return false;
73
        }
74
75 18
        return $request->isJson();
76
    }
77
78
    /**
79
     * Get request methods.
80
     *
81
     * @param  string|array|null  $methods
82
     *
83
     * @return array
84
     */
85 18
    private function getMethods($methods)
86
    {
87 18
        $methods = $methods ?? $this->methods;
88
89 18
        if (is_string($methods))
90 12
            $methods = (array) $methods;
91
92 18
        return is_array($methods) ? array_map('strtoupper', $methods) : [];
93
    }
94
}
95