TransformerAdapter::transform()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 4
nop 4
dl 0
loc 25
rs 8.8571
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: xuan
6
 * Date: 9/24/15
7
 * Time: 12:08 PM.
8
 */
9
namespace PHPHub\Transformers\IncludeManager;
10
11
use Dingo\Api\Http\Request;
12
use Dingo\Api\Transformer\Adapter\Fractal;
13
use Dingo\Api\Transformer\Binding;
14
use Dingo\Api\Contract\Transformer\Adapter;
15
use Illuminate\Contracts\Pagination\Paginator as IlluminatePaginator;
16
17
class TransformerAdapter extends Fractal implements Adapter
18
{
19
    public function transform($response, $transformer, Binding $binding, Request $request)
20
    {
21
        // 过滤掉未允许的 include 项
22
        $include_manager = app(IncludeManager::class);
23
        $this->fractal->parseIncludes($include_manager->figureOutWhichIncludes());
24
25
        $resource = $this->createResource($response, $transformer, $binding->getParameters());
26
27
        // If the response is a paginator then we'll create a new paginator
28
        // adapter for Laravel and set the paginator instance on our
29
        // collection resource.
30
        if ($response instanceof IlluminatePaginator) {
31
            $paginator = $this->createPaginatorAdapter($response);
32
33
            $resource->setPaginator($paginator);
0 ignored issues
show
Bug introduced by
The method setPaginator does only exist in League\Fractal\Resource\Collection, but not in League\Fractal\Resource\Item.

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...
34
        }
35
36
        foreach ($binding->getMeta() as $key => $value) {
37
            $resource->setMetaValue($key, $value);
38
        }
39
40
        $binding->fireCallback($resource, $this->fractal);
41
42
        return $this->fractal->createData($resource)->toArray();
43
    }
44
}
45