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

AbstractSorter::ksort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 2
crap 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