Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
32 / 32 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
OneOfChecker | |
100.00% |
32 / 32 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
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% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
checkConstraintParameters | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
4 | |
5 | use Wikibase\DataModel\Statement\Statement; |
6 | use WikibaseQuality\ConstraintReport\Constraint; |
7 | use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
8 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
9 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException; |
10 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser; |
11 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage; |
12 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
13 | use WikibaseQuality\ConstraintReport\Role; |
14 | |
15 | /** |
16 | * @author BP2014N1 |
17 | * @license GPL-2.0-or-later |
18 | */ |
19 | class OneOfChecker implements ConstraintChecker { |
20 | |
21 | /** |
22 | * @var ConstraintParameterParser |
23 | */ |
24 | private $constraintParameterParser; |
25 | |
26 | public function __construct( |
27 | ConstraintParameterParser $constraintParameterParser |
28 | ) { |
29 | $this->constraintParameterParser = $constraintParameterParser; |
30 | } |
31 | |
32 | /** |
33 | * @codeCoverageIgnore This method is purely declarative. |
34 | */ |
35 | public function getSupportedContextTypes() { |
36 | return self::ALL_CONTEXT_TYPES_SUPPORTED; |
37 | } |
38 | |
39 | /** |
40 | * @codeCoverageIgnore This method is purely declarative. |
41 | */ |
42 | public function getDefaultContextTypes() { |
43 | return Context::ALL_CONTEXT_TYPES; |
44 | } |
45 | |
46 | /** @codeCoverageIgnore This method is purely declarative. */ |
47 | public function getSupportedEntityTypes() { |
48 | return self::ALL_ENTITY_TYPES_SUPPORTED; |
49 | } |
50 | |
51 | /** |
52 | * Checks 'One of' constraint. |
53 | * |
54 | * @param Context $context |
55 | * @param Constraint $constraint |
56 | * |
57 | * @throws ConstraintParameterException |
58 | * @return CheckResult |
59 | */ |
60 | public function checkConstraint( Context $context, Constraint $constraint ) { |
61 | if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) { |
62 | return new CheckResult( $context, $constraint, CheckResult::STATUS_DEPRECATED ); |
63 | } |
64 | |
65 | $constraintParameters = $constraint->getConstraintParameters(); |
66 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
67 | |
68 | $items = $this->constraintParameterParser->parseItemsParameter( |
69 | $constraintParameters, |
70 | $constraintTypeItemId, |
71 | true |
72 | ); |
73 | |
74 | $snak = $context->getSnak(); |
75 | |
76 | $message = ( new ViolationMessage( 'wbqc-violation-message-one-of' ) ) |
77 | ->withEntityId( $context->getSnak()->getPropertyId(), Role::PREDICATE ) |
78 | ->withItemIdSnakValueList( $items, Role::OBJECT ); |
79 | $status = CheckResult::STATUS_VIOLATION; |
80 | |
81 | foreach ( $items as $item ) { |
82 | if ( $item->matchesSnak( $snak ) ) { |
83 | $message = null; |
84 | $status = CheckResult::STATUS_COMPLIANCE; |
85 | break; |
86 | } |
87 | } |
88 | |
89 | return new CheckResult( $context, $constraint, $status, $message ); |
90 | } |
91 | |
92 | public function checkConstraintParameters( Constraint $constraint ) { |
93 | $constraintParameters = $constraint->getConstraintParameters(); |
94 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
95 | $exceptions = []; |
96 | try { |
97 | $this->constraintParameterParser->parseItemsParameter( |
98 | $constraintParameters, |
99 | $constraintTypeItemId, |
100 | true |
101 | ); |
102 | } catch ( ConstraintParameterException $e ) { |
103 | $exceptions[] = $e; |
104 | } |
105 | return $exceptions; |
106 | } |
107 | |
108 | } |