Completed
Push — master ( 68a553...e9f254 )
by Milroy
01:21
created

BaseController::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Http\Controllers;
6
7
use League\Fractal\Manager;
8
use Illuminate\Http\Request;
9
use Illuminate\Http\JsonResponse;
10
use League\Fractal\Resource\Item;
11
use Illuminate\Routing\Controller;
12
use Sarala\Query\ApiRequestInspector;
13
use League\Fractal\Resource\Collection;
14
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
15
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
16
17
class BaseController extends Controller
18
{
19
    protected $fractal;
20
21
    public function __construct(Manager $manager, Request $request)
22
    {
23
        if ($request->filled('fields')) {
24
            $manager->parseFieldsets($request->get('fields'));
25
        }
26
27
        if ($request->filled('include')) {
28
            $manager->parseIncludes($request->get('include'));
29
        }
30
31
        if ($request->filled('exclude')) {
32
            $manager->parseExcludes($request->get('exclude'));
33
        }
34
35
        $this->fractal = $manager;
36
    }
37
38
    public function callAction($method, $parameters)
39
    {
40
        (new ApiRequestInspector($parameters))->inspect();
41
42
        return call_user_func_array([$this, $method], $parameters);
43
    }
44
45
    protected function responseItem($object, $transformer, $resourceKey = null): JsonResponse
46
    {
47
        $resource = new Item($object, $transformer, $resourceKey);
48
49
        return $this->response($this->fractal->createData($resource)->toArray());
0 ignored issues
show
Bug introduced by
It seems like $this->fractal->createData($resource)->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Sarala\Http\Controllers\BaseController::response() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
    }
51
52
    protected function responseCollection($collection, $transformer, $resourceKey = null): JsonResponse
53
    {
54
        $resource = new Collection($collection, $transformer, $resourceKey);
55
56
        if ($collection instanceof LengthAwarePaginator) {
57
            $resource->setPaginator(new IlluminatePaginatorAdapter($collection));
58
        }
59
60
        return $this->response($this->fractal->createData($resource)->toArray());
0 ignored issues
show
Bug introduced by
It seems like $this->fractal->createData($resource)->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Sarala\Http\Controllers\BaseController::response() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
    }
62
63
    private function response($data = [], $status = 200, $headers = [])
64
    {
65
        return response()->json($data, $status, array_merge(config('sarala.response_headers'), $headers));
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...
66
    }
67
}
68