StatementListDiffer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toDiffArray() 0 16 3
A newDiffer() 0 2 1
A getDiff() 0 7 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use Diff\Comparer\ComparableComparer;
6
use Diff\Differ\MapDiffer;
7
use Diff\DiffOp\Diff\Diff;
8
use UnexpectedValueException;
9
use Wikibase\DataModel\Statement\Statement;
10
use Wikibase\DataModel\Statement\StatementList;
11
12
/**
13
 * @since 3.6
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class StatementListDiffer {
19
20
	/**
21
	 * @since 1.0
22
	 *
23
	 * @param StatementList $fromStatements
24
	 * @param StatementList $toStatements
25
	 *
26
	 * @return Diff
27
	 * @throws UnexpectedValueException
28
	 */
29
	public function getDiff( StatementList $fromStatements, StatementList $toStatements ) {
30
		return new Diff(
31
			$this->newDiffer()->doDiff(
32
				$this->toDiffArray( $fromStatements ),
33
				$this->toDiffArray( $toStatements )
34
			),
35
			true
36
		);
37
	}
38
39
	private function newDiffer(): MapDiffer {
40
		return new MapDiffer( false, null, new ComparableComparer() );
41
	}
42
43
	private function toDiffArray( StatementList $statementList ): array {
44
		$statementArray = [];
45
46
		/**
47
		 * @var Statement $statement
48
		 */
49
		foreach ( $statementList as $statement ) {
50
			$guid = $statement->getGuid();
51
			if ( $guid === null ) {
52
				$statementArray[] = $statement;
53
			} else {
54
				$statementArray[$guid] = $statement;
55
			}
56
		}
57
58
		return $statementArray;
59
	}
60
61
}
62