Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.08% covered (success)
96.08%
49 / 51
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemChecker
96.08% covered (success)
96.08%
49 / 51
66.67% covered (warning)
66.67%
2 / 3
11
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSupportedContextTypes
n/a
0 / 0
n/a
0 / 0
1
 getDefaultContextTypes
n/a
0 / 0
n/a
0 / 0
1
 getSupportedEntityTypes
n/a
0 / 0
n/a
0 / 0
1
 checkConstraint
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
4
 checkConstraintParameters
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2
3namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker;
4
5use Wikibase\DataModel\Statement\Statement;
6use WikibaseQuality\ConstraintReport\Constraint;
7use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
8use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
9use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConnectionCheckerHelper;
10use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException;
11use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
12use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage;
13use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
14use WikibaseQuality\ConstraintReport\Role;
15
16/**
17 * @author BP2014N1
18 * @license GPL-2.0-or-later
19 */
20class ItemChecker implements ConstraintChecker {
21
22    /**
23     * @var ConstraintParameterParser
24     */
25    private $constraintParameterParser;
26
27    /**
28     * @var ConnectionCheckerHelper
29     */
30    private $connectionCheckerHelper;
31
32    public function __construct(
33        ConstraintParameterParser $constraintParameterParser,
34        ConnectionCheckerHelper $connectionCheckerHelper
35    ) {
36        $this->constraintParameterParser = $constraintParameterParser;
37        $this->connectionCheckerHelper = $connectionCheckerHelper;
38    }
39
40    /**
41     * @codeCoverageIgnore This method is purely declarative.
42     */
43    public function getSupportedContextTypes() {
44        return [
45            Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE,
46            // TODO T175562
47            Context::TYPE_QUALIFIER => CheckResult::STATUS_TODO,
48            Context::TYPE_REFERENCE => CheckResult::STATUS_TODO,
49        ];
50    }
51
52    /**
53     * @codeCoverageIgnore This method is purely declarative.
54     */
55    public function getDefaultContextTypes() {
56        return [
57            Context::TYPE_STATEMENT,
58            // TODO T175562
59            // Context::TYPE_QUALIFIER,
60            // Context::TYPE_REFERENCE,
61        ];
62    }
63
64    /** @codeCoverageIgnore This method is purely declarative. */
65    public function getSupportedEntityTypes() {
66        return self::ALL_ENTITY_TYPES_SUPPORTED;
67    }
68
69    /**
70     * Checks 'Item' constraint.
71     *
72     * @param Context $context
73     * @param Constraint $constraint
74     *
75     * @throws ConstraintParameterException
76     * @return CheckResult
77     */
78    public function checkConstraint( Context $context, Constraint $constraint ) {
79        if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) {
80            return new CheckResult( $context, $constraint, CheckResult::STATUS_DEPRECATED );
81        }
82
83        $constraintParameters = $constraint->getConstraintParameters();
84        $constraintTypeItemId = $constraint->getConstraintTypeItemId();
85
86        $propertyId = $this->constraintParameterParser->parsePropertyParameter(
87            $constraintParameters,
88            $constraintTypeItemId
89        );
90
91        $items = $this->constraintParameterParser->parseItemsParameter(
92            $constraintParameters,
93            $constraintTypeItemId,
94            false
95        );
96
97        /*
98         * 'Item' can be defined with
99         *   a) a property only
100         *   b) a property and a number of items (each combination of property and item forming an individual claim)
101         */
102        if ( $items === [] ) {
103            $requiredStatement = $this->connectionCheckerHelper->findStatementWithProperty(
104                $context->getEntity()->getStatements(),
105                $propertyId
106            );
107        } else {
108            $requiredStatement = $this->connectionCheckerHelper->findStatementWithPropertyAndItemIdSnakValues(
109                $context->getEntity()->getStatements(),
110                $propertyId,
111                $items
112            );
113        }
114
115        if ( $requiredStatement !== null ) {
116            $status = CheckResult::STATUS_COMPLIANCE;
117            $message = null;
118        } else {
119            $status = CheckResult::STATUS_VIOLATION;
120            $message = ( new ViolationMessage( 'wbqc-violation-message-item' ) )
121                ->withEntityId( $context->getSnak()->getPropertyId(), Role::CONSTRAINT_PROPERTY )
122                ->withEntityId( $propertyId, Role::PREDICATE )
123                ->withItemIdSnakValueList( $items, Role::OBJECT );
124        }
125
126        return new CheckResult( $context, $constraint, $status, $message );
127    }
128
129    public function checkConstraintParameters( Constraint $constraint ) {
130        $constraintParameters = $constraint->getConstraintParameters();
131        $constraintTypeItemId = $constraint->getConstraintTypeItemId();
132        $exceptions = [];
133        try {
134            $this->constraintParameterParser->parsePropertyParameter(
135                $constraintParameters,
136                $constraintTypeItemId
137            );
138        } catch ( ConstraintParameterException $e ) {
139            $exceptions[] = $e;
140        }
141        try {
142            $this->constraintParameterParser->parseItemsParameter(
143                $constraintParameters,
144                $constraintTypeItemId,
145                false
146            );
147        } catch ( ConstraintParameterException $e ) {
148            $exceptions[] = $e;
149        }
150        return $exceptions;
151    }
152
153}