collection.php ➔ merge()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Pitchart\Collection\Helpers;
4
5
use Pitchart\Collection\CollectionInterface;
6
use Pitchart\Collection\Collection;
7
use Pitchart\Collection\GeneratorCollection;
8
9
/**
10
 * Get a Collection from an iterable list
11
 *
12
 * @param  iterable $items
13
 *
14
 * @return Collection
15
 */
16
function collect($items)
17
{
18 1
    return Collection::from($items);
19
}
20
21
/**
22
 * Get a Collection from an iterable list
23
 *
24
 * @param  iterable $items
25
 *
26
 * @return GeneratorCollection
27
 */
28
function generator($items)
29
{
30 1
    return GeneratorCollection::from($items);
31
}
32
33
/**
34
 * Applies a callable to the elements of an iterable list and returns a Collection
35
 *
36
 * @param  iterable $items
37
 * @param  callable $callable The callable takes an element as parameter and returns a transformed result
38
 *
39
 * @return CollectionInterface
40
 */
41
function map($items, $callable)
42
{
43 6
    if (is_array($items)
44 4
        || !($items instanceof CollectionInterface)
45 6
    ) {
46 4
        $items = Collection::from($items);
0 ignored issues
show
Bug introduced by
It seems like $items defined by \Pitchart\Collection\Collection::from($items) on line 46 can also be of type array; however, Pitchart\Collection\Collection::from() does only seem to accept object<Pitchart\Collection\iterable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47 4
    }
48 6
    return $items->map($callable);
49
}
50
51
/**
52
 * Filters elements of an iterable list and returns a Collection
53
 *
54
 * @param  iterable $items
55
 * @param  callable $callable The callable takes an element as parameter and returns a boolean
56
 *
57
 * @return CollectionInterface
58
 */
59
function filter($items, $callable)
60
{
61 6
    if (is_array($items)
62 4
        || !($items instanceof CollectionInterface)
63 6
    ) {
64 4
        $items = Collection::from($items);
0 ignored issues
show
Bug introduced by
It seems like $items defined by \Pitchart\Collection\Collection::from($items) on line 64 can also be of type array; however, Pitchart\Collection\Collection::from() does only seem to accept object<Pitchart\Collection\iterable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65 4
    }
66 6
    return $items->filter($callable);
67
}
68
69
/**
70
 * Rejects elements of an iterable list and returns a Collection
71
 *
72
 * @param  iterable $items
73
 * @param  callable $callable The callable takes an element as parameter and returns a boolean
74
 *
75
 * @return CollectionInterface
76
 */
77
function reject($items, $callable)
78
{
79 6
    if (is_array($items)
80 4
        || !($items instanceof CollectionInterface)
81 6
    ) {
82 4
        $items = Collection::from($items);
0 ignored issues
show
Bug introduced by
It seems like $items defined by \Pitchart\Collection\Collection::from($items) on line 82 can also be of type array; however, Pitchart\Collection\Collection::from() does only seem to accept object<Pitchart\Collection\iterable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
83 4
    }
84 6
    return $items->reject($callable);
85
}
86
87
/**
88
 * Merge one or more iterables and returns a collection
89
 *
90
 * @param iterable $iterables
91
 *
92
 * @return CollectionInterface
93
 */
94
function merge(...$iterables)
95
{
96 4
    foreach ($iterables as $position => $iterable) {
97 4
        if (is_array($iterable)
98 3
            || !($iterable instanceof CollectionInterface)
99 4
        ) {
100 4
            $iterables[$position] = Collection::from($iterable);
0 ignored issues
show
Bug introduced by
It seems like $iterable defined by $iterable on line 96 can also be of type array; however, Pitchart\Collection\Collection::from() does only seem to accept object<Pitchart\Collection\iterable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101 4
        }
102 4
    }
103
    /** @var CollectionInterface $first */
104 4
    $first = array_shift($iterables);
105 4
    return $first->merge(...$iterables);
106
}
107
108
/**
109
 * Iteratively reduce an iterable to a single value using a callable
110
 *
111
 * @param callable $callable The callable takes an accumulator and an iterable element as parameters and returns a values which will be the accumulator of the next iteration
112
 * @param mixed    $initial
113
 *
114
 * @return mixed
115
 */
116
function reduce($items, $callable, $initial)
117
{
118 6
    if (is_array($items)
119 4
        || !($items instanceof CollectionInterface)
120 6
    ) {
121 4
        $items = Collection::from($items);
122 4
    }
123 6
    return $items->reduce($callable, $initial);
124
}
125