Test Setup Failed
Push — v2 ( 74186a...c94b75 )
by Alexander
06:56
created

TransformerResolver::makeClosureTransformer()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Transformers;
4
5
use Flugg\Responder\Contracts\Transformers\TransformerResolver as TransformerResolverContract;
6
use Flugg\Responder\Contracts\Transformable;
7
use Flugg\Responder\Exceptions\InvalidTransformerException;
8
use Illuminate\Contracts\Container\Container;
9
use Illuminate\Contracts\Support\Arrayable;
10
11
/**
12
 * This class is responsible for resolving transformers.
13
 *
14
 * @package flugger/laravel-responder
15
 * @author  Alexander Tømmerås <[email protected]>
16
 * @license The MIT License
17
 */
18
class TransformerResolver implements TransformerResolverContract
19
{
20
    /**
21
     * An IoC container, used to resolve transformers.
22
     *
23
     * @var \Illuminate\Contracts\Container\Container
24
     */
25
    protected $container;
26
27
    /**
28
     * Transformable to transformer mappings.
29
     *
30
     * @var array
31
     */
32
    protected $bindings = [];
33
34
    /**
35
     * Construct the resolver class.
36
     *
37
     * @param \Illuminate\Contracts\Container\Container $container
38
     */
39
    public function __construct(Container $container)
40
    {
41
        $this->container = $container;
42
    }
43
44
    /**
45
     * Register a transformable to transformer binding.
46
     *
47
     * @param  string|array    $transformable
48
     * @param  string|callback $transformer
49
     * @return void
50
     */
51
    public function bind($transformable, $transformer)
52
    {
53
        $this->bindings = array_merge($this->bindings, is_array($transformable) ? $transformable : [
54
            $transformable => $transformer,
55
        ]);
56
    }
57
58
    /**
59
     * Resolve a transformer.
60
     *
61
     * @param  \Flugg\Responder\Transformers\Transformer|string|callable $transformer
62
     * @return \Flugg\Responder\Transformers\Transformer|callable
63
     * @throws \Flugg\Responder\Exceptions\InvalidTransformerException
64
     */
65
    public function resolve($transformer)
66
    {
67
        if (is_string($transformer)) {
68
            return $this->container->make($transformer);
69
        }
70
71
        if (! is_callable($transformer) && ! $transformer instanceof Transformer) {
72
            throw new InvalidTransformerException;
73
        }
74
75
        return $transformer;
76
    }
77
78
    /**
79
     * Resolve a transformer from the given data.
80
     *
81
     * @param  mixed $data
82
     * @return \Flugg\Responder\Transformers\Transformer|callable|null
83
     */
84
    public function resolveFromData($data)
85
    {
86
        if ($transformable = $this->resolveTransformable($data)) {
0 ignored issues
show
Unused Code introduced by
$transformable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
            if ($transformer = $this->resolveTransformer()) {
0 ignored issues
show
Bug introduced by
The call to resolveTransformer() misses a required argument $transformable.

This check looks for function calls that miss required arguments.

Loading history...
88
                return $this->resolve($transformer);
0 ignored issues
show
Documentation introduced by
$transformer is of type object<Flugg\Responder\Contracts\Transformable>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
            }
90
        }
91
92
        return $this->makeClosureTransformer();
93
    }
94
95
    /**
96
     * Resolve a transformable from the transformation data.
97
     *
98
     * @param  mixed $data
99
     * @return \Flugg\Responder\Contracts\Transformable|null
100
     */
101
    protected function resolveTransformable($data)
102
    {
103
        if (is_iterable($data)) {
104
            foreach ($data as $item) {
105
                if ($item instanceof Transformable) {
106
                    return $item;
107
                }
108
            }
109
        }
110
111
        return $data instanceof Transformable ? $data : null;
112
    }
113
114
    /**
115
     * Resolve a transformer from the transformable.
116
     *
117
     * @param  \Flugg\Responder\Contracts\Transformable $transformable
118
     * @return \Flugg\Responder\Contracts\Transformable|null
119
     */
120
    protected function resolveTransformer(Transformable $transformable)
121
    {
122
        if (key_exists($this->bindings, get_class($transformable))) {
123
            return $this->bindings[get_class($transformable)];
124
        }
125
126
        return $transformable->transformer();
0 ignored issues
show
Bug Compatibility introduced by
The expression $transformable->transformer(); of type callable|null adds the type callable to the return on line 126 which is incompatible with the return type documented by Flugg\Responder\Transfor...ver::resolveTransformer of type Flugg\Responder\Contracts\Transformable|null.
Loading history...
127
    }
128
129
    /**
130
     * Make a simple closure transformer.
131
     *
132
     * @return callable
133
     */
134
    protected function makeClosureTransformer(): callable
135
    {
136
        return function ($data) {
137
            return $data instanceof Arrayable ? $data->toArray() : $data;
138
        };
139
    }
140
}