Parametrizable::parametersToArray()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Gregoriohc\Protean\Common\Concerns;
4
5
use ArrayIterator;
6
use Gregoriohc\Protean\Common\Exceptions\InvalidParametersException;
7
use Gregoriohc\Protean\Common\Validation\Validator;
8
9
trait Parametrizable
10
{
11
    /**
12
     * @var array
13
     */
14
    private $parameters = [];
15
16
    /**
17
     * Initialize request with parameters
18
     *
19
     * @param array $parameters
20
     */
21 39
    public function bootParameters($parameters = [])
22
    {
23 39
        $this->parameters = array_replace($this->defaultParameters(), $parameters);
24 39
    }
25
26
    /**
27
     * @return array
28
     */
29 24
    public function defaultParameters()
30
    {
31 24
        return [];
32
    }
33
34
    /**
35
     * @return array
36
     */
37 18
    public function parametersValidationRules()
38
    {
39 18
        return [];
40
    }
41
42
    /**
43
     * @param array $extraRules
44
     */
45 27
    public function validateParameters($extraRules = [])
46
    {
47 27
        $rules = array_merge($this->parametersValidationRules(), $extraRules);
48
49 27
        $validator = Validator::make($this->allParameters(), $rules, $this);
50
51 27
        if ($validator->fails()) {
0 ignored issues
show
Bug introduced by
The method fails does only exist in Illuminate\Validation\Validator, but not in Gregoriohc\Protean\Common\Validation\Validator.

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...
52 12
            $messages = [];
53 12
            foreach ($validator->messages()->all() as $message) {
0 ignored issues
show
Bug introduced by
The method messages does only exist in Illuminate\Validation\Validator, but not in Gregoriohc\Protean\Common\Validation\Validator.

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...
54 12
                $messages[] = $message;
55
            }
56 12
            $class = class_basename($this);
57 12
            throw new InvalidParametersException("'$class' validation failed: " . implode(' ', $messages));
58
        }
59 24
    }
60
61
    /**
62
     * @param string $key
63
     * @param mixed $default
64
     * @return mixed
65
     */
66 6
    public function getParameter($key, $default = null)
67
    {
68 6
        return array_get($this->parameters, $key, $default);
69
    }
70
71
    /**
72
     * @param string $key
73
     * @param mixed $value
74
     * @return array
75
     */
76 3
    public function setParameter($key, $value)
77
    {
78 3
        return array_set($this->parameters, $key, $value);
79
    }
80
81
    /**
82
     * @param string|array $keys
83
     * @return bool
84
     */
85 6
    public function hasParameter($keys)
86
    {
87 6
        return array_has($this->parameters, $keys);
88
    }
89
90
    /**
91
     * @param string|array $keys
92
     */
93 3
    public function unsetParameter($keys)
94
    {
95 3
        array_forget($this->parameters, $keys);
96 3
    }
97
98
    /**
99
     * @return array
100
     */
101 33
    public function allParameters()
102
    {
103 33
        return $this->parameters;
104
    }
105
106
    /**
107
     * @return array
108
     */
109 12
    public function parametersToArray()
110
    {
111 12
        $data = $this->parameters;
112
113 12
        foreach ($data as $key => $value) {
114 12
            if (is_object($value)) {
115 12
                if (in_array(Parametrizable::class, class_uses_recursive($value))) {
116
                    /** @var Parametrizable $value */
117 9
                    $data[$key] = $value->parametersToArray();
118
                } else {
119 12
                    $data[$key] = (array) $value;
120
                }
121
            }
122
        }
123
124 12
        return $data;
125
    }
126
127
    /**
128
     * @param $map
129
     * @param bool $skipMissing
130
     * @return array
131
     */
132 3
    public function mapParameters($map, $skipMissing = false)
133
    {
134 3
        $data = [];
135 3
        $parameters = $this->parametersToArray();
136
137 3
        foreach ($map as $mapKey => $parameter) {
138 3
            if ($skipMissing && !array_has($parameters, $parameter)) {
139 3
                continue;
140
            }
141 3
            array_set($data, $mapKey, array_get($parameters, $parameter));
142
        }
143
144 3
        return $data;
145
    }
146
147
    /**
148
     * @param string $name
149
     * @return mixed
150
     */
151 3
    public function __get($name)
152
    {
153 3
        return $this->getParameter($name);
154
    }
155
156
    /**
157
     * @param string $name
158
     * @param mixed $value
159
     */
160 3
    public function __set($name, $value)
161
    {
162 3
        $this->setParameter($name, $value);
163 3
    }
164
165
    /**
166
     * @param string $name
167
     * @return bool
168
     */
169 6
    public function __isset($name)
170
    {
171 6
        return $this->hasParameter($name);
172
    }
173
174
    /**
175
     * @param string $offset
176
     * @return bool
177
     */
178 3
    public function offsetExists($offset)
179
    {
180 3
        return $this->hasParameter($offset);
181
    }
182
183
    /**
184
     * @param string $offset
185
     * @return mixed
186
     */
187 3
    public function offsetGet($offset)
188
    {
189 3
        return $this->getParameter($offset);
190
    }
191
192
    /**
193
     * @param $offset
194
     * @param mixed $value
195
     */
196 3
    public function offsetSet($offset, $value)
197
    {
198 3
        $this->setParameter($offset, $value);
199 3
    }
200
201
    /**
202
     * @param string $offset
203
     */
204 3
    public function offsetUnset($offset)
205
    {
206 3
        $this->unsetParameter($offset);
207 3
    }
208
209
    /**
210
     * @return ArrayIterator
211
     */
212 3
    public function getIterator()
213
    {
214 3
        return new ArrayIterator($this->parameters);
215
    }
216
217
    /**
218
     * @return array
219
     */
220 3
    public function jsonSerialize()
221
    {
222 3
        return $this->parameters;
223
    }
224
}