Completed
Push — 21x ( 0e3ed4...f4f4da )
by adam
17:20 queued 21s
created

ListDiffer::getRealArrayComparer()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 8
nc 4
nop 1
crap 42
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
	 * Use non-strict comparison and do not care about quantity.
28
	 * This makes use of @see array_diff
29
	 *
30
	 * @since 0.4
31 30
	 * @deprecated since 0.8, use new NativeArrayComparer() instead
32 30
	 */
33 30
	const MODE_NATIVE = 0;
34
35
	/**
36
	 * Use strict comparison and care about quantity.
37
	 * This makes use of @see ListDiffer::strictDiff
38
	 *
39
	 * @since 0.4
40
	 * @deprecated since 0.8, use null instead
41
	 */
42
	const MODE_STRICT = 1;
43
44
	/**
45 30
	 * @var ArrayComparer
46 30
	 */
47
	private $arrayComparer;
48 30
49 12
	/**
50
	 * @param ArrayComparer|int|null $arrayComparer Defaults to a StrictArrayComparer. The use of
51
	 * the self::MODE_... constants is deprecated.
52 30
	 *
53 10
	 * The first argument is an ArrayComparer since version 0.8.
54
	 * Before this it was an element of the ListDiffer::MODE_ enum.
55
	 * The later is still supported though deprecated.
56 30
	 */
57
	public function __construct( $arrayComparer = null ) {
58
		$this->arrayComparer = $this->getRealArrayComparer( $arrayComparer );
59
	}
60
61
	/**
62
	 * @param ArrayComparer|int|null $arrayComparer Defaults to a StrictArrayComparer. The use of
63
	 * the self::MODE_... constants is deprecated.
64
	 *
65 30
	 * @return ArrayComparer
66 30
	 * @throws InvalidArgumentException
67
	 */
68
	private function getRealArrayComparer( $arrayComparer ) {
69
		if ( $arrayComparer === null || $arrayComparer === self::MODE_STRICT ) {
0 ignored issues
show
Deprecated Code introduced by
The constant Diff\Differ\ListDiffer::MODE_STRICT has been deprecated with message: since 0.8, use null instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
70
			return new StrictArrayComparer();
71
		}
72
73
		if ( $arrayComparer === self::MODE_NATIVE ) {
0 ignored issues
show
Deprecated Code introduced by
The constant Diff\Differ\ListDiffer::MODE_NATIVE has been deprecated with message: since 0.8, use new NativeArrayComparer() instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
74
			return new NativeArrayComparer();
75
		}
76
77
		if ( is_object( $arrayComparer ) && $arrayComparer instanceof ArrayComparer ) {
78
			return $arrayComparer;
79
		}
80
81
		throw new InvalidArgumentException( 'Got an invalid $arrayComparer' );
82
	}
83
84
	/**
85
	 * @see Differ::doDiff
86
	 *
87
	 * @since 0.4
88
	 *
89
	 * @param array $oldValues The first array
90
	 * @param array $newValues The second array
91
	 *
92
	 * @return DiffOp[]
93
	 */
94
	public function doDiff( array $oldValues, array $newValues ) {
95
		$operations = array();
96
97
		foreach ( $this->diffArrays( $newValues, $oldValues ) as $addition ) {
98
			$operations[] = new DiffOpAdd( $addition );
99
		}
100
101
		foreach ( $this->diffArrays( $oldValues, $newValues ) as $removal ) {
102
			$operations[] = new DiffOpRemove( $removal );
103
		}
104
105
		return $operations;
106
	}
107
108
	/**
109
	 * @param array $arrayOne
110
	 * @param array $arrayTwo
111
	 *
112
	 * @return array
113
	 */
114
	private function diffArrays( array $arrayOne, array $arrayTwo ) {
115
		return $this->arrayComparer->diffArrays( $arrayOne, $arrayTwo );
116
	}
117
118
}
119