Passed
Push — master ( 5b9b0c...e86726 )
by Nelson
02:47
created

Numbers::ensureIsNumeric()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Copyright © 2019 Nelson Martell (http://nelson6e65.github.io)
6
 *
7
 * Licensed under The MIT License (MIT)
8
 * For full copyright and license information, please see the LICENSE
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright 2019 Nelson Martell
12
 * @link      http://nelson6e65.github.io/php_nml/
13
 * @since     1.0.0
14
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
15
 * */
16
namespace NelsonMartell\Extensions;
17
18
use InvalidArgumentException;
19
20
use function NelsonMartell\msg;
21
use function NelsonMartell\typeof;
22
use NelsonMartell\IComparer;
23
24
/**
25
 * Provides extension methods to handle numbers.
26
 *
27
 * @since 1.0.0
28
 * @author Nelson Martell <[email protected]>
29
 * */
30
class Numbers implements IComparer
31
{
32
    /**
33
     * Ensures that object given is an integer. Else, thows an exception.
34
     *
35
     * @param mixed $obj Object to validate.
36
     *
37
     * @return float|int Same object given, but ensured that is a number.
38
     *
39
     * @throws InvalidArgumentException if object is not a `int` or `float`.
40
     */
41
    public static function ensureIsNumeric($obj)
42
    {
43
        if (!is_int($obj) || !is_float($obj) || is_nan($obj)) {
0 ignored issues
show
introduced by
The condition is_float($obj) is always false.
Loading history...
44
            $msg = msg('Provided object must to be an integer or float; "{0}" given.', typeof($obj));
45
            throw new InvalidArgumentException($msg);
46
        }
47
48
        return $obj;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     * @param int|float|mixed $left
54
     * @param int|float|mixed $right
55
     *
56
     * @return int|null
57
     *
58
     * @since 1.0.0
59
     */
60 42
    public static function compare($left, $right)
61
    {
62 42
        $r = null;
63
64 42
        if (is_numeric($left)) {
65 42
            if (is_numeric($right)) {
66 30
                $r = $left - $right;
67
68 30
                if ($r > 0) {
69 14
                    $r = (int) ceil($r);
70
                } else {
71 30
                    $r = (int) floor($r);
72
                }
73 12
            } elseif ($right === null || is_nan($right)) {
74 8
                $r = 1;
75 4
            } elseif (typeof($right)->isCustom()) {
76
                $r = -1;
77
            } else {
78 42
                $r = null;
79
            }
80 6
        } elseif (is_numeric($right)) {
81 6
            $r = static::compare($right, $left);
82
83 6
            if ($r !== null) {
84 6
                $r *= -1;
85
            }
86
        } else {
87
            $r = null;
88
        }
89
90 42
        return $r;
91
    }
92
}
93