ItemPatcher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 67
ccs 28
cts 28
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A canPatchEntityType() 0 3 1
A patchEntity() 0 5 1
A assertIsItem() 0 5 2
A patchItem() 0 15 2
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\EntityDocument;
7
use Wikibase\DataModel\Entity\Item;
8
use Wikibase\DataModel\Services\Diff\Internal\FingerprintPatcher;
9
use Wikibase\DataModel\Services\Diff\Internal\SiteLinkListPatcher;
10
11
/**
12
 * @since 1.0
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class ItemPatcher implements EntityPatcherStrategy {
18
19
	/**
20
	 * @var FingerprintPatcher
21
	 */
22
	private $fingerprintPatcher;
23
24
	/**
25
	 * @var StatementListPatcher
26
	 */
27
	private $statementListPatcher;
28
29
	/**
30
	 * @var SiteLinkListPatcher
31
	 */
32
	private $siteLinkListPatcher;
33
34
	public function __construct() {
35 4
		$this->fingerprintPatcher = new FingerprintPatcher();
36 4
		$this->statementListPatcher = new StatementListPatcher();
37 4
		$this->siteLinkListPatcher = new SiteLinkListPatcher();
38 4
	}
39 4
40
	/**
41
	 * @param string $entityType
42
	 *
43
	 * @return boolean
44
	 */
45
	public function canPatchEntityType( $entityType ) {
46 1
		return $entityType === 'item';
47 1
	}
48
49
	/**
50
	 * @param EntityDocument $entity
51
	 * @param EntityDiff $patch
52
	 *
53
	 * @throws InvalidArgumentException
54
	 */
55
	public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
56
		$this->assertIsItem( $entity );
57 3
58 3
		$this->patchItem( $entity, $patch );
0 ignored issues
show
Compatibility introduced by
$entity 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...
59
	}
60 2
61 2
	private function assertIsItem( EntityDocument $item ) {
62
		if ( !( $item instanceof Item ) ) {
63 3
			throw new InvalidArgumentException( '$item must be an instance of Item' );
64 3
		}
65 1
	}
66
67 2
	private function patchItem( Item $item, EntityDiff $patch ) {
68
		$this->fingerprintPatcher->patchFingerprint( $item->getFingerprint(), $patch );
69 2
70 2
		if ( $patch instanceof ItemDiff ) {
71
			$item->setSiteLinkList( $this->siteLinkListPatcher->getPatchedSiteLinkList(
72 2
				$item->getSiteLinkList(),
73 2
				$patch->getSiteLinkDiff()
74 2
			) );
75 2
		}
76 2
77 2
		$this->statementListPatcher->patchStatementList(
78
			$item->getStatements(),
79 2
			$patch->getClaimsDiff()
80 2
		);
81 2
	}
82 2
83
}
84