Passed
Push — master ( 89d4ac...68779c )
by Claudio
02:14
created

ConfigurableCollator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 9
cp 0
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compare() 0 15 7
A __construct() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Budgegeria\IntlSort\Collator;
6
7
use Collator as IntlCollator;
8
9
use function assert;
10
use function is_int;
11
12
class ConfigurableCollator implements Collator
13
{
14
    public function __construct(private IntlCollator $collator, private Configuration $config)
15
    {
16
    }
17
18
    public function compare(mixed $value, mixed $comparativeValue): int
19
    {
20
        if (($comparativeValue === null || $value === null) && $this->config->getNullableSort() === Configuration::NULL_VALUES_LAST) {
21
            return $comparativeValue <=> $value;
22
        }
23
24
        if (($comparativeValue === null || $value === null) && $this->config->getNullableSort() === Configuration::NULL_VALUES_FIRST) {
25
            return Configuration::NULL_VALUES_FIRST;
26
        }
27
28
        $result = $this->collator->compare((string) $value, (string) $comparativeValue);
29
30
        assert(is_int($result));
31
32
        return $result;
33
    }
34
}
35