Completed
Push — master ( 9d0a0b...952be3 )
by Raphaël
11:37 queued 09:42
created

CollectionSorterTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 5
c 3
b 0
f 3
lcom 0
cbo 1
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reindex() 0 4 1
A sort() 0 6 1
A groupBy() 0 12 3
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
    public function reindex()
10
    {
11
        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
    public function sort(callable $callback)
19
    {
20
        $collection = (array)$this;
21
        uasort($collection, $callback);
22
        return new $this($collection);
23
    }
24
25
    /**
26
     * @param callable $callback
27
     * @return $this[]
28
     */
29
    public function groupBy(callable $callback)
30
    {
31
        $results = [];
32
        $collection = new Collection($this);
33
        foreach ($collection->each($callback) as $key => $result) {
34
            if (!isset($results[$result])) {
35
                $results[$result] = new $this();
36
            }
37
            $results[$result][$key] = $this[$key];
38
        }
39
        return $results;
40
    }
41
}
42