Comparators   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 7
c 0
b 0
f 0
dl 0
loc 34
ccs 8
cts 8
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A objectPropertyComparator() 0 4 1
A scalarComparator() 0 4 1
A callbackComparator() 0 4 1
1
<?php
2
/**
3
 * @author Maxim Sokolovsky
4
 */
5
6
namespace WS\Utils\Collections\Functions;
7
8
use Closure;
9
10
class Comparators
11
{
12
    /**
13
     * Returns <Fn($a: scalar, $b: scalar): int> for scalar value compares such as int, bool, float, string
14
     * @return callable
15
     */
16 6
    public static function scalarComparator(): Closure
17
    {
18
        return static function ($a, $b) {
19 6
            return $a <=> $b;
20 6
        };
21
    }
22
23
    /**
24
     * Returns <Fn($a: object, $b: object): int>  for scalar object property value compares such as int, bool, float, string
25
     * @param string $property
26
     * @return callable
27
     */
28 6
    public static function objectPropertyComparator(string $property): Closure
29
    {
30
        return static function ($a, $b) use ($property) {
31 6
            return ObjectFunctions::getPropertyValue($a, $property) <=> ObjectFunctions::getPropertyValue($b, $property);
32 6
        };
33
    }
34
35
    /**
36
     * Returns <Fn($a: scalar, $b: scalar): int> for scalar value compares such as int, bool, float, string
37
     * @param callable $f <Fn(value: mixed): scalar>
38
     * @return Closure
39
     */
40 7
    public static function callbackComparator(callable $f): Closure
41
    {
42
        return static function ($a, $b) use ($f) {
43 7
            return $f($a) <=> $f($b);
44 7
        };
45
    }
46
}
47