Response   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 1
dl 0
loc 26
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A linkResponse() 0 18 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace MuhamedDidovic\Shortener\Traits;
6
7
use MuhamedDidovic\Shortener\Models\Link;
8
9
/**
10
 * Trait Response.
11
 */
12
trait Response
13
{
14
    /**
15
     * @param Link  $link
16
     * @param array $merge
17
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
18
     */
19 9
    protected function linkResponse(Link $link, $merge = [])
20
    {
21 9
        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...
22
            [
23 9
                'data' => array_merge(
24
                    [
25 9
                        'original_url'   => $link->original_url,
26 9
                        'shortened_url'  => $link->shortenedUrl(),
27 9
                        'code'           => $link->code,
28 9
                        'last_requested' => $link->last_requested,
29 9
                        'last_used'      => $link->last_used ?? null,
30
                    ],
31
                    $merge
32
                ),
33
            ],
34 9
            200
35
        );
36
    }
37
}
38