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

MapBasedSorter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 28
ccs 11
cts 11
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperty() 0 3 1
A getComparisonValueFor() 0 3 1
A getSortingFunction() 0 6 1
A setMap() 0 5 1
1
<?php
2
3
namespace WebTheory\Collection\Sorting;
4
5
use WebTheory\Collection\Contracts\CollectionSorterInterface;
6
use WebTheory\Collection\Contracts\OrderInterface;
7
use WebTheory\Collection\Sorting\Abstracts\AbstractPropertyBasedSorter;
8
9
class MapBasedSorter extends AbstractPropertyBasedSorter implements CollectionSorterInterface
10
{
11
    protected array $map = [];
12
13 9
    public function setMap(array $map): MapBasedSorter
14
    {
15 9
        $this->map = $map;
16
17 9
        return $this;
18
    }
19
20 9
    public function setProperty(string $property): MapBasedSorter
21
    {
22 9
        return parent::setProperty($property);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setProperty($property) returns the type WebTheory\Collection\Sor...ractPropertyBasedSorter which includes types incompatible with the type-hinted return WebTheory\Collection\Sorting\MapBasedSorter.
Loading history...
23
    }
24
25 6
    protected function getSortingFunction(string $order = OrderInterface::ASC): callable
26
    {
27 6
        return fn ($a, $b): int => $this->resolveEntriesOrder(
28 6
            $this->getComparisonValueFor($a),
29 6
            $this->getComparisonValueFor($b),
30
            $order
31
        );
32
    }
33
34 6
    protected function getComparisonValueFor($item): int
35
    {
36 6
        return (int) $this->map[$this->resolveValue($item)] ?? 0;
37
    }
38
}
39