ReferencedEntitiesFinder::findForItem()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Queryr\Replicator\EntitySource;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
use Wikibase\DataModel\Entity\EntityIdValue;
7
use Wikibase\DataModel\Entity\Item;
8
use Wikibase\DataModel\Snak\PropertyValueSnak;
9
10
/**
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class ReferencedEntitiesFinder {
15
16
	/**
17
	 * @param Item $item
18
	 *
19
	 * @return EntityId[]
20
	 */
21 3
	public function findForItem( Item $item ): array {
22 3
		$references = [];
23
24 3
		foreach ( $item->getStatements()->toArray() as $statement ) {
25 2
			$references[] = $statement->getPropertyId();
26
27 2
			$mainSnak = $statement->getMainSnak();
28
29 2
			if ( $mainSnak instanceof PropertyValueSnak && $mainSnak->getDataValue() instanceof EntityIdValue ) {
30 2
				$references[] = $mainSnak->getDataValue()->getEntityId();
31
			}
32
		}
33
34 3
		return array_unique( $references );
35
	}
36
37
}