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

ComparableWithPhpOperators   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 15
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A compareTo() 0 10 3
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