ArrayImitator::merge()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Arrayzy;
4
5
/**
6
 * Some methods could change the array instance itself
7
 * and some could return a new instance of ObjectOrientedArray.
8
 * This class repeats the PHP built-in functions behavior.
9
 *
10
 * @author Victor Bocharsky <[email protected]>
11
 */
12
class ArrayImitator extends AbstractArray
13
{
14
    // The public method list ordered by ASC
15
16
    /**
17
     * Add a new element to the current array.
18
     *
19
     * @param mixed $element
20
     *
21
     * @return ArrayImitator The current array with added value
22
     */
23 1
    public function add($element)
24
    {
25 1
        $this->elements[] = $element;
26
27 1
        return $this;
28
    }
29
30
    /**
31
     * Create a chunked version of current array.
32
     *
33
     * @param int $size Size of each chunk
34
     * @param bool $preserveKeys Whether array keys are preserved or no
35
     *
36
     * @return ArrayImitator A new array of chunks from the current array
37
     */
38 4
    public function chunk($size, $preserveKeys = false)
39
    {
40 4
        return new static(array_chunk($this->elements, $size, $preserveKeys));
41
    }
42
43
    /**
44
     * Clear the current array.
45
     *
46
     * @return ArrayImitator The current empty array
47
     */
48 4
    public function clear()
49
    {
50 4
        $this->elements = [];
51
52 4
        return $this;
53
    }
54
55
    /**
56
     * Create an array using the current array as keys and the other array as values.
57
     *
58
     * @param array $array Values array
59
     *
60
     * @return ArrayImitator A new array with values from the other array
61
     */
62 1
    public function combine(array $array)
63
    {
64 1
        return new static(array_combine($this->elements, $array));
65
    }
66
67
    /**
68
     * Compute the current array values which not present in the given one.
69
     *
70
     * @param array $array Array for diff
71
     *
72
     * @return ArrayImitator A new array containing all the entries from this array
73
     * that are not present in $array
74
     */
75 4
    public function diff(array $array)
76
    {
77 4
        return new static(array_diff($this->elements, $array));
78
    }
79
80
    /**
81
     * Filter the current array for elements satisfying the predicate $func.
82
     *
83
     * @param callable $func
84
     *
85
     * @return ArrayImitator A new array with only element satisfying $func
86
     */
87 4
    public function filter(callable $func)
88
    {
89 4
        return new static(array_filter($this->elements, $func));
90
    }
91
92
    /**
93
     * Exchanges all keys of current array with their associated values.
94
     *
95
     * @return ArrayImitator A new array with flipped elements
96
     */
97 4
    public function flip()
98
    {
99 4
        return new static(array_flip($this->elements));
100
    }
101
102
    /**
103
     * Compute the current array values which present in the given one.
104
     *
105
     * @param array $array Array for intersect
106
     *
107
     * @return ArrayImitator An array with containing all the entries from this array
108
     * that are present in $array
109
     */
110 4
    public function intersect(array $array)
111
    {
112 4
        return new static(array_intersect($this->elements, $array));
113
    }
114
115
    /**
116
     * Compute the current array values with additional index check
117
     *
118
     * @param array $array Array for intersect
119
     *
120
     * @return ArrayImitator An array with containing all the entries from this array
121
     * that are present in $array. Note that the keys are also used in the comparison
122
     * unlike in intersect().
123
     */
124 4
    public function intersectAssoc(array $array)
125
    {
126 4
        return new static(array_intersect_assoc($this->elements, $array));
127
    }
128
129
    /**
130
     * Compute the current array using keys for comparison which present in the given one.
131
     *
132
     * @param array $array Array for intersect
133
     *
134
     * @return ArrayImitator An array with containing all the entries from this array
135
     * which have keys that are present in $array.
136
     */
137 4
    public function intersectKey(array $array)
138
    {
139 4
        return new static(array_intersect_key($this->elements, $array));
140
    }
141
142
    /**
143
     * Apply the given function to the every element of the current array,
144
     * collecting the results.
145
     *
146
     * @param callable $func
147
     *
148
     * @return ArrayImitator A new array with modified elements
149
     */
150 4
    public function map(callable $func)
151
    {
152 4
        return new static(array_map($func, $this->elements));
153
    }
154
155
    /**
156
     * Merge the current array with the provided one. The latter array is overwriting.
157
     *
158
     * @param array $array Array to merge with (overwrites)
159
     * @param bool $recursively Whether array will be merged recursively or no
160
     *
161
     * @return ArrayImitator A new array with the keys/values from $array added
162
     */
163 8 View Code Duplication
    public function merge(array $array, $recursively = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165 8
        if (true === $recursively) {
166 4
            return new static(array_merge_recursive($this->elements, $array));
167
        }
168
169 4
        return new static(array_merge($this->elements, $array));
170
    }
171
172
    /**
173
     * Pad the current array to the specified size with a given value.
174
     *
175
     * @param int $size Size of the result array
176
     * @param mixed $value Empty value by default
177
     *
178
     * @return ArrayImitator A new array padded to $size with $value
179
     */
180 4
    public function pad($size, $value)
181
    {
182 4
        return new static(array_pad($this->elements, $size, $value));
183
    }
184
185
    /**
186
     * Create a numerically re-indexed array based on the current array.
187
     *
188
     * @return ArrayImitator A new array with re-indexed elements
189
     */
190 4
    public function reindex()
191
    {
192 4
        return new static(array_values($this->elements));
193
    }
194
195
    /**
196
     * Replace values in the current array with values in the given one
197
     * that have the same key.
198
     *
199
     * @param array $array Array of replacing values
200
     * @param bool $recursively Whether array will be replaced recursively or no
201
     *
202
     * @return ArrayImitator A new array with the same keys but new values
203
     */
204 8 View Code Duplication
    public function replace(array $array, $recursively = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206 8
        if (true === $recursively) {
207 4
            return new static(array_replace_recursive($this->elements, $array));
208
        }
209
210 4
        return new static(array_replace($this->elements, $array));
211
    }
212
213
    /**
214
     * Reverse the values order of the current array.
215
     *
216
     * @param bool $preserveKeys Whether array keys are preserved or no
217
     *
218
     * @return ArrayImitator A new array with the order of the elements reversed
219
     */
220 4
    public function reverse($preserveKeys = false)
221
    {
222 4
        return new static(array_reverse($this->elements, $preserveKeys));
223
    }
224
225
    /**
226
     * Randomize elements order of the current array.
227
     *
228
     * @return ArrayImitator The current array with the shuffled elements order
229
     */
230 4
    public function shuffle()
231
    {
232 4
        shuffle($this->elements);
233
234 4
        return $this;
235
    }
236
237
    /**
238
     * Extract a slice of the current array.
239
     *
240
     * @param int $offset Slice begin index
241
     * @param int|null $length Length of the slice
242
     * @param bool $preserveKeys Whether array keys are preserved or no
243
     *
244
     * @return ArrayImitator A new array, which is slice of the current array
245
     * with specified $length
246
     */
247 4
    public function slice($offset, $length = null, $preserveKeys = false)
248
    {
249 4
        return new static(array_slice($this->elements, $offset, $length, $preserveKeys));
250
    }
251
252
    /**
253
     * Remove duplicate values from the current array.
254
     *
255
     * @param int|null $sortFlags
256
     *
257
     * @return ArrayImitator A new array with only unique elements
258
     */
259 4
    public function unique($sortFlags = null)
260
    {
261 4
        return new static(array_unique($this->elements, $sortFlags));
262
    }
263
264
    /**
265
     * Apply the given function to the every element of the current array,
266
     * discarding the results.
267
     *
268
     * @param callable $func
269
     * @param bool $recursively Whether array will be walked recursively or no
270
     *
271
     * @return ArrayImitator The current array with modified elements
272
     */
273 8
    public function walk(callable $func, $recursively = false)
274
    {
275 8
        if (true === $recursively) {
276 4
            array_walk_recursive($this->elements, $func);
277 4
        } else {
278 4
            array_walk($this->elements, $func);
279
        }
280
281 8
        return $this;
282
    }
283
}
284