Test Setup Failed
Push — master ( a9e404...0579c6 )
by Alex
03:26
created

HasMutations::flat()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 2
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nxmad\Larapay\Traits;
6
7
use Illuminate\Config\Repository;
8
9
trait HasMutations
10
{
11
    /**
12
     * The original query of request.
13
     *
14
     * @var Repository
15
     */
16
    protected $query;
17
18
    /**
19
     * The mutated query of request.
20
     *
21
     * @var Repository
22
     */
23
    protected $mutated;
24
25
    /**
26
     * Magic get method.
27
     *
28
     * @param $name
29
     *
30
     * @return mixed
31
     */
32
    public function __get($name)
33
    {
34
        return self::get(...func_get_args());
0 ignored issues
show
Bug Best Practice introduced by
The method Nxmad\Larapay\Traits\HasMutations::get() is not static, but was called statically. ( Ignorable by Annotation )

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

34
        return self::/** @scrutinizer ignore-call */ get(...func_get_args());
Loading history...
Bug introduced by
func_get_args() is expanded, but the parameter $field of Nxmad\Larapay\Traits\HasMutations::get() does not expect variable arguments. ( Ignorable by Annotation )

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

34
        return self::get(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
35
    }
36
37
    /**
38
     * Magic set method.
39
     *
40
     * @param $name
41
     * @param $value
42
     *
43
     * @return self
44
     */
45
    public function __set($name, $value)
46
    {
47
        return self::set(...func_get_args());
0 ignored issues
show
Bug Best Practice introduced by
The method Nxmad\Larapay\Traits\HasMutations::set() is not static, but was called statically. ( Ignorable by Annotation )

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

47
        return self::/** @scrutinizer ignore-call */ set(...func_get_args());
Loading history...
Bug introduced by
func_get_args() is expanded, but the parameter $field of Nxmad\Larapay\Traits\HasMutations::set() does not expect variable arguments. ( Ignorable by Annotation )

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

47
        return self::set(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
48
    }
49
50
    /**
51
     * Determine if request has a field.
52
     *
53
     * @param string $field
54
     *
55
     * @return bool
56
     */
57
    public function has(string $field): bool
58
    {
59
        return $this->mutated->has($field);
60
    }
61
62
    /**
63
     * Get mutated request field using accessor.
64
     *
65
     * @param string  $field
66
     * @param mixed   $default
67
     *
68
     * @return mixed
69
     */
70
    public function get(string $field, $default = null)
71
    {
72
        $accessorName = 'get' . $this->camelize($field);
0 ignored issues
show
Bug introduced by
It seems like camelize() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

72
        $accessorName = 'get' . $this->/** @scrutinizer ignore-call */ camelize($field);
Loading history...
73
74
        if (method_exists($this, $accessorName)) {
75
            return $this->{$accessorName}();
76
        }
77
78
        return $this->mutated->get($field, $default);
79
    }
80
81
    /**
82
     * Set request field and then mutate.
83
     *
84
     * @param $field
85
     * @param $value
86
     *
87
     * @return self
88
     */
89
    public function set($field, $value = null)
90
    {
91
        if (is_array($field)) {
92
            return $this->fill($field);
93
        }
94
95
        $mutatorName = 'set' . $this->camelize($field);
96
97
        if (method_exists($this, $mutatorName)) {
98
            $result = $this->{$mutatorName}($value);
99
100
            $this->queryHasChanged();
101
102
            return $result;
103
        }
104
105
        $this->query->set($field, $value);
106
107
        $this->queryHasChanged();
108
109
        return $this;
110
    }
111
112
    /**
113
     * Fill request.
114
     *
115
     * @param array $parameters
116
     *
117
     * @return self
118
     */
119
    public function fill(array $parameters)
120
    {
121
        foreach ($parameters as $key => $value) {
122
            $this->set($key, $value);
123
        }
124
125
        $this->queryHasChanged();
126
127
        return $this;
128
    }
129
130
    /**
131
     * Get full mutated query.
132
     *
133
     * @return array
134
     */
135
    public function all()
136
    {
137
        return $this->mutated->all();
138
    }
139
140
    /**
141
     * Update mutated fields.
142
     */
143
    protected function queryHasChanged()
144
    {
145
        $this->mutated = $this->mutate();
146
    }
147
148
    /**
149
     * Get mutated query.
150
     *
151
     * @return Repository
152
     */
153
    public function mutate()
154
    {
155
        $result = [];
156
        $aliases = $this->aliases;
0 ignored issues
show
Bug Best Practice introduced by
The property aliases does not exist on Nxmad\Larapay\Traits\HasMutations. Since you implemented __get, consider adding a @property annotation.
Loading history...
157
158
        foreach ($this->flat() as $key => $value) {
159
            if (array_key_exists($key, $aliases)) {
160
                $key = $aliases[$key];
161
            }
162
163
            $result[$key] = $value;
164
        }
165
166
        return new Repository($result);
167
    }
168
169
    /**
170
     * Flat request.
171
     *
172
     * @param null $query
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $query is correct as it would always require null to be passed?
Loading history...
173
     * @param string $prefix
174
     *
175
     * @return array
176
     */
177
    public function flat($query = null, $prefix = '')
178
    {
179
        $result = [];
180
181
        if (is_null($query)) {
0 ignored issues
show
introduced by
The condition is_null($query) is always true.
Loading history...
182
            $query = $this->query->all();
183
        }
184
185
        foreach ($query as $key => $value) {
186
            if (is_array($value)) {
187
                $result = array_merge($result, $this->flat($value, "{$prefix}{$key}."));
188
            } else {
189
                $result[$prefix . $key] = $value;
190
            }
191
        }
192
193
        return $result;
194
    }
195
}
196