Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ConnectionCheckerHelper
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 findStatementWithProperty
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 findStatementWithPropertyAndEntityIdValue
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 findStatementWithPropertyAndItemIdSnakValues
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Helper;
4
5use Wikibase\DataModel\Entity\EntityId;
6use Wikibase\DataModel\Entity\EntityIdValue;
7use Wikibase\DataModel\Entity\PropertyId;
8use Wikibase\DataModel\Snak\PropertyValueSnak;
9use Wikibase\DataModel\Statement\Statement;
10use Wikibase\DataModel\Statement\StatementList;
11use 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 */
19class 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}