ComparableComparator::compare()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Phootwork package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license MIT License
8
 * @copyright Thomas Gossmann
9
 */
10
namespace phootwork\lang;
11
12
use InvalidArgumentException;
13
14
/**
15
 * Class ComparableComparator
16
 *
17
 * Comparator for objects implementing phootwork\lang\Comparable interface.
18
 *
19
 * @author Thomas Gossmann
20
 */
21
class ComparableComparator implements Comparator {
22
	/**
23
	 * @param mixed $a
24
	 * @param mixed $b
25
	 *
26
	 * @throws InvalidArgumentException If the objects don't implement phootwork\lang\Comparable interface.
27
	 *
28
	 * @return int
29
	 */
30 5
	public function compare(mixed $a, mixed $b): int {
31 5
		if (! $a instanceof Comparable) {
32 1
			throw new InvalidArgumentException(
33 1
				"ComparableComparator can compare only objects implementing phootwork\lang\Comparable interface"
34 1
			);
35
		}
36
37 4
		return $a->compareTo($b);
38
	}
39
}
40