Completed
Push — master ( b7a8b5...1308b9 )
by Raphaël
01:53
created

CollectionSorterTrait::reverse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace TRex\Collection;
3
4
trait CollectionSorterTrait
5
{
6
    /**
7
     * @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...
8
     */
9 1
    public function reindex()
10
    {
11 1
        return new $this(array_values((array)$this));
12
    }
13
14
    /**
15
     * @param callable $callback
16
     * @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...
17
     */
18 2
    public function sort(callable $callback)
19
    {
20 2
        $collection = (array)$this;
21 2
        uasort($collection, $callback);
22 2
        return new $this($collection);
23
    }
24
25
    /**
26
     * @param bool $areKeysPreserved
27
     * @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...
28
     */
29 1
    public function reverse($areKeysPreserved = true)
30
    {
31 1
        return new $this(array_reverse((array)$this, $areKeysPreserved));
32
    }
33
34
    /**
35
     * @param bool $areKeysPreserved
36
     * @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...
37
     */
38 2
    public function shuffle($areKeysPreserved = true)
39
    {
40 2
        if($areKeysPreserved){
41 1
            return $this->sort(function(){
42 1
                return mt_rand(-1, 1);
43 1
            });
44
        }
45
46 1
        $collection = (array)$this;
47 1
        shuffle($collection);
48 1
        return new $this($collection);
49
    }
50
51
    /**
52
     * @param callable $callback
53
     * @return $this[]
54
     */
55 1
    public function groupBy(callable $callback)
56
    {
57 1
        $results = [];
58 1
        $collection = new Collection($this);
59 1
        foreach ($collection->each($callback) as $key => $result) {
60 1
            if (!isset($results[$result])) {
61 1
                $results[$result] = new $this();
62 1
            }
63 1
            $results[$result][$key] = $this[$key];
64 1
        }
65 1
        return $results;
66
    }
67
}
68