Completed
Push — master ( f2f357...ccb861 )
by
unknown
02:21
created

ConnectionCheckerHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 5
dl 0
loc 80
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findStatementWithProperty() 0 11 2
A findStatementWithPropertyAndEntityIdValue() 0 20 5
A findStatementWithPropertyAndItemIdSnakValues() 0 17 4
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Helper;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
use Wikibase\DataModel\Entity\EntityIdValue;
7
use Wikibase\DataModel\Entity\PropertyId;
8
use Wikibase\DataModel\Snak\PropertyValueSnak;
9
use Wikibase\DataModel\Statement\Statement;
10
use Wikibase\DataModel\Statement\StatementList;
11
use WikibaseQuality\ConstraintReport\ConstraintCheck\ItemIdSnakValue;
12
13
/**
14
 * Class for helper functions for the connection checkers.
15
 *
16
 * @author BP2014N1
17
 * @license GPL-2.0-or-later
18
 */
19
class ConnectionCheckerHelper {
20
21
	/**
22
	 * Finds a statement with the given property ID.
23
	 *
24
	 * @param StatementList $statementList
25
	 * @param PropertyId $propertyId
26
	 *
27
	 * @return Statement|null
28
	 */
29
	public function findStatementWithProperty(
30
		StatementList $statementList,
31
		PropertyId $propertyId
32
	) {
33
		$statementListByPropertyId = $statementList->getByPropertyId( $propertyId );
34
		if ( $statementListByPropertyId->isEmpty() ) {
35
			return null;
36
		} else {
37
			return $statementListByPropertyId->toArray()[0];
38
		}
39
	}
40
41
	/**
42
	 * Finds a statement with the given property ID and entity ID value.
43
	 *
44
	 * @param StatementList $statementList
45
	 * @param PropertyId $propertyId
46
	 * @param EntityId $value
47
	 *
48
	 * @return Statement|null
49
	 */
50
	public function findStatementWithPropertyAndEntityIdValue(
51
		StatementList $statementList,
52
		PropertyId $propertyId,
53
		EntityId $value
54
	) {
55
		$statementListByPropertyId = $statementList->getByPropertyId( $propertyId );
56
		/** @var Statement $statement */
57
		foreach ( $statementListByPropertyId as $statement ) {
58
			$snak = $statement->getMainSnak();
59
			if ( $snak instanceof PropertyValueSnak ) {
60
				$dataValue = $snak->getDataValue();
61
				if ( $dataValue instanceof EntityIdValue &&
62
					$dataValue->getEntityId()->equals( $value )
63
				) {
64
					return $statement;
65
				}
66
			}
67
		}
68
		return null;
69
	}
70
71
	/**
72
	 * Finds a statement with the given property ID and one of the given item ID snak values.
73
	 *
74
	 * @param StatementList $statementList
75
	 * @param PropertyId $propertyId
76
	 * @param ItemIdSnakValue[] $values
77
	 *
78
	 * @return Statement|null
79
	 */
80
	public function findStatementWithPropertyAndItemIdSnakValues(
81
		StatementList $statementList,
82
		PropertyId $propertyId,
83
		array $values
84
	) {
85
		$statementListByPropertyId = $statementList->getByPropertyId( $propertyId );
86
		/** @var Statement $statement */
87
		foreach ( $statementListByPropertyId as $statement ) {
88
			$snak = $statement->getMainSnak();
89
			foreach ( $values as $value ) {
90
				if ( $value->matchesSnak( $snak ) ) {
91
					return $statement;
92
				}
93
			}
94
		}
95
		return null;
96
	}
97
98
}
99