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

TransformerAbstract::transform()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Transformer;
6
7
use Illuminate\Support\Arr;
8
use League\Fractal\TransformerAbstract as BaseTransformerAbstract;
9
use Sarala\Links;
10
use Sarala\Query\Fields;
11
12
abstract class TransformerAbstract extends BaseTransformerAbstract
13
{
14
    const REQUIRED_FIELDS = [
15
        'id',
16
    ];
17
18
    public function transform($data)
19
    {
20
        $user = auth(config('sarala.guard'))->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
21
22
        $links = $this->links($data, $user)->all();
23
        $meta = $this->meta($data, $user);
24
        $data = $this->filterFields($this->data($data));
0 ignored issues
show
Bug introduced by
The method data() does not seem to exist on object<Sarala\Transformer\TransformerAbstract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
26
        if (! empty($links)) {
27
            $data['links'] = $links;
28
        }
29
30
        if (! empty($meta)) {
31
            $data['meta'] = $meta;
32
        }
33
34
        return $data;
35
    }
36
37
    protected function links($model, $user = null): Links
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        return Links::make();
40
    }
41
42
    protected function meta($model, $user = null): array
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        return [];
45
    }
46
47
    private function filterFields(array $data): array
48
    {
49
        $resourceName = $this->getCurrentScope()->getResource()->getResourceKey();
50
        /** @var Fields $fields */
51
        $fields = request()->fields();
52
53
        if (! $fields->has($resourceName)) {
54
            return $data;
55
        }
56
57
        $fields = array_merge($fields->get($resourceName), self::REQUIRED_FIELDS);
58
59
        return Arr::only($data, $fields);
60
    }
61
}
62