Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
98.21% |
55 / 56 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
TypeChecker | |
98.21% |
55 / 56 |
|
66.67% |
2 / 3 |
15 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
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 | |
97.30% |
36 / 37 |
|
0.00% |
0 / 1 |
8 | |||
checkConstraintParameters | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
4 | |
5 | use MediaWiki\Config\Config; |
6 | use Wikibase\DataModel\Statement\Statement; |
7 | use WikibaseQuality\ConstraintReport\Constraint; |
8 | use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
9 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
10 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException; |
11 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser; |
12 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\SparqlHelperException; |
13 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\TypeCheckerHelper; |
14 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
15 | |
16 | /** |
17 | * @author BP2014N1 |
18 | * @license GPL-2.0-or-later |
19 | */ |
20 | class TypeChecker implements ConstraintChecker { |
21 | |
22 | /** |
23 | * @var ConstraintParameterParser |
24 | */ |
25 | private $constraintParameterParser; |
26 | |
27 | /** |
28 | * @var TypeCheckerHelper |
29 | */ |
30 | private $typeCheckerHelper; |
31 | |
32 | /** |
33 | * @var Config |
34 | */ |
35 | private $config; |
36 | |
37 | public function __construct( |
38 | ConstraintParameterParser $constraintParameterParser, |
39 | TypeCheckerHelper $typeCheckerHelper, |
40 | Config $config |
41 | ) { |
42 | $this->constraintParameterParser = $constraintParameterParser; |
43 | $this->typeCheckerHelper = $typeCheckerHelper; |
44 | $this->config = $config; |
45 | } |
46 | |
47 | /** |
48 | * @codeCoverageIgnore This method is purely declarative. |
49 | */ |
50 | public function getSupportedContextTypes() { |
51 | return self::ALL_CONTEXT_TYPES_SUPPORTED; |
52 | } |
53 | |
54 | /** |
55 | * @codeCoverageIgnore This method is purely declarative. |
56 | */ |
57 | public function getDefaultContextTypes() { |
58 | return [ |
59 | Context::TYPE_STATEMENT, |
60 | ]; |
61 | } |
62 | |
63 | /** @codeCoverageIgnore This method is purely declarative. */ |
64 | public function getSupportedEntityTypes() { |
65 | return self::ALL_ENTITY_TYPES_SUPPORTED; |
66 | } |
67 | |
68 | /** |
69 | * Checks 'Type' constraint. |
70 | * |
71 | * @param Context $context |
72 | * @param Constraint $constraint |
73 | * |
74 | * @throws ConstraintParameterException |
75 | * @throws SparqlHelperException if the checker uses SPARQL and the query times out or some other error occurs |
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 | if ( $context->getType() === Context::TYPE_REFERENCE ) { |
83 | return new CheckResult( $context, $constraint, CheckResult::STATUS_NOT_IN_SCOPE ); |
84 | } |
85 | |
86 | $constraintParameters = $constraint->getConstraintParameters(); |
87 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
88 | |
89 | $classes = $this->constraintParameterParser->parseClassParameter( |
90 | $constraintParameters, |
91 | $constraintTypeItemId |
92 | ); |
93 | |
94 | $relation = $this->constraintParameterParser->parseRelationParameter( |
95 | $constraintParameters, |
96 | $constraintTypeItemId |
97 | ); |
98 | $relationIds = []; |
99 | if ( $relation === 'instance' || $relation === 'instanceOrSubclass' ) { |
100 | $relationIds[] = $this->config->get( 'WBQualityConstraintsInstanceOfId' ); |
101 | } |
102 | if ( $relation === 'subclass' || $relation === 'instanceOrSubclass' ) { |
103 | $relationIds[] = $this->config->get( 'WBQualityConstraintsSubclassOfId' ); |
104 | } |
105 | |
106 | $result = $this->typeCheckerHelper->hasClassInRelation( |
107 | $context->getEntity()->getStatements(), |
108 | $relationIds, |
109 | $classes |
110 | ); |
111 | |
112 | if ( $result->getBool() ) { |
113 | $message = null; |
114 | $status = CheckResult::STATUS_COMPLIANCE; |
115 | } else { |
116 | $message = $this->typeCheckerHelper->getViolationMessage( |
117 | $context->getSnak()->getPropertyId(), |
118 | $context->getEntity()->getId(), |
119 | $classes, |
120 | 'type', |
121 | $relation |
122 | ); |
123 | $status = CheckResult::STATUS_VIOLATION; |
124 | } |
125 | |
126 | return ( new CheckResult( $context, $constraint, $status, $message ) ) |
127 | ->withMetadata( $result->getMetadata() ); |
128 | } |
129 | |
130 | public function checkConstraintParameters( Constraint $constraint ) { |
131 | $constraintParameters = $constraint->getConstraintParameters(); |
132 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
133 | $exceptions = []; |
134 | try { |
135 | $this->constraintParameterParser->parseClassParameter( |
136 | $constraintParameters, |
137 | $constraintTypeItemId |
138 | ); |
139 | } catch ( ConstraintParameterException $e ) { |
140 | $exceptions[] = $e; |
141 | } |
142 | try { |
143 | $this->constraintParameterParser->parseRelationParameter( |
144 | $constraintParameters, |
145 | $constraintTypeItemId |
146 | ); |
147 | } catch ( ConstraintParameterException $e ) { |
148 | $exceptions[] = $e; |
149 | } |
150 | return $exceptions; |
151 | } |
152 | |
153 | } |