Passed
Push — php70 ( b053c6...055c96 )
by Jeroen De
05:58 queued 03:29
created

ListDiffer::getRealArrayComparer()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 8.8571
cc 6
eloc 8
nc 4
nop 1
crap 6
1
<?php
2
3
namespace Diff\Differ;
4
5
use Diff\ArrayComparer\ArrayComparer;
6
use Diff\ArrayComparer\NativeArrayComparer;
7
use Diff\ArrayComparer\StrictArrayComparer;
8
use Diff\DiffOp\DiffOp;
9
use Diff\DiffOp\DiffOpAdd;
10
use Diff\DiffOp\DiffOpRemove;
11
use InvalidArgumentException;
12
13
/**
14
 * Differ that only looks at the values of the arrays (and thus ignores key differences).
15
 *
16
 * By default values are compared using a StrictArrayComparer.
17
 * You can alter the ArrayComparer used by providing one in the constructor.
18
 *
19
 * @since 0.4
20
 *
21
 * @license GPL-2.0+
22
 * @author Jeroen De Dauw < [email protected] >
23
 */
24
class ListDiffer implements Differ {
25
26
	/**
27
	 * @var ArrayComparer
28
	 */
29
	private $arrayComparer;
30
31 30
	public function __construct( ArrayComparer $arrayComparer = null ) {
32 30
		$this->arrayComparer = $arrayComparer ?? new StrictArrayComparer();
33 30
	}
34
35
	/**
36
	 * @see Differ::doDiff
37
	 *
38
	 * @since 0.4
39
	 *
40
	 * @param array $oldValues The first array
41
	 * @param array $newValues The second array
42
	 *
43
	 * @return DiffOp[]
44
	 */
45 30
	public function doDiff( array $oldValues, array $newValues ): array {
46 30
		$operations = array();
47
48 30
		foreach ( $this->diffArrays( $newValues, $oldValues ) as $addition ) {
49 12
			$operations[] = new DiffOpAdd( $addition );
50
		}
51
52 30
		foreach ( $this->diffArrays( $oldValues, $newValues ) as $removal ) {
53 10
			$operations[] = new DiffOpRemove( $removal );
54
		}
55
56 30
		return $operations;
57
	}
58
59
	/**
60
	 * @param array $arrayOne
61
	 * @param array $arrayTwo
62
	 *
63
	 * @return array
64
	 */
65 30
	private function diffArrays( array $arrayOne, array $arrayTwo ): array {
66 30
		return $this->arrayComparer->diffArrays( $arrayOne, $arrayTwo );
67
	}
68
69
}
70