Completed
Push — master ( 44cd2c...638a5b )
by Daniel
05:15
created

Traverse::reject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Narrowspark\Arr;
3
4
class Traverse
5
{
6
7
8
    /**
9
     * Applies the callback to the elements of the given arrays
10
     *
11
     * @param array $array
12
     * @param callable $callback
13
     *
14
     * @return $array
0 ignored issues
show
Documentation introduced by
The doc-type $array could not be parsed: Unknown type name "$array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
15
     */
16
    public function map(array $array, callable $callback)
17
    {
18
        $newArray = [];
19
20
        foreach ($array as $key => $item) {
21
            $result = call_user_func($callback, $item, $key);
22
23
            $newArray = is_array($result) ?
24
                array_replace_recursive($array, $result) :
25
                array_merge_recursive($array, (array) $result);
26
        }
27
28
        return $newArray;
29
    }
30
31
    /**
32
     * Filters each of the given values through a function.
33
     *
34
     * @param array    $array
35
     * @param callable $callback
36
     *
37
     * @return array
38
     */
39
    public function filter(array $array, callable $callback)
40
    {
41
        $newArray = [];
42
43
        foreach ($array as $key => $item) {
44
            if (call_user_func($callback, $item, $key)) {
45
                $newArray[$key] = $item;
46
            }
47
        }
48
49
        return $newArray;
50
    }
51
52
    /**
53
     *  The opposite of filter().
54
     *
55
     *  @param array    $array
56
     *  @param callable $cb Function to filter values.
57
     *
58
     *  @return array filtered array.
59
     */
60
    public static function reject(array $array, callable $cb)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $cb. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
61
    {
62
        return $this->filter($array, function ($value, $key) use ($cb) {
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...
63
            return !call_user_func($cb, $value, $key);
64
        });
65
    }
66
}
67