Test Failed
Pull Request — master (#13)
by
unknown
18:55
created

ApiResponse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 60
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A send() 0 4 1
A buildResponse() 0 9 1
1
<?php
2
3
namespace MedianetDev\LaravelAuthApi\Http\Helpers;
4
5
use Symfony\Component\HttpFoundation\Response as ResponseCode;
6
7
class ApiResponse
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
8
{
9
    /**
10
     * @var array
11
     */
12
    private $data;
13
14
    /**
15
     * @var bool
16
     */
17
    private $status;
18
19
    /**
20
     * @var int
21
     */
22
    private $responseCode;
23
24
    /**
25
     * @var string
26
     */
27
    private $message;
28
29
    private function __construct($data, $status, $responseCode, $message)
30
    {
31
        $this->data = $data;
32
        $this->status = $status;
33
        $this->responseCode = $responseCode;
34
        $this->message = $message ?: ResponseCode::$statusTexts[$responseCode];
35
    }
36
37
    /**
38
     * setup, build and return the response.
39
     *
40
     * @param array  $data         An array of returned data
41
     * @param int    $status       true or false for the operation status
42
     * @param int    $responseCode the Http response code
43
     * @param string $message      response message
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    public static function send($data, $status = true, $responseCode = ResponseCode::HTTP_OK, string $message = '')
48
    {
49
        return (new static($data, $status, $responseCode, $message))->buildResponse();
50
    }
51
52
    /**
53
     * build the response.
54
     *
55
     * @return \Illuminate\Http\Response
56
     */
57
    private function buildResponse()
58
    {
59
        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...
60
            'base_url' => \URL::to('/'),
61
            'status' => (bool) $this->status,
62
            'message' => $this->message,
63
            'data' => $this->data,
64
        ], $this->responseCode);
65
    }
66
}
67