Completed
Push — master ( 22ed49...a2d5e3 )
by Raphaël
03:11
created

CollectionSorterTrait::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
namespace TRex\Collection;
3
4
trait CollectionSorterTrait
5
{
6
    /**
7
     * Reindex the keys
8
     *
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 reindex()
12
    {
13 1
        return new $this(array_values((array)$this));
14
    }
15
16
    /**
17
     * @param callable $callback
18
     * @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...
19
     * @deprecated
20
     */
21
    public function sort(callable $callback)
22
    {
23
        $collection = (array)$this;
24
        uasort($collection, $callback);
25
        return new $this($collection);
26
    }
27
28
    /**
29
     * Reverses the values
30
     *
31
     * @param bool $areKeysPreserved
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 1
    public function reverse($areKeysPreserved = true)
35
    {
36 1
        return new $this(array_reverse((array)$this, $areKeysPreserved));
37
    }
38
39
    /**
40
     * Shuffles the values
41
     *
42
     * @param bool $areKeysPreserved
43
     * @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...
44
     */
45 2
    public function shuffle($areKeysPreserved = true)
46
    {
47 2
        if($areKeysPreserved){
48 1
            return $this->sort(function(){
0 ignored issues
show
Deprecated Code introduced by
The method TRex\Collection\CollectionSorterTrait::sort() has been deprecated.

This method has been deprecated.

Loading history...
49 1
                return mt_rand(-1, 1);
50 1
            });
51
        }
52
53 1
        $collection = (array)$this;
54 1
        shuffle($collection);
55 1
        return new $this($collection);
56
    }
57
58
    /**
59
     * Splits the values into every possible response returned by the callback.
60
     *
61
     * For example, if the callback juste return the value:
62
     * ['a', 'b', 'c']
63
     * will become:
64
     * ['a' => ['a'], 'b' => ['b'], 'c' => ['c']]
65
     *
66
     * If the callback return a boolean, it will be return an array with two collections.
67
     * [0 => $nonMatchedCollection, 1 => $matchedCollection]
68
     * This seems to be quite the same as Doctrine's ARrayCollection. But be careful because keys are inverted.
69
     *
70
     * @param callable $callback
71
     * @return $this[]
72
     */
73 1
    public function groupBy(callable $callback)
74
    {
75 1
        $results = [];
76 1
        $collection = new Collection($this);
77 1
        foreach ($collection->each($callback) as $key => $result) {
78 1
            if (!isset($results[$result])) {
79 1
                $results[$result] = new $this();
80 1
            }
81 1
            $results[$result][$key] = $this[$key];
82 1
        }
83 1
        return $results;
84
    }
85
}
86