Passed
Push — master ( 913c0a...fd8403 )
by Ondřej
05:45
created

ComparableWithPhpOperators::compareTo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
declare(strict_types=1);
3
namespace Ivory\Value\Alg;
4
5
use Ivory\Exception\IncomparableException;
6
7
/**
8
 * Provides an implementation of the {@link IComparable} interface, comparing objects using the PHP `<=>` operator.
9
 *
10
 * Only objects of the exact same class are considered comparable. Other types of values emit an
11
 * {@link IncomparableException}.
12
 */
13
trait ComparableWithPhpOperators
14
{
15
    use EqualableWithPhpOperators;
16
17
    final public function compareTo($other): int
18
    {
19
        if ($other === null) {
20
            throw new \InvalidArgumentException('comparing with null');
21
        } elseif (get_class($this) == get_class($other)) {
22
            return ($this <=> $other);
23
        } else {
24
            throw new IncomparableException();
25
        }
26
    }
27
}
28