Completed
Push — master ( 379b27...5b49bb )
by
unknown
12s
created

src/Diff/ItemDiffer.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\SiteLinkList;
11
12
/**
13
 * @since 1.0
14
 *
15
 * @license GPL-2.0+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class ItemDiffer implements EntityDifferStrategy {
19
20
	/**
21
	 * @var MapDiffer
22
	 */
23
	private $recursiveMapDiffer;
24
25
	/**
26
	 * @var StatementListDiffer
27
	 */
28
	private $statementListDiffer;
29
30
	public function __construct() {
31 6
		$this->recursiveMapDiffer = new MapDiffer( true );
32 6
		$this->statementListDiffer = new StatementListDiffer();
33 6
	}
34 6
35
	/**
36
	 * @param string $entityType
37
	 *
38
	 * @return bool
39
	 */
40
	public function canDiffEntityType( $entityType ) {
41
		return $entityType === 'item';
42
	}
43
44
	/**
45
	 * @param EntityDocument $from
46
	 * @param EntityDocument $to
47
	 *
48
	 * @return ItemDiff
49
	 * @throws InvalidArgumentException
50
	 */
51
	public function diffEntities( EntityDocument $from, EntityDocument $to ) {
52 4
		$this->assertIsItem( $from );
53 4
		$this->assertIsItem( $to );
54 4
55
		return $this->diffItems( $from, $to );
0 ignored issues
show
$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...
$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...
56 4
	}
57
58
	private function assertIsItem( EntityDocument $item ) {
59 4
		if ( !( $item instanceof Item ) ) {
60 4
			throw new InvalidArgumentException( '$item must be an instance of Item' );
61
		}
62
	}
63 4
64
	public function diffItems( Item $from, Item $to ) {
65 6
		$diffOps = $this->recursiveMapDiffer->doDiff(
66 6
			$this->toDiffArray( $from ),
67 6
			$this->toDiffArray( $to )
68 6
		);
69 6
70
		$diffOps['claim'] = $this->statementListDiffer->getDiff( $from->getStatements(), $to->getStatements() );
71 6
72
		return new ItemDiff( $diffOps );
73 6
	}
74
75
	private function toDiffArray( Item $item ) {
76 6
		$array = [];
77 6
78
		$array['aliases'] = $item->getAliasGroups()->toTextArray();
79 6
		$array['label'] = $item->getLabels()->toTextArray();
80 6
		$array['description'] = $item->getDescriptions()->toTextArray();
81 6
		$array['links'] = $this->getSiteLinksInDiffFormat( $item->getSiteLinkList() );
82 6
83
		return $array;
84 6
	}
85
86
	private function getSiteLinksInDiffFormat( SiteLinkList $siteLinks ) {
87 6
		$linksInDiffFormat = [];
88 6
89
		foreach ( $siteLinks->toArray() as $siteLink ) {
90
			$linksInDiffFormat[$siteLink->getSiteId()] = [
91
				'name' => $siteLink->getPageName(),
92
				'badges' => array_map(
93 6
					function( ItemId $id ) {
94 1
						return $id->getSerialization();
95 1
					},
96 1
					$siteLink->getBadges()
97 1
				)
98
			];
99 1
		}
100 1
101 1
		return $linksInDiffFormat;
102 1
	}
103 6
104
	/**
105 6
	 * @param EntityDocument $entity
106
	 *
107
	 * @return ItemDiff
108
	 * @throws InvalidArgumentException
109
	 */
110
	public function getConstructionDiff( EntityDocument $entity ) {
111
		$this->assertIsItem( $entity );
112
		return $this->diffEntities( new Item(), $entity );
113
	}
114 2
115 2
	/**
116 2
	 * @param EntityDocument $entity
117
	 *
118
	 * @return ItemDiff
119
	 * @throws InvalidArgumentException
120
	 */
121
	public function getDestructionDiff( EntityDocument $entity ) {
122
		$this->assertIsItem( $entity );
123
		return $this->diffEntities( $entity, new Item() );
124
	}
125 1
126
}
127