Completed
Push — master ( 53f9c2...126279 )
by
unknown
02:44
created

ItemDiffer::getSiteLinksInDiffFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.0017

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 12
cts 13
cp 0.9231
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2.0017
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use Diff\Differ\MapDiffer;
6
use InvalidArgumentException;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
use Wikibase\DataModel\Entity\Item;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Services\Diff\Internal\StatementListDiffer;
11
use Wikibase\DataModel\SiteLinkList;
12
13
/**
14
 * @since 1.0
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class ItemDiffer implements EntityDifferStrategy {
20
21
	/**
22
	 * @var MapDiffer
23
	 */
24
	private $recursiveMapDiffer;
25
26
	/**
27
	 * @var StatementListDiffer
28
	 */
29
	private $statementListDiffer;
30
31 6
	public function __construct() {
32 6
		$this->recursiveMapDiffer = new MapDiffer( true );
33 6
		$this->statementListDiffer = new StatementListDiffer();
34 6
	}
35
36
	/**
37
	 * @param string $entityType
38
	 *
39
	 * @return bool
40
	 */
41
	public function canDiffEntityType( $entityType ) {
42
		return $entityType === 'item';
43
	}
44
45
	/**
46
	 * @param EntityDocument $from
47
	 * @param EntityDocument $to
48
	 *
49
	 * @return ItemDiff
50
	 * @throws InvalidArgumentException
51
	 */
52 4
	public function diffEntities( EntityDocument $from, EntityDocument $to ) {
53 4
		$this->assertIsItem( $from );
54 4
		$this->assertIsItem( $to );
55
56 4
		return $this->diffItems( $from, $to );
0 ignored issues
show
Compatibility introduced by
$from of type object<Wikibase\DataModel\Entity\EntityDocument> is not a sub-type of object<Wikibase\DataModel\Entity\Item>. It seems like you assume a concrete implementation of the interface Wikibase\DataModel\Entity\EntityDocument to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$to of type object<Wikibase\DataModel\Entity\EntityDocument> is not a sub-type of object<Wikibase\DataModel\Entity\Item>. It seems like you assume a concrete implementation of the interface Wikibase\DataModel\Entity\EntityDocument to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
57
	}
58
59 4
	private function assertIsItem( EntityDocument $item ) {
60 4
		if ( !( $item instanceof Item ) ) {
61
			throw new InvalidArgumentException( '$item must be an instance of Item' );
62
		}
63 4
	}
64
65 6
	public function diffItems( Item $from, Item $to ) {
66 6
		$diffOps = $this->recursiveMapDiffer->doDiff(
67 6
			$this->toDiffArray( $from ),
68 6
			$this->toDiffArray( $to )
69 6
		);
70
71 6
		$diffOps['claim'] = $this->statementListDiffer->getDiff( $from->getStatements(), $to->getStatements() );
72
73 6
		return new ItemDiff( $diffOps );
74
	}
75
76 6
	private function toDiffArray( Item $item ) {
77 6
		$array = array();
78
79 6
		$array['aliases'] = $item->getFingerprint()->getAliasGroups()->toTextArray();
80 6
		$array['label'] = $item->getFingerprint()->getLabels()->toTextArray();
81 6
		$array['description'] = $item->getFingerprint()->getDescriptions()->toTextArray();
82 6
		$array['links'] = $this->getSiteLinksInDiffFormat( $item->getSiteLinkList() );
83
84 6
		return $array;
85
	}
86
87 6
	private function getSiteLinksInDiffFormat( SiteLinkList $siteLinks ) {
88 6
		$linksInDiffFormat = array();
89
90
		foreach ( $siteLinks->toArray() as $siteLink ) {
91
			$linksInDiffFormat[$siteLink->getSiteId()] = array(
92
				'name' => $siteLink->getPageName(),
93 6
				'badges' => array_map(
94 1
					function( ItemId $id ) {
95 1
						return $id->getSerialization();
96 1
					},
97 1
					$siteLink->getBadges()
98
				)
99 1
			);
100 1
		}
101 1
102 1
		return $linksInDiffFormat;
103 6
	}
104
105 6
	/**
106
	 * @param EntityDocument $entity
107
	 *
108
	 * @return ItemDiff
109
	 * @throws InvalidArgumentException
110
	 */
111
	public function getConstructionDiff( EntityDocument $entity ) {
112
		$this->assertIsItem( $entity );
113
		return $this->diffEntities( new Item(), $entity );
114 2
	}
115 2
116 2
	/**
117
	 * @param EntityDocument $entity
118
	 *
119
	 * @return ItemDiff
120
	 * @throws InvalidArgumentException
121
	 */
122
	public function getDestructionDiff( EntityDocument $entity ) {
123
		$this->assertIsItem( $entity );
124
		return $this->diffEntities( $entity, new Item() );
125 1
	}
126 1
127
}
128