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

ConfigurableCollator::compare()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 56
rs 8.8333
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