Completed
Push — master ( 870cb1...1f9632 )
by Milroy
07:07 queued 05:53
created

BaseController::responseCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Http\Controllers;
6
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8
use Illuminate\Http\JsonResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\Routing\Controller;
11
use League\Fractal\Manager;
12
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
13
use League\Fractal\Resource\Collection;
14
use League\Fractal\Resource\Item;
15
use Sarala\Query\ApiRequestInspector;
16
17
class BaseController extends Controller
18
{
19
    protected $manager;
20
21
    public function __construct(Manager $manager, Request $request)
22
    {
23
        if ($request->filled('include')) {
24
            $manager->parseIncludes($request->get('include'));
25
        }
26
27
        if ($request->filled('exclude')) {
28
            $manager->parseExcludes($request->get('exclude'));
29
        }
30
31
        $this->manager = $manager;
32
    }
33
34
    public function callAction($method, $parameters)
35
    {
36
        if (request()->filled('include')) {
37
            (new ApiRequestInspector($parameters))->inspect();
38
        }
39
40
        return parent::callAction($method, $parameters);
41
    }
42
43
    public function responseItem($object, $transformer, $resourceKey, $status = 200): JsonResponse
44
    {
45
        $resource = new Item($object, $transformer, $resourceKey);
46
47
        return $this->response($this->manager->createData($resource)->toArray(), $status);
48
    }
49
50
    public function responseCollection($collection, $transformer, $resourceKey, $status = 200): JsonResponse
51
    {
52
        $resource = new Collection($collection, $transformer, $resourceKey);
53
54
        if ($collection instanceof LengthAwarePaginator) {
55
            $resource->setPaginator(new IlluminatePaginatorAdapter($collection));
56
        }
57
58
        return $this->response($this->manager->createData($resource)->toArray(), $status);
59
    }
60
61
    private function response($data, $status)
62
    {
63
        return response()->json($data, $status);
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...
64
    }
65
}
66