TypeUtils::compareNumbers()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Dontdrinkandroot\Utils;
4
5
/**
6
 * @author Philip Washington Sorst <[email protected]>
7
 */
8
class TypeUtils
9
{
10
    /**
11
     * Asserts that the input is an integerish, otherwise null will be returned.
12
     *
13
     * @param mixed $value The input value.
14
     *
15
     * @return integer|null
16
     */
17
    public static function integerOrNull($value): ?int
18
    {
19
        $intVal = intval($value);
20
        if (is_object($value)
21
            || strval($intVal) != $value
22
            || is_bool($value)
23
            || is_null($value)
24
        ) {
25
            return null;
26
        }
27
28
        return $intVal;
29
    }
30
31
    /**
32
     * @param int|float $num1
33
     * @param int|float $num2
34
     *
35
     * @return int
36
     */
37
    public static function compareNumbers($num1, $num2): ?int
38
    {
39
        if ($num1 == $num2) {
40
            return 0;
41
        }
42
43
        return ($num1 < $num2) ? -1 : 1;
44
    }
45
}
46