CollectionFilterTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 87.88%

Importance

Changes 7
Bugs 1 Features 5
Metric Value
c 7
b 1
f 5
dl 0
loc 89
wmc 14
lcom 0
cbo 0
ccs 29
cts 33
cp 0.8788
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 13 4
A each() 0 9 2
A assert() 0 9 3
A extract() 0 6 2
A rand() 0 11 3
1
<?php
2
namespace TRex\Collection;
3
4
trait CollectionFilterTrait
5
{
6
    /**
7
     * @param callable|null $filter
8
     * @param int $flag (only for php version >= 5.6)
9
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
10
     */
11 1
    public function filter(callable $filter = null, $flag = 0)
12
    {
13 1
        if (defined('HHVM_VERSION')) {
14
            if (version_compare(HHVM_VERSION, '3.8.1', '>=')) {
15
                return new $this(array_filter((array)$this, $filter, $flag));
16
            }
17
        } else {
18 1
            if (version_compare(phpversion(), '5.6.0', '>=')) {
19
                return new $this(array_filter((array)$this, $filter, $flag));
20
            }
21
        }
22 1
        return new $this(array_filter((array)$this, $filter));
23
    }
24
25
    /**
26
     * Executes the callback for every value.
27
     * Returns a collection with the result of each callback call.
28
     * If the callback returns nothing, the returned collection will contain same values as the original ones.
29
     *
30
     * @param callable $callback
31
     * @param array $args
32
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
33
     */
34 3
    public function each(callable $callback, array $args = [])
35
    {
36 3
        return new $this(array_map(
37 3
            function ($value) use ($callback, $args) {
38 3
                return call_user_func($callback, $value, $args) ?: $value;
39 3
            },
40
            (array)$this
41 3
        ));
42
    }
43
44
    /**
45
     * Executes the callback for every value.
46
     * Returns false if the callback returns once a falsy value.
47
     *
48
     * @param callable $callback
49
     * @return bool
50
     */
51 2
    public function assert(callable $callback)
52
    {
53 2
        foreach ($this as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<TRex\Collection\CollectionFilterTrait> is not traversable.
Loading history...
54 2
            if (!call_user_func($callback, $value)) {
55 1
                return false;
56
            }
57 1
        }
58 1
        return true;
59
    }
60
61
    /**
62
     * Extracts the sequence of elements.
63
     * Starts at index $startIndex and stop after $length keys.
64
     *
65
     * @param int $startIndex
66
     * @param int $length
67
     * @param bool $areKeysPreserved
68
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
69
     */
70 3
    public function extract($startIndex, $length = 0, $areKeysPreserved = true)
71
    {
72 3
        $collection = (array)$this;
73 3
        $length = $length ?: count($collection);
74 3
        return new $this(array_slice($collection, $startIndex, $length, $areKeysPreserved));
75
    }
76
77
    /**
78
     * @param int $length
79
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
80
     */
81 1
    public function rand($length = 1)
82
    {
83 1
        $keys = (array)array_rand((array)$this, $length);
84 1
        $result = new $this;
85 1
        foreach ($this as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<TRex\Collection\CollectionFilterTrait> is not traversable.
Loading history...
86 1
            if (in_array($key, $keys)) {
87 1
                $result[$key] = $value;
88 1
            }
89 1
        }
90 1
        return $result;
91
    }
92
}