Completed
Pull Request — master (#42)
by
unknown
01:53
created

macros.php ➔ inArrayRecursive()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 4
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 311 and the first side effect is on line 310.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use Illuminate\Support\Collection;
4
use Illuminate\Support\Debug\Dumper;
5
6
if (! Collection::hasMacro('dd')) {
7
    /*
8
     * Dump the contents of the collection and terminate the script.
9
     */
10
    Collection::macro('dd', function () {
11
        dd($this);
12
    });
13
}
14
15
if (! Collection::hasMacro('dump')) {
16
    /*
17
     * Dump the arguments given followed by the collection.
18
     */
19
    Collection::macro('dump', function () {
20
        Collection::make(func_get_args())
21
            ->push($this)
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
22
            ->each(function ($item) {
23
                (new Dumper)->dump($item);
24
            });
25
26
        return $this;
27
    });
28
}
29
30 View Code Duplication
if (! Collection::hasMacro('ifEmpty')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31
    /*
32
     * Execute a callable if the collection is empty, then return the collection.
33
     *
34
     * @param callable $callback
35
     *
36
     * @return \Illuminate\Support\Collection
37
     */
38
    Collection::macro('ifEmpty', function (callable $callback): Collection {
39
        if ($this->isEmpty()) {
40
            $callback($this);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
41
        }
42
43
        return $this;
44
    });
45
}
46
47 View Code Duplication
if (! Collection::hasMacro('ifAny')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
48
    /*
49
     * Execute a callable if the collection isn't empty, then return the collection.
50
     *
51
     * @param callable callback
52
53
     * @return \Illuminate\Support\Collection
54
     */
55
    Collection::macro('ifAny', function (callable $callback): Collection {
56
        if (! $this->isEmpty()) {
57
            $callback($this);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
58
        }
59
60
        return $this;
61
    });
62
}
63
64 View Code Duplication
if (! Collection::hasMacro('range')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
    /*
66
     * Create a new collection instance with a range of numbers. `range`
67
     * accepts the same parameters as PHP's standard `range` function.
68
     *
69
     * @see range
70
     *
71
     * @param mixed $start
72
     * @param mixed $end
73
     * @param int|float $step
74
     *
75
     * @return \Illuminate\Support\Collection
76
     */
77
    Collection::macro('range', function ($start, $end, $step = 1): Collection {
78
        return new Collection(range($start, $end, $step));
79
    });
80
}
81
82 View Code Duplication
if (! Collection::hasMacro('withSize')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
83
    /*
84
     * Create a new collection with the specified amount of items
85
     *
86
     * @param int $size
87
     *
88
     * @return \Illuminate\Support\Collection
89
     */
90
    Collection::macro('withSize', function (int $size): Collection {
91
        if ($size < 1) {
92
            return new Collection();
93
        }
94
95
        return new Collection(range(1, $size));
96
    });
97
}
98
99
if (! Collection::hasMacro('none')) {
100
    /*
101
     * Check whether a collection doesn't contain any occurrences of a given
102
     * item, key-value pair, or passing truth test. `none` accepts the same
103
     * parameters as the `contains` collection method.
104
     *
105
     * @see \Illuminate\Support\Collection::contains
106
     *
107
     * @param mixed $key
108
     * @param mixed $value
109
     *
110
     * @return bool
111
     */
112
    Collection::macro('none', function ($key, $value = null): bool {
113
        if (func_num_args() === 2) {
114
            return ! $this->contains($key, $value);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
115
        }
116
117
        return ! $this->contains($key);
118
    });
119
}
120
121
if (! Collection::hasMacro('validate')) {
122
    /*
123
     * Returns true if $callback returns true for every item. If $callback
124
     * is a string or an array, regard it as a validation rule.
125
     *
126
     * @param string|callable $callback
127
     *
128
     * @return bool
129
     */
130
    Collection::macro('validate', function ($callback): bool {
131
        if (is_string($callback) || is_array($callback)) {
132
            $validationRule = $callback;
133
134
            $callback = function ($item) use ($validationRule) {
135
                if (! is_array($item)) {
136
                    $item = ['default' => $item];
137
                }
138
139
                if (! is_array($validationRule)) {
140
                    $validationRule = ['default' => $validationRule];
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $validationRule, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
141
                }
142
143
                return app('validator')->make($item, $validationRule)->passes();
144
            };
145
        }
146
147
        foreach ($this->items as $item) {
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
148
            if (! $callback($item)) {
149
                return false;
150
            }
151
        }
152
153
        return true;
154
    });
155
}
156
157
if (! Collection::hasMacro('groupByModel')) {
158
    /*
159
     * Group a collection by an Eloquent model.
160
     *
161
     * @param string|callable $callback
162
     * @param string $keyName
163
     *
164
     * @return \Illuminate\Support\Collection
165
     */
166
    Collection::macro('groupByModel', function ($callback, $keyName = 'model') {
167
        $callback = is_callable($callback) ? $callback : function ($item) use ($callback) {
168
            return $item[$callback];
169
        };
170
171
        return Collection::make($this->items)->map(function ($item) use ($callback) {
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
172
            return ['key' => $callback($item), 'item' => $item];
173
        })->groupBy(function (array $keyedItem) {
174
            return $keyedItem['key']->getKey();
175
        })->map(function (Collection $group) use ($keyName) {
176
            return $group->reduce(function (array $result, array $group) use ($keyName) {
177
                $result[$keyName] = $group['key'];
178
                $result['items'][] = $group['item'];
179
180
                return $result;
181
            }, []);
182
        })->map(function (array $group) {
183
            $group['items'] = Collection::make($group['items']);
184
185
            return $group;
186
        })->values();
187
    });
188
}
189
190
if (! Collection::hasMacro('fromPairs')) {
191
    /*
192
     * Transform a collection into an associative array form collection item.
193
     *
194
     * @return \Illuminate\Support\Collection
195
     */
196
    Collection::macro('fromPairs', function () {
197
        return $this->reduce(function ($assoc, array $keyValuePair): Collection {
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
198
            list($key, $value) = $keyValuePair;
199
            $assoc[$key] = $value;
200
201
            return $assoc;
202
        }, new static);
203
    });
204
}
205
206
if (! Collection::hasMacro('toPairs')) {
207
    /*
208
     * Transform a collection into an an array with pairs.
209
     *
210
     * @return \Illuminate\Support\Collection
211
     */
212
    Collection::macro('toPairs', function () {
213
        return $this->keys()->map(function ($key) {
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
214
            return [$key, $this->items[$key]];
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
215
        });
216
    });
217
}
218
219
if (! Collection::hasMacro('transpose')) {
220
    /*
221
     * Transpose an array.
222
     *
223
     * @return \Illuminate\Support\Collection
224
     *
225
     * @throws \LengthException
226
     */
227
    Collection::macro('transpose', function (): Collection {
228
        $values = $this->values();
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
229
230
        $expectedLength = count($this->first());
231
        $diffLength = count(array_intersect_key(...$values));
232
233
        if ($expectedLength !== $diffLength) {
234
            throw new \LengthException("Element's length must be equal.");
235
        }
236
237
        $items = array_map(function (...$items) {
238
            return new static($items);
239
        }, ...$values);
240
241
        return new static($items);
242
    });
243
}
244
245
if (! Collection::hasMacro('collect')) {
246
    /*
247
     * Get a new collection from the collection by key.
248
     *
249
     * @param  mixed  $key
250
     * @param  mixed  $default
251
     *
252
     * @return static
253
     */
254
    Collection::macro('collect', function ($key, $default = null) {
255
        return new static($this->get($key, $default));
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
256
    });
257
}
258
259
if (! Collection::hasMacro('after')) {
260
    /*
261
     * Get the next item from the collection.
262
     *
263
     * @param mixed $currentItem
264
     * @param mixed $fallback
265
     *
266
     * @return mixed
267
     */
268
    Collection::macro('after', function ($currentItem, $fallback = null) {
269
        $currentKey = $this->search($currentItem, true);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
270
271
        if ($currentKey === false) {
272
            return $fallback;
273
        }
274
275
        $currentOffset = $this->keys()->search($currentKey, true);
276
277
        $next = $this->slice($currentOffset, 2);
278
279
        if ($next->count() < 2) {
280
            return $fallback;
281
        }
282
283
        return $next->last();
284
    });
285
}
286
287
if (! Collection::hasMacro('before')) {
288
    /*
289
     * Get the previous item from the collection.
290
     *
291
     * @param mixed $currentItem
292
     * @param mixed $fallback
293
     *
294
     * @return mixed
295
     */
296
    Collection::macro('before', function ($currentItem, $fallback = null) {
297
        return $this->reverse()->after($currentItem, $fallback);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
298
    });
299
}
300
301
if (! Collection::hasMacro('exists')) {
302
    /*
303
     * Check if value exists in collection.
304
     *
305
     * @param string $item
306
     * @param bool $strict
307
     *
308
     * @return string|bool
309
     */
310
    Collection::macro('exists', function ($item, $strict = false) {
311
        function inArrayRecursive($needle, $haystack, $strict = false, $path = [])
312
        {
313
            foreach ($haystack as $key => $value) {
314
                if ($strict ? $value === $needle : $value == $needle) {
315
                    return implode('.', $path);
316
                } else {
317
                    if (is_array($value)) {
318
                        $path[] = $key;
319
                        
320
                        return inArrayRecursive($needle, $value, $strict, $path);
321
                    }
322
                }
323
            }
324
325
            return false;
326
        }
327
328
        return inArrayRecursive($item, $this->items, $strict, []);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
329
    });
330
}
331
332
if (! Collection::hasMacro('forgetAll')) {
333
334
    /*
335
     * The forgetAll method removes an item from the collection by its key(s).
336
     *
337
     * @param mixed $keys
338
     *
339
     * @return void
340
     */
341
    Collection::macro('forgetAll', function ($keys) {
342
        Arr::forget($this->items, $keys);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
343
    });
344
}
345