ApiResponse::success()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
4
namespace Savannabits\JetstreamInertiaGenerator\Helpers;
5
6
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8
use Illuminate\Support\Collection;
9
use Symfony\Component\HttpFoundation\HeaderBag;
10
11
class ApiResponse
12
{
13
    private $success=true, $message="Request Successful.", $payload=[], $code, $headers=[];
14
15
    /**
16
     * The operation was successful.
17
     * @return ApiResponse
18
     */
19
    public function success() {
20
        $this->success = true;
21
        if (!$this->code) {
22
            $this->code = 200;
23
        }
24
        return $this;
25
    }
26
27
    /**
28
     * The operation was not successful, we are returning an error
29
     * @return ApiResponse
30
     */
31
    public function failed() {
32
        $this->success = false;
33
        if (!$this->code) {
34
            $this->code = 500;
35
        }
36
        return $this;
37
    }
38
39
    /**
40
     * set the message to respond with
41
     * @param string $message
42
     * @return ApiResponse
43
     */
44
    public function message($message="") {
45
        $this->message = $message;
46
        return $this;
47
    }
48
49
    /**
50
     * Set the payload to the response
51
     * @param $payload | The data to send to the client
52
     * @return ApiResponse
53
     */
54
    public function payload($payload) {
55
        $this->payload = $payload;
56
        return $this;
57
    }
58
59
    /**
60
     * Set the response code according to the status of the response
61
     * @param int $httpCode
62
     * @return ApiResponse
63
     */
64
    public function code($httpCode=200) {
65
        $this->code = $httpCode;
66
        return $this;
67
    }
68
69
    /**
70
     * Set custom response headers if necessary
71
     * @param array|HeaderBag $headers
72
     */
73
    public function headers($headers) {
74
        $this->headers = $headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $headers can also be of type object<Symfony\Component...tpFoundation\HeaderBag>. However, the property $headers is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
        return $this;
76
    }
77
    public function send() {
78
        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...
79
            "success" => $this->success,
80
            "message" => $this->message,
81
            "payload" => $this->payload,
82
        ],$this->code)->withHeaders($this->headers);
83
    }
84
}
85