Passed
Push — master ( e3acee...71d19a )
by Jeroen De
02:21
created

CallbackComparer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 24
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A valuesAreEqual() 0 9 2
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 BSD-3-Clause
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 4
	public function __construct( $callback ) {
26 4
		$this->callback = $callback;
27 4
	}
28
29 4
	public function valuesAreEqual( $firstValue, $secondValue ): bool {
30 4
		$valuesAreEqual = call_user_func_array( $this->callback, [ $firstValue, $secondValue ] );
31
32 4
		if ( !is_bool( $valuesAreEqual ) ) {
33 1
			throw new \RuntimeException( 'ValueComparer callback needs to return a boolean' );
34
		}
35
36 3
		return $valuesAreEqual;
37
	}
38
39
}
40