Test Failed
Push — master ( 8e4667...0df70f )
by Jodie
02:22
created

Compiler::transformItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
4
namespace Rexlabs\Smokescreen\Scope;
5
6
7
use Rexlabs\Smokescreen\Exception\InvalidTransformerException;
8
use Rexlabs\Smokescreen\Helpers\StrHelper;
9
use Rexlabs\Smokescreen\Relations\RelationLoaderInterface;
10
use Rexlabs\Smokescreen\Resource\Item;
11
use Rexlabs\Smokescreen\Resource\ResourceInterface;
12
use Rexlabs\Smokescreen\Serializer\SerializerInterface;
13
use Rexlabs\Smokescreen\Transformer\TransformerInterface;
14
15
class Compiler
16
{
17
    /** @var Scope */
18
    protected $scope;
19
20
    /** @var SerializerInterface */
21
    protected $serializer;
22
23
    /** @var RelationLoaderInterface|null */
24
    protected $loader;
25
26
    public function __construct(Scope $scope, SerializerInterface $serializer, RelationLoaderInterface $loader = null)
27
    {
28
        $this->scope = $scope;
29
        $this->serializer = $serializer;
30
        $this->loader = $loader;
31
    }
32
33
    public function compile()
34
    {
35
        $output = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
36
37
38
    }
39
40
    /**
41
     * @param Scope $scope
42
     * @return array
43
     */
44
    protected function serialize(Scope $scope): array
45
    {
46
        // Relationship loading
47
        $this->loadRelations($scope->getResource());
48
49
        // Build the output by recursively transforming each resource
50
        return $scope->isCollection() ?
51
            $this->serializeCollection($scope) : $this->serializeItem($scope);
52
    }
53
54
    protected function loadRelations(ResourceInterface $resource)
55
    {
56
        if ($this->loader !== null) {
57
            $this->loader->load($resource);
58
        }
59
    }
60
61
    protected function serializeCollection(Scope $scope)
62
    {
63
        $items = array_map(function($data) use ($scope) {
64
65
            $scope = new Scope(
0 ignored issues
show
Unused Code introduced by
The assignment to $scope is dead and can be removed.
Loading history...
66
                new Item(
67
                    $data,
68
                    $scope->getResource()->getTransformer(),
69
                    $scope->getResource()->getResourceKey()
70
                ),
71
                $scope->getIncludes()
72
            );
73
74
            return $this->transformItem($item);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item seems to be never defined.
Loading history...
75
        }, $scope->getResource()->toArray());
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Rexlabs\Smokescreen\Resource\ResourceInterface. It seems like you code against a sub-type of Rexlabs\Smokescreen\Resource\ResourceInterface such as Rexlabs\Smokescreen\Resource\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        }, $scope->getResource()->/** @scrutinizer ignore-call */ toArray());
Loading history...
76
77
        // Serialize all the items
78
        $output = $this->serializer->collection(
79
            $collection->getResourceKey(),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $collection seems to be never defined.
Loading history...
80
            $items
81
        );
82
83
        // Merge in any pagination
84
        if ($collection->hasPaginator()) {
85
            $output = array_merge($output, $this->serializer->paginator($collection->getPaginator()));
86
        }
87
88
        return $output;
89
    }
90
91
    /**
92
     * @param Item $item
93
     *
94
     * @return array
95
     */
96
    protected function transformItem(Item $item): array
97
    {
98
        $transformer = $item->getTransformer();
0 ignored issues
show
Unused Code introduced by
The assignment to $transformer is dead and can be removed.
Loading history...
99
100
        return [];
101
    }
102
103
    protected function callTransformerIncludeMethodFor($transformer, $includeKey, $item)
104
    {
105
        return $transformer->{'include' . StrHelper::studlyCase($includeKey)}($item);
106
    }
107
108
    protected function serializeItem(Scope $scope)
109
    {
110
        $resource = $scope->getResource();
111
112
        return $this->serializer->item(
113
            $resource->getResourceKey(),
114
            $this->transformItem($resource->getData(), $resource->getTransformer())
0 ignored issues
show
Unused Code introduced by
The call to Rexlabs\Smokescreen\Scop...mpiler::transformItem() has too many arguments starting with $resource->getTransformer(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
            $this->/** @scrutinizer ignore-call */ 
115
                   transformItem($resource->getData(), $resource->getTransformer())

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
115
        );
116
    }
117
118
}