Completed
Push — master ( 1c5793...a5a83d )
by Jeroen De
08:38 queued 05:51
created

CallbackComparer::valuesAreEqual()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.032
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Diff\Comparer;
6
7
/**
8
 * Adapter around a comparison callback that implements the ValueComparer
9
 * interface.
10
 *
11
 * @since 0.6
12
 *
13
 * @license GPL-2.0+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class CallbackComparer implements ValueComparer {
17
18
	private $callback;
19
20
	/**
21
	 * @since 0.6
22
	 *
23
	 * @param callable $callback
24
	 */
25 7
	public function __construct( $callback ) {
26 7
		$this->callback = $callback;
27 7
	}
28
29 7
	public function valuesAreEqual( $firstValue, $secondValue ): bool {
30 7
		$valuesAreEqual = call_user_func_array( $this->callback, array( $firstValue, $secondValue ) );
31
32 7
		if ( !is_bool( $valuesAreEqual ) ) {
33
			throw new \RuntimeException( 'ValueComparer callback needs to return a boolean' );
34
		}
35
36 7
		return $valuesAreEqual;
37
	}
38
39
}
40