TypeUtils   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A integerOrNull() 0 13 5
A compareNumbers() 0 8 3
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