Passed
Push — master ( 5ad58b...7269b1 )
by Chris
07:31
created

AbstractSorter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 64.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 35
ccs 9
cts 14
cp 0.6429
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateOrder() 0 3 1
A ksort() 0 9 1
A sort() 0 9 1
A resolveEntriesOrder() 0 3 2
1
<?php
2
3
namespace WebTheory\Collection\Sorting\Abstracts;
4
5
use WebTheory\Collection\Contracts\CollectionSorterInterface;
6
use WebTheory\Collection\Contracts\OrderInterface;
7
use WebTheory\Collection\Sorting\Order;
8
9
abstract class AbstractSorter implements CollectionSorterInterface
10
{
11 18
    public function sort(array $array, string $order): array
12
    {
13 18
        $this->validateOrder($order);
14
15 12
        $array = $array;
16
17 12
        usort($array, $this->getSortingFunction($order));
18
19 12
        return $array;
20
    }
21
22
    public function ksort(array $array, string $order): array
23
    {
24
        $this->validateOrder($order);
25
26
        $array = $array;
27
28
        uksort($array, $this->getSortingFunction($order));
29
30
        return $array;
31
    }
32
33 12
    protected function resolveEntriesOrder($a, $b, string $order): int
34
    {
35 12
        return ($a <=> $b) * ($order === OrderInterface::DESC ? -1 : 1);
36
    }
37
38 18
    protected function validateOrder($order): void
39
    {
40 18
        Order::throwExceptionIfInvalid($order);
41
    }
42
43
    abstract protected function getSortingFunction(string $order): callable;
44
}
45